Jump to content

why can't insert data into the database table?


runeveryday

Recommended Posts

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>";

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

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.

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>";

$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 ?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.