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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Tokunbo
Forum Commoner
Posts: 46
Joined: Thu Sep 29, 2011 8:53 am

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

Post 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?
mikeashfield
Forum Contributor
Posts: 159
Joined: Sat Oct 22, 2011 10:50 am

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

Post 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. :)
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post by Celauran »

Have you tried echoing the query to see if that sheds any light on where the error might be?
Tokunbo
Forum Commoner
Posts: 46
Joined: Thu Sep 29, 2011 8:53 am

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

Post 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...
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

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

Post 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']).'")';
Tokunbo
Forum Commoner
Posts: 46
Joined: Thu Sep 29, 2011 8:53 am

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

Post by Tokunbo »

@icesolid

oops, my bad, I think I missed that.

thanks.

My code is ok now.
Post Reply