0

I have two scripts which insert a new line into my SQL database. One works, and the other doesn't. They both look the same (but for the different data) but one doesn't work. I can not figure out what I am doing wrong.

Disclosure: I am self taught and have not taken any programming courses except for a pascal class several years ago.

This one works: include('db.php');

mysql_query("INSERT INTO leadprofile (first_name, last_name, home_phone, mobile_phone, work_phone, work_phone_ext, email_address, description, fk_source) VALUES ('$_POST[first_name]','$_POST[last_name]','$_POST[home_phone]','$_POST[mobile_phone]','$_POST[work_phone]','$_POST[work_phone_ext]','$_POST[email_address]','$_POST[description]','$_POST[fk_source]')");

mysql_close($con);

This one does not work. The page loads, but no values are inserted into the table:

include('db.php');

mysql_query("INSERT INTO tasks (fk_lead_id, task_type, task_detail, due_date) VALUES ('$_POST[fk_lead_id]',('$_POST[task_type]','$_POST[task_detail]','$_POST[due_date]')");

mysql_close($con);

Both are referencing the same db.php file which loads the database only. I appreciate any help and hope it is something crazy simple. I have confirmed the table name and column names are all correct.

4
  • 2
    And any errors that you're not trapping; unescaped strings (what if my surname is O'Connor?); SQL injection risks? Commented Dec 4, 2013 at 19:30
  • Do not use mysql_ functions, they are deprecated. Use mysqli_ or PDO instead. Also NEVER pass any kind of input directly into a SQL query, look up SQL injections asap. Commented Dec 4, 2013 at 19:31
  • Where are all these awful mysql tutorials that make no mention of mysql_error()? "I am self taught" - Forget everything you know and start over phptherightway.com/#databases Commented Dec 4, 2013 at 20:14
  • Thank you all for your help. Mike B I will go to study phptherightway.com. Commented Dec 5, 2013 at 0:19

1 Answer 1

1

Theres a bracket too much

INSERT INTO tasks (fk_lead_id, task_type, task_detail, due_date)
    VALUES ('$_POST[fk_lead_id]',('$_POST[task_type]','$_POST[task_detail]','$_POST[due_date]')
                                 ^here

You would have easily found out if you had used mysql_error() e.g. like the following:

mysql_query("...") or die(mysql_error());
Sign up to request clarification or add additional context in comments.

1 Comment

I ran the or die(mysql_error()); and realized in addition to the extra bracket I had a space in the column name in the table itself on the database. Thanks for pointing out what or die(mysql_error()); does. Case closed and this is working now.

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.