runeveryday Posted June 24, 2010 Share Posted June 24, 2010 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> the 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 VALUES ('$_POST[id]','$_POST[name]','$_POST[email]','$_POST[opinion]')"; $results=mysql_query($sqlquery); mysql_close(); echo "<html><titile>PHP and mysql</title><body><p><center>you just enter this information into the database <p><blockquote>"; print "Name:$_POST[name]<p>E-mail:$_POST[email]<p>opinion:$_POST[opinion]</blockquote></center>"; Quote Link to comment https://forums.phpfreaks.com/topic/205710-why-cant-insert-data-into-the-database-table/ Share on other sites More sharing options...
inversesoft123 Posted June 24, 2010 Share Posted June 24, 2010 Assign proper values while inserting into the database $sqlquery = "INSERT INTO $table (postid, postname, email, opinion) VALUES ('$_POST[id]','$_POST[name]','$_POST[email]','$_POST[opinion]')"; $results=mysql_query($sqlquery); Replace (postid, postname, email, opinion) with proper field names which are in table $table Quote Link to comment https://forums.phpfreaks.com/topic/205710-why-cant-insert-data-into-the-database-table/#findComment-1076391 Share on other sites More sharing options...
runeveryday Posted June 24, 2010 Author Share Posted June 24, 2010 Assign proper values while inserting into the database $sqlquery = "INSERT INTO $table (postid, postname, email, opinion) VALUES ('$_POST[id]','$_POST[name]','$_POST[email]','$_POST[opinion]')"; $results=mysql_query($sqlquery); Replace (postid, postname, email, opinion) with proper field names which are in table $table i have replaced (id, name, email, opinion),but there is also no data in the database table. Quote Link to comment https://forums.phpfreaks.com/topic/205710-why-cant-insert-data-into-the-database-table/#findComment-1076460 Share on other sites More sharing options...
pengu Posted June 24, 2010 Share Posted June 24, 2010 Do some basic trouble shooting, see if there is any data in the variables. With $_GET & $_POST I've always done them with ' & '. So for example $_POST['hello']. But I don't think this matters.. Try the following code to see if anything is being printed. $DBhost ="localhost"; //mysql-server $DBuser = "root"; //mysqluser $DBpass = "123"; $DBName = "learnphp"; $table = "information"; echo $_POST['id']. "<br />"; echo $_POST['name']. "<br />"; echo $_POST['email']. "<br />"; echo $_POST['opinion']. "<br />"; //mysql_connect($DBhost,$DBuser,$DBpass) or die("error"); //mysql_select_db("$DBName") or die("error"); //$sqlquery = "INSERT INTO $table VALUES ('$_POST[id]','$_POST[name]','$_POST[email]','$_POST[opinion]')"; //$results=mysql_query($sqlquery); //mysql_close(); //echo "<html><titile>PHP and mysql</title><body><p><center>you just enter this information into the database //<p><blockquote>"; //print "Name:$_POST[name]<p>E-mail:$_POST[email]<p>opinion:$_POST[opinion]</blockquote></center>"; Quote Link to comment https://forums.phpfreaks.com/topic/205710-why-cant-insert-data-into-the-database-table/#findComment-1076464 Share on other sites More sharing options...
runeveryday Posted June 24, 2010 Author Share Posted June 24, 2010 there is no any output,why? Quote Link to comment https://forums.phpfreaks.com/topic/205710-why-cant-insert-data-into-the-database-table/#findComment-1076472 Share on other sites More sharing options...
wee493 Posted June 24, 2010 Share Posted June 24, 2010 there is no any output,why? This is because the variables are not being posted to your script. Are u meaning to use $_GET ? Quote Link to comment https://forums.phpfreaks.com/topic/205710-why-cant-insert-data-into-the-database-table/#findComment-1076475 Share on other sites More sharing options...
runeveryday Posted June 24, 2010 Author Share Posted June 24, 2010 no,i used POST to deliever the variables. Quote Link to comment https://forums.phpfreaks.com/topic/205710-why-cant-insert-data-into-the-database-table/#findComment-1076477 Share on other sites More sharing options...
runeveryday Posted June 24, 2010 Author Share Posted June 24, 2010 oh,damm it! now,it can out put the result that i inputed. but how to insert the data to the table ? Quote Link to comment https://forums.phpfreaks.com/topic/205710-why-cant-insert-data-into-the-database-table/#findComment-1076478 Share on other sites More sharing options...
runeveryday Posted June 24, 2010 Author Share Posted June 24, 2010 $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 ('$_POST[id]','$_POST[name]','$_POST[email]','$_POST[opinion]')"; $results=mysql_query($sqlquery); mysql_close(); echo $_POST['id']. "<br />"; echo $_POST['name']. "<br />"; echo $_POST['email']. "<br />"; echo $_POST['opinion']. "<br />"; anyone helps,why this code can't insert the data to the table ? Quote Link to comment https://forums.phpfreaks.com/topic/205710-why-cant-insert-data-into-the-database-table/#findComment-1076490 Share on other sites More sharing options...
PFMaBiSmAd Posted June 24, 2010 Share Posted June 24, 2010 echo mysql_error(); on the next line after your mysql_query() statement to find out if the query is failing and why. Quote Link to comment https://forums.phpfreaks.com/topic/205710-why-cant-insert-data-into-the-database-table/#findComment-1076758 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.