Help with mysql and php connecting!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jamesmoore
    New Member
    • Jan 2011
    • 47

    Help with mysql and php connecting!

    Hi,
    I am trying to connect with mysql. I have followed a video tutorial and it works fine for the person in the video but not for me.

    The main php file code is as follows:

    Code:
    <?php
    require("connect.php")
    $name=$_POST['name'];
    $comment=$_POST['comment'];
    $submit=$_POST['submit'];
    if($submit)
    {
        if($name&&$comment)
        {
        $query=mysql_query("INSERT INTO comment (id,name,comment) VALUES ('','$name','$comment')");
        }
        else
        {
            echo "Please fill out all the fields.";
        }
    }
    ?>
    !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
    <head>
    <title>Comment Box</title>
    </head>
    
    <body>
    <form action="index22.php" method="POST">
    <table>
    <tr><td>Name: </td><td><input type="text" name="name"></td></tr>
    <tr><td colspan="2">Comment: </td><td> 
    <tr><td colspan="2"><textarea name="comment"></textarea></td><td>
    <tr><td colspan="2"><input type="submit" name="submit" value="comment"/></td></tr>
    </table>
    
    </body>
    </html>
    And the connect.php to mysql code is:

    Code:
    <?php
    
    mysql_connect("cust-mysql-123-03,"****","****");
    mysql_select_db("fourwaysdpcouk_576458_db1");
    ?>

    What am I doing wrong? I have included my password in the actual file on my computer but not above!

    Thanks,

    James
    Last edited by Dormilich; Aug 10 '11, 09:27 AM. Reason: removed login credentials
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    What am I doing wrong?
    you don’t check, whether your database connection succeeds. your database host name looks strange.

    something along
    Code:
    $res = mysql_query($sql);
    if (!$res)
    {
        throw new RuntimeException("Database query failed.");
    }
    this is to be done for EVERY mysql_* function.

    Comment

    • jamesmoore
      New Member
      • Jan 2011
      • 47

      #3
      Ah thanks so where would i put that bit of code?
      I am using phpmyadmin and that is what it says now instead of localhost since i purchased a host.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        every where there is a mysql_* function

        Comment

        Working...