Author Topic: [SOLVED] Can't get INSERT query to work. Can't see where SYNTAX error is.  (Read 294 times)

0 Members and 1 Guest are viewing this topic.

Offline vapokerproTopic starter

  • Irregular
  • Posts: 2
    • View Profile
Here is my code.  It keeps giving me an error that says there is something wrong with my syntax but I've poured over it and can't figure it out so I need some fresh eyes to tell me what I am missing.  Thanks in advance.
Code: [Select]
<?php
mysql_connect 
('localhost','<user>',',<pass>') or die ("Could not connect to MySQL " mysql_error());
mysql_select_db ('preemployments') or die ("Could not select Database " mysql_error());
$sql "INSERT INTO preemployments (create_date, to, emsi_office, fax, from, supplies, tracking, donor_first, donor_last, donor_ssn, test_date, test_time, dpc, dpc_phone, facility_no, mro, test_reason, drug_test, oa, dot_type) VALUES ('$_POST[create_date]', '$_POST[to]', '$_POST[emsi_office]', '$_POST[fax]', '$_POST[from]', '$_POST[supplies]', '$_POST[tracking]','$_POST[donor_first]','$_POST[donor_last]', '$_POST[donor_ssn]', '$_POST[test_date]', '$_POST[test_time]', '$_POST[dpc]', '$_POST[dpc_phone]', '$_POST[facility_no]', '$_POST[mro]', '$_POST[test_reason]', '$_POST[drug_test]', '$_POST[oa]', '$_POST[dot_type]')";
mysql_query ($sql) or die ("Error inserting records in to Table " mysql_error());
?>


Online kickstart

  • Guru
  • Addict
  • *
  • Posts: 2,680
    • View Profile
Re: Can't get INSERT query to work. Can't see where SYNTAX error is.
« Reply #1 on: September 18, 2009, 03:47:46 PM »
Hi

Personally I would guess that one of the fields you are inserting contains a quote.

Try this:-


<?php
mysql_connect 
('localhost','<user>',',<pass>') or die ("Could not connect to MySQL " mysql_error());
mysql_select_db ('preemployments') or die ("Could not select Database " mysql_error());
$sql "INSERT INTO preemployments 
(create_date, to, emsi_office, fax, from, supplies, tracking, donor_first, donor_last, donor_ssn, test_date, test_time, dpc, dpc_phone, facility_no, mro, test_reason, drug_test, oa, dot_type) VALUES ('"
.mysql_real_escape_string($_POST['create_date'])."', '".mysql_real_escape_string($_POST['to'])."', '".mysql_real_escape_string($_POST['emsi_office'])."', '".mysql_real_escape_string($_POST['fax'])."', '".mysql_real_escape_string($_POST['from'])."', '".mysql_real_escape_string($_POST['supplies'])."', '".mysql_real_escape_string($_POST['tracking'])."','".mysql_real_escape_string($_POST['donor_first'])."','".mysql_real_escape_string($_POST['donor_last'])."', '".mysql_real_escape_string($_POST['donor_ssn'])."', '".mysql_real_escape_string($_POST['test_date'])."', '".mysql_real_escape_string($_POST['test_time'])."', '".mysql_real_escape_string($_POST['dpc'])."', '".mysql_real_escape_string($_POST['dpc_phone'])."', '".mysql_real_escape_string($_POST['facility_no'])."', '".mysql_real_escape_string($_POST['mro'])."', '".mysql_real_escape_string($_POST['test_reason'])."', '".mysql_real_escape_string($_POST['drug_test'])."', '".mysql_real_escape_string($_POST['oa'])."', '".mysql_real_escape_string($_POST['dot_type'])."')";
mysql_query ($sql) or die ("Error inserting records in to Table $sql " mysql_error());
?>


All the best

Keith
There are 10 types of people in the world. Those who understand binary and those who don't

Offline PFMaBiSmAd

  • Guru
  • 'Insane!'
  • *
  • Posts: 14,587
  • In Coding, Automatic means you write code to do it
    • View Profile
Re: Can't get INSERT query to work. Can't see where SYNTAX error is.
« Reply #2 on: September 18, 2009, 03:56:41 PM »
And at least two of your field names are reserved keywords and either need to be changed to something else or enclosed in back-ticks.
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

Offline vapokerproTopic starter

  • Irregular
  • Posts: 2
    • View Profile
Re: Can't get INSERT query to work. Can't see where SYNTAX error is.
« Reply #3 on: September 18, 2009, 04:09:17 PM »
Thank you!!!!  I changed the field names in MySQL and on the corresponding pages and it works now.