Can't insert form data into mysql database
Results 1 to 11 of 11

Thread: Can't insert form data into mysql database

  1. #1
    Join Date
    Dec 2009
    Posts
    28

    Question Can't insert form data into mysql database

    There are two files, index.htm(form) and submit.php(code to insert form data into a database's table).

    The database is created manually using HeidiSQL with appropriate table and columns.

    But the problem is that after filling out the fields and clicking the submit button an empty screen appears and no data was inserted into the table of a database.

    How fix?

    index.htm code
    Code:
    <html>
    <head>
    <title>Form</title>
    <link rel="stylesheet" href="style.css" />
    </head>
    <body>
    <form name="form1" action="submit.php" action="post">
    <table>
    <tr>
    <th class="center" colspan="2">Form</th>
    </tr>
    <tr>
    <td class="right">Field1</td><td><input type="text" name="field1" /></td>
    </tr>
    <tr>
    <td class="right">Field2</td><td><input type="text" name="field2" /></td>
    </tr>
    <tr>
    <td class="center" colspan="2"><input type="submit" value="Add" /></td>
    </tr>
    </table>
    </form>
    </body>
    </html>
    submit.php code
    Code:
    <?php
    //Connect to mysql
    $connect = mysql_connect("localhost","root","");
    
    //If unable to connect, give me an explanation
    if (!$connect)
    {
    die("Couldn't connect: " . mysql_error());
    }
    
    //If all is jolly, select the database using the current connection
    mysql_select_db("db", $connect);
    
    //Insert data into the table using input from the form page's fields
    $sql = "INSERT INTO items (field1,field2) VALUES ('$_POST[field1]','$_POST[field2]')";
    
    //If all fails, give me an explanation
    if (!mysql_query($sql, $connect)
    {
    die('Error: ' . mysql_error());
    }
    
    //Otherwise congratulate me
    echo "1 record added";
    
    //Close connection
    mysql_close($connect);
    ?>

  2. #2
    Join Date
    Apr 2000
    Location
    Sheboygan, WI
    Posts
    53,391
    Opps sorry

  3. #3
    JPnyc is offline Virtual PC Specialist!!!
    Join Date
    Jan 2005
    Posts
    7,877
    Are you sure the database name and path are correct? The database is literally at the root of your C drive? And it's named db?
    There is nothing to fear, but life itself.

  4. #4
    Join Date
    Dec 2009
    Posts
    28
    Quote Originally Posted by JPnyc View Post
    Are you sure the database name and path are correct? The database is literally at the root of your C drive? And it's named db?
    Database name yes, but I don't see where is the path indicator.
    I'm using wamp server installed at C:\wamp\

  5. #5
    JPnyc is offline Virtual PC Specialist!!!
    Join Date
    Jan 2005
    Posts
    7,877
    $connect = mysql_connect("localhost","root",""); is telling the program to look for the database at the very root of your file directory, which you're telling me is not where it's located. That should be changed to reflect the actual path C:\wamp\
    There is nothing to fear, but life itself.

  6. #6
    Join Date
    Dec 2009
    Posts
    28
    Quote Originally Posted by JPnyc View Post
    $connect = mysql_connect("localhost","root",""); is telling the program to look for the database at the very root of your file directory, which you're telling me is not where it's located. That should be changed to reflect the actual path C:\wamp\
    Okay. How would I do that?

    Tried to do it like that...

    Code:
    $connect = mysql_connect("C:\wamp\","root","");
    ... and didn't work.

  7. #7
    JPnyc is offline Virtual PC Specialist!!!
    Join Date
    Jan 2005
    Posts
    7,877
    $connect = mysql_connect("localhost","C:\wamp\","");
    There is nothing to fear, but life itself.

  8. #8
    Join Date
    Dec 2009
    Posts
    28
    Code:
    $connect = mysql_connect("localhost","C:\wamp\","");
    Still doesn't add anything to the database and shows a blank page after submitting the form.

  9. #9
    Join Date
    Feb 2000
    Location
    Fullerton, CA, USA
    Posts
    726
    Quote Originally Posted by neshtak View Post
    Still...shows a blank page after submitting the form.
    There are no html commands/structure being sent back to the browser from submit.php.

    Format your VALUES like this: '{$_POST[field1]}','{$_POST[field2]}'

  10. #10
    Join Date
    Dec 2009
    Posts
    28
    Quote Originally Posted by prouton View Post
    There are no html commands/structure being sent back to the browser from submit.php.

    Format your VALUES like this: '{$_POST[field1]}','{$_POST[field2]}'
    Tried it, but no luck.

    Here's are the latest codes:

    index.htm
    Code:
    <html>
    <head>
    <title>Welcome</title>
    <link rel="stylesheet" href="style.css" />
    </head>
    <body>
    <form name="form1" method="post" action="submit.php">
    Name<input type="text" name="name" /><br />
    Age<input type="text" name="age" /><br />
    <input type="submit" value="Add" />
    </form>
    </body>
    </html>
    submit.php
    Code:
    <?php
    print_r($_POST);
    
    $connect = mysql_connect("localhost","C:\wamp\","");
    
    if (!$connect)
    {
    die("Couldnt connect" . mysql_error());
    }
    
    mysql_select_db("db", $connect);
    
    $sql = "INSERT INTO table (name, password) VALUES ('{$_POST[name]}','{$_POST[age]}')";
    
    echo $sql;
    
    if (!mysql_query($sql, $connect))
    {
    die("Error: " . mysql_error());
    }
    
    echo "1 record added";
    
    mysql_close($connect);
    ?>

  11. #11
    Join Date
    Feb 2000
    Location
    Fullerton, CA, USA
    Posts
    726
    First, you still haven't put any html commands into submit.php. What are you expecting the browser to do when it receives simple text? Add the html, head and body tags appropriately.

    Second, you're passing the wrong values to the connect function. The three values are location ('localhost', since it's running on the same server as the php program), username (which in your original example was 'root'), and password. What user/password did you define when you created the database?

    So if your database is named 'db' and the fields in table 'table' are named 'name' and 'password', then you should be good to go.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •