View Full Version : why can't insert data into the database table?

06-24-2010, 10:05 AM
the html file

<form method="post" action="add.php"><input type="hidden" name="id" value="NULL" /><table><tr height="20"><td colspan="2"><font size="+0" face="verdana">Below is a sample form for our php</td></tr><tr height="50"><td></td></tr><tr><td align="left"><font size="+0" face="verdana"><b>Your name<br />Your Email Address</b></td><td><input type="text" name="name"><br /><input type="text" name="email" /></td> </tr><tr><td colspan="2"><center><select name="opinion"><option value="is greate">I like your site</option><option value="is OK">you site is ok</option><option value="is horrible">your site is horrible</option></select><input type="submit" value="tell us"!/></td></tr></table></form>

add.php file

$DBhost ="localhost"; //mysql-server
$DBuser = "root"; //mysqluser
$DBpass = "123";
$DBName = "learnphp";
$table = "information";
mysql_connect($DBhost,$DBuser,$DBpass) or die("error");
mysql_select_db("$DBName") or die("error");
$sqlquery = "INSERT INTO $table (id,name,email,opinion)VALUES ('NULL','$_POST[name]','$_POST[email]','$_POST[opinion]')";
echo $_POST['id']. "<br />";
echo $_POST['name']. "<br />";
echo $_POST['email']. "<br />";
echo $_POST['opinion']. "<br />";

06-24-2010, 11:00 AM
Not going to code it out for you, but here's some tips for you ;)

- in the databasequery:

$sqlquery = "INSERT INTO $table (id,name,email,opinion)VALUES ('NULL','$_POST[name]','$_POST[email]','$_POST[opinion]')";

This isnt going to work.

Try putting a space between de column naming and ' VALUES', further the inputted values ($_POST[name]) isnt going to work. $_POST['name'] is the correct one. If you simply assign some variables to the posts, it will be easyer to insert those in a query.

There might be more, but this is what I got after 1 minute of looking. :)

06-24-2010, 11:02 AM
Try this

HTML Code (Few tags r not closed and I changed this <input type="submit" value="tell us"! /> ; Even without making any changes it worked for me. )

<form method="post" action="add.php">
<input type="hidden" name="id" value="NULL" />
<tr height="20">
<td colspan="2"><font size="+0" face="verdana">Below is a sample form for our php</font></td>
<tr height="50">
<td align="left"><font size="+0" face="verdana"><b>Your name</font><br />
Your Email Address</b></td>
<td><input type="text" name="name">
<br />
<input type="text" name="email" /></td>
<td colspan="2"><center>
<select name="opinion">
<option value="is greate">I like your site</option>
<option value="is OK">you site is ok</option>
<option value="is horrible">your site is horrible</option>
<input type="submit" value="tell us!" /></td>

I used the same PHP Code above..

It worked for me... Hope this helps for you too..

If it wont work for you, please tell the error you are getting.. may be some problem with your database connection or so..