Jump to content

Archived

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

shmickvl

HELP!! - mysql database/php help

Recommended Posts

Hi guys

im having some trouble.
i create a table in phpmyadmin, with two fields for example: id and name. i set my id field to auto increment, not null, and the primary key. i have made a "index.html" form to submit data, and made my "submission.php" page which takes it all there. The problem is that in my table, if i have the id field, data will not go to the database. However, if i delete the id field, it will go there.

PLease help

attached are my codes.

<html>
<body>
<form action="submission.php" method="post">
First Name: <input type="text" name="name"><br>
<input type="Submit">
</form>
</body>
</html>


<?
$name=$_POST['name'];
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("example") or die(mysql_error());
mysql_query("INSERT INTO `example1` VALUES ('$name')");
Print "Your information has been successfully added to the database.";
?>


cheers for your help

Share this post


Link to post
Share on other sites
When you use this syntax
[code]
mysql_query("INSERT INTO `example1` VALUES ('$name')");
[/code]

you must supply values for all the fields in the right order, even if one of them is auto-incremented, so if your id field is first in the table for example:

[code]
mysql_query("INSERT INTO `example1` VALUES (NULL, '$name')");
[/code]

the NULL will be replaced by the auto-increment value.

Or you could use the syntax
[code]
mysql_query("INSERT INTO `example1` (name) VALUES ('$name')");
[/code]

Share this post


Link to post
Share on other sites
As a rule, you should never rely on the column order, and always specify the column list for which you are providing values.

Share this post


Link to post
Share on other sites
thanks

it worked!!!!!!! yay!!! its been bugging me for hours. im learning mysql/php :)

thanks again guys. so simple!!!

Share this post


Link to post
Share on other sites

×

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.