Page 1 of 1

Could not write data due to an error in your SQL syntax

Posted: Mon Oct 31, 2011 1:08 pm
by Tokunbo
hi guys,

this question is in relation to an earlier question about an "undefined index error message". Here is the link to the first question:
http://www.devnetwork.net/viewtopic.php ... 2ab8768cfe

This is my original code:

Code: Select all

<?php

        if (count($_POST)>0 && isset($_POST['sname']) && isset($_POST['fname'])) {

                // FORM WAS SUBMITTED

                $SQL  = 'INSERT INTO `Contracts` (`Surname`,`Firstname`) VALUES (';
                $SQL .= '"'.mysql_real_escape_string($_POST['sname']).'",';
                $SQL .= '"'.mysql_real_escape_string($_POST['fname']).'")';
               
                mysql_query($SQL)
                        or die ('ERR: Could not write data. '.mysql_error());

        }
so when I try to add a new field, etc like this:

Code: Select all


        if (count($_POST)>0 && isset($_POST['sname']) && isset($_POST['fname']) && isset($_POST['nname'])) {

                // FORM WAS SUBMITTED

                $SQL  = 'INSERT INTO `Contracts` (`Surname`,`Firstname`, `Nickname`) VALUES (';
                $SQL .= '"'.mysql_real_escape_string($_POST['sname']).'",';
                $SQL .= '"'.mysql_real_escape_string($_POST['fname']).'")';
                $SQL .= '"'.mysql_real_escape_string($_POST['nname']).'")';
               
                mysql_query($SQL)
                        or die ('ERR: Could not write data. '.mysql_error());

        }
I get the error:
ERR: Could not write data. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"sss")' at line 1
where the above "sss" is the field from my form with "nname". Please note, in my table, the field Nickname is a VARCHAR just like sname and fname.

2) Why is it that on some tutorials online, sometimes, this:

Code: Select all

INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
could be written like this:

Code: Select all

INSERT INTO 'table_name' ('column1', 'column2', 'column3')
VALUES ('value1', 'value2', 'value3')
is there really any difference between the two?

Re: Could not write data due to an error in your SQL syntax

Posted: Mon Oct 31, 2011 2:03 pm
by mikeashfield
Yes, the tutorials say '...' for you to insert more/other rows/values if the need be. My sql cannot find a table named ... so gives up. :)

Re: Could not write data due to an error in your SQL syntax

Posted: Mon Oct 31, 2011 2:47 pm
by Celauran
Have you tried echoing the query to see if that sheds any light on where the error might be?

Re: Could not write data due to an error in your SQL syntax

Posted: Tue Nov 01, 2011 8:14 am
by Tokunbo
@mikeashefield
which tutorial are you referring to?

@Celauran
Yes Ive tried reechoing the third variable. Its like the problem is with the third variable., or if a third var-check isnt allowed with the command Im using...

Re: Could not write data due to an error in your SQL syntax

Posted: Tue Nov 01, 2011 9:16 am
by icesolid
Looks like you have a ) instead of a ,

Simple syntax error it appears.

Maybe it should look like this:

Code: Select all

$SQL  = 'INSERT INTO `Contracts` (`Surname`,`Firstname`, `Nickname`) VALUES (';
$SQL .= '"'.mysql_real_escape_string($_POST['sname']).'",';
$SQL .= '"'.mysql_real_escape_string($_POST['fname']).'",';
$SQL .= '"'.mysql_real_escape_string($_POST['nname']).'")';

Re: Could not write data due to an error in your SQL syntax

Posted: Tue Nov 01, 2011 9:42 am
by Tokunbo
@icesolid

oops, my bad, I think I missed that.

thanks.

My code is ok now.