Jump to content

[SOLVED] SQL syntax error with PHP variables


ShootingBlanks

Recommended Posts

I'm getting the following error:

 

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 'div, writer, sme, progress, status, due) VALUES (Test Project,test description,t' at line 1

 

Based on this code:

$insertSQL = sprintf("INSERT INTO projects (proj_name, proj_desc, sponsor, priority, div, writer, sme, progress, status, due)
VALUES (".$_POST['proj_name'].",".$_POST['desc'].",".$_POST['sponsor'].",".$_POST['priority'].",".$_POST['div'].","
.$_POST['writer'].",".$_POST['sme'].",".$progress.",'Open',".$mysqlFormat.'"');

 

Regarding that code, this is the values of the variable noted in there:

 

$progress = "Project opened."

 

$mysqlFormat = 2007-12-27

 

The $mysqlFormat is being put into the "due" column that is listed as a "DATE" type in my table.  If you need other info to help troubleshoot, please let me know.  Thanks!...

 

Link to comment
Share on other sites

 

Okay - I kept all the code the same, except I renamed "div" to "division" (in both the code and my database table), and now I am getting THIS error:

 

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 'project,test description,test sponsor,Medium,ALL,,,Project opened.,'Open',2007-1' at line 1

 

The "writer" and "sme" fields were left blank in my form (so the $_POST variables), which is why there are just blank commas there after "ALL" in the error above, but in my database table setup, I have them set to "NULL", so if they're empty, then doesn't that mean that it is okay to have nothing in them?  Or am I wrong about that?

 

Now what?  Thanks for that tip on "div", by the way!!!

 

 

Link to comment
Share on other sites

you must add sigle qoute around string type data

Try

$insertSQL = sprintf("INSERT INTO projects (proj_name, proj_desc, sponsor, priority, division, writer, sme, progress, status, due)
VALUES ('".$_POST['proj_name']."', '".$_POST['desc']."', '".$_POST['sponsor']."', '".$_POST['priority']."', '".$_POST['div']."', '"
.$_POST['writer']."', '".$_POST['sme']."', '".$progress."', 'Open', '".$mysqlFormat."'");

Link to comment
Share on other sites

why not just try  ;D

 

$insertSQL = "INSERT INTO projects (proj_name, proj_desc, sponsor, priority, div, writer, sme, progress, status, due) VALUES ('{$_POST['proj_name']}','{$_POST['desc']}','{$_POST['sponsor']}','{$_POST['priority']}','{$_POST['div']}','{$_POST['writer']}','{$_POST['sme']}','$progress','Open','$mysqlFormat')";

Link to comment
Share on other sites

String values in a MySQL statement need to be quoted with single quotes. You should also process any user input through, at least, the mysql_real_escape_string() function:

<?php
$tmpq = array();
foreach ($_POST as $k => $v)
    switch ($k) {
        case 'proj_name':
        case 'desc':
        case 'sponsor':
        case 'priority':
        case 'div':
        case 'writer':
        case 'sme':
           $tmpq[] = "'" . mysql_real_escape_string($v) . "'";
    }
$insertSQL = "INSERT INTO projects (proj_name, proj_desc, sponsor, priority, division, writer, sme, progress, status, due)
VALUES (" . impode(', '. $tmpq) .  ",'" . $progress . "', 'Open', '".$mysqlFormat . "')";
?>

 

Ken

Link to comment
Share on other sites

you must add sigle qoute around string type data

Try

$insertSQL = sprintf("INSERT INTO projects (proj_name, proj_desc, sponsor, priority, division, writer, sme, progress, status, due)
VALUES ('".$_POST['proj_name']."', '".$_POST['desc']."', '".$_POST['sponsor']."', '".$_POST['priority']."', '".$_POST['div']."', '"
.$_POST['writer']."', '".$_POST['sme']."', '".$progress."', 'Open', '".$mysqlFormat."'");

 

Getting closer!...

 

...after adding the single quotes around $progress, I get the following error:

 

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 'project,test description - empty,test sponsor,Medium,W&C,,,'Project opened.','Op' at line 1

 

If I fill in those "NULL" fields ("writer" and "sme"), then I get this error:

 

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 'project,test description - filled in,test sponsor,Medium,W&C,sample writer,sampl' at line 1

 

Any more ideas???

???

Link to comment
Share on other sites

String values in a MySQL statement need to be quoted with single quotes. You should also process any user input through, at least, the mysql_real_escape_string() function:

<?php
$tmpq = array();
foreach ($_POST as $k => $v)
    switch ($k) {
        case 'proj_name':
        case 'desc':
        case 'sponsor':
        case 'priority':
        case 'div':
        case 'writer':
        case 'sme':
           $tmpq[] = "'" . mysql_real_escape_string($v) . "'";
    }
$insertSQL = "INSERT INTO projects (proj_name, proj_desc, sponsor, priority, division, writer, sme, progress, status, due)
VALUES (" . impode(', '. $tmpq) .  ",'" . $progress . "', 'Open', '".$mysqlFormat . "')";
?>

 

Ken

 

I copied that code exactly above (except I changed "impode" to "implode", assuming a spelling error), and now I get the following errors:

 

Warning: implode() [function.implode]: Argument to implode must be an array. in C:\htdocs\_PHP-SITES\ProjectBoard\admin\addProject.php on line 161

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 ''Project opened.', 'Open', '2007-12-27')' at line 2

 

Is this a step forward or backwards?

 

 

Link to comment
Share on other sites

On the line where you do the Mysql statement add an "or die" clause and echo your query:

<?php
$rs = mysql_query($insertSQL) or die("Error with query: $insertSQL<br>" . mysql_error();
?>

 

This should give you a better understanding of what's going on.

 

Ken

Link to comment
Share on other sites

On the line where you do the Mysql statement add an "or die" clause and echo your query:

<?php
$rs = mysql_query($insertSQL) or die("Error with query: $insertSQL<br>" . mysql_error();
?>

 

This should give you a better understanding of what's going on.

 

Ken

 

Pardon my newbie ignorance, but what do I echo to get the output of the query?  is it just:

echo '<p>'.$insertSQL.</p>;

in the body of the HTML document?  if so, wouldn't I still get the error page because the query will try to execute, so then I wouldn't ever see the echo?

 

Link to comment
Share on other sites

Change it to:

<?php
$Result1 = mysql_query($insertSQL, $ProjectBoard) or die("Error with query: $insertSQL<br>" . mysql_error());
?>

 

And your formatted query will display before the error message.

Okay, cool.  Here's the current error (and I did correct that "implode" typo):

 

Warning: implode() [function.implode]: Argument to implode must be an array. in C:\htdocs\_PHP-SITES\ProjectBoard\admin\addProject.php on line 161

Error with query: INSERT INTO projects (proj_name, proj_desc, sponsor, priority, division, writer, sme, progress, status, due) VALUES (,'Project opened.', 'Open', '2007-12-27')

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 ''Project opened.', 'Open', '2007-12-27')' at line 2

Link to comment
Share on other sites

Here you go.  Thanks so much again for all the help - it is MUCH appreciated!!!  ;D

 

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $tmpq = array();
foreach ($_POST as $k => $v)
    switch ($k) {
        case 'proj_name':
        case 'desc':
        case 'sponsor':
        case 'priority':
        case 'div':
        case 'writer':
        case 'sme':
           $tmpq[] = "'" . mysql_real_escape_string($v) . "'";
    }
$insertSQL = "INSERT INTO projects (proj_name, proj_desc, sponsor, priority, division, writer, sme, progress, status, due)
VALUES (" . implode(', '. $tmpq) .  ",'" . $progress . "', 'Open', '".$mysqlFormat . "')";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.