Page 1 of 1

why can't insert data into the database table?

Posted: Thu Jun 24, 2010 1:11 am
by everydayrun
the html file

Code: Select all


<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

Code: Select all


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

Re: why can't insert data into the database table?

Posted: Thu Jun 24, 2010 4:26 am
by internet-solution
Have you checked mySQl error? Try this

Code: Select all

$results=mysql_query($sqlquery) or die(mysql_error());
There are some issues - your $_POST['id'] is set to "NULL" as a string (not NULL value). So there might be data type mismatch error if your MySQL id field is of INT.

If id field is supposed to be a primary key in your table then set this to auto_increment in the table strcuture and drop this field from your SQL statement (and also from your HTML form). MySQL server will add this to the row.

Code: Select all

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

Re: why can't insert data into the database table?

Posted: Fri Jun 25, 2010 1:02 am
by everydayrun
internet-solution wrote:Have you checked mySQl error? Try this

Code: Select all

$results=mysql_query($sqlquery) or die(mysql_error());
There are some issues - your $_POST['id'] is set to "NULL" as a string (not NULL value). So there might be data type data type mismatch error if your MySQL id field is of INT.

If id field is supposed to be a primary key in your table then set this to auto_increment in the table strcuture and drop this field from your SQL statement (and also from your HTML form). MySQL server will add this to the row.

Code: Select all

$sqlquery = "INSERT INTO `$table` (`name`,`email`,`opinion`)VALUES ('$_POST[name]','$_POST[email]','$_POST[opinion]')";
yeah,you're right.this is the code that i created my table.
mysql> CREATE TABLE information (
> id INT NOT NULL AUTO_INCREMENT,
> name VARCHAR (40),
> email VARCHAR (40),
> opinion VARCHAR (30),
> PRIMARY KEY (id)
);
how to change my code and make it can insert the data into the table.thank you.

Re: why can't insert data into the database table?

Posted: Fri Jun 25, 2010 2:32 am
by internet-solution
In my previous post, I have already given you the sql query required.