1

So i have php code to insert form data in a table. Here's the code:

$link = @mysql_connect("***", "***", "****");
if (!$link) {
 echo "save_failed";
 return; 
}
mysql_select_db("***", $link);

$sql="INSERT INTO Conference (`First Name`, `Last Name`)
VALUES ('$_POST[fname]', '$_POST[lname]')";

mysql_close($link);

The *** are replaced with the actual values in the real code, obviously. But is there anything wrong with the above code? I tried to run it, it didn't have any errors with connection but it also didn't insert anything. Here's is what my mysql table looks like: Mysql table

Also, I need the table to have an auto incremented number so that each entry is unique with it's own index value. Any ideas on either problem? Thanks

4
  • What does your generated SQL look like? If you echo out $sql, does it work when you run it in the database? Commented Sep 17, 2012 at 16:16
  • 2
    Please stop writing new code with the ancient mysql_* functions. They are no longer maintained and community has begun the deprecation process. Instead you should learn about prepared statements and use either PDO or MySQLi. If you care to learn, here is a quite good PDO-related tutorial. Commented Sep 17, 2012 at 16:21
  • This example has a severe SQL injection bug that should be patched immediately. You should not ever insert $_POST or $_GET data directly into your query. Commented Sep 17, 2012 at 16:25
  • @DCoder is right - if you don't use PDO, you will get pwned. Commented Sep 17, 2012 at 16:27

1 Answer 1

6

You haven't executed the query, which should be done as it follows:

mysql_query($sql, $link);

Also, please consider using mysqli or even better PDO as the mysql package is deprecated (see the red box), i.e. mysql_query().

Sign up to request clarification or add additional context in comments.

4 Comments

wow i feel really stupid. Okay that worked perfectly, thank you. One more thing, is there any way to get that auto-incremented id number?
From what I see on your screenshot, you don't have an id column in your table. Also it would be a good practice to replace space with an underscore.
Yah I tried putting an ID column but there is no setting to make it auto increment.
Yes, there is. Have a good look in all the options to a field ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.