why can't insert data into the database table?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
everydayrun
Forum Commoner
Posts: 51
Joined: Wed Jan 20, 2010 1:30 am

why can't insert data into the database table?

Post 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>";
internet-solution
Forum Contributor
Posts: 220
Joined: Thu May 27, 2010 6:27 am
Location: UK

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

Post 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]')";
Last edited by internet-solution on Fri Jun 25, 2010 2:30 am, edited 1 time in total.
everydayrun
Forum Commoner
Posts: 51
Joined: Wed Jan 20, 2010 1:30 am

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

Post 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.
internet-solution
Forum Contributor
Posts: 220
Joined: Thu May 27, 2010 6:27 am
Location: UK

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

Post by internet-solution »

In my previous post, I have already given you the sql query required.
Post Reply