简体   繁体   中英

Mysql Query Displays an error in SQL syntax when it's right

I got sick of this error. I'm pretty sure it works with '$_POST[name]' but sql doesn't accept it.

It gives me the error saying: 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 ')' at line 2

$syn = mysql_real_escape_string($_POST['syn']);
$fore = mysql_real_escape_string($_POST['fore']);
 $localfore = mysql_real_escape_string($_POST['localfore']);
$save = mysql_query("INSERT INTO tblforecast (Issued,Valid,Synopsis,Forecast,Local_Forecast,Station11,Station12,Station13,Station14,Station15,Station16,Station17,Station18,Station19,Forecaster)
                           VALUES (now(),'24','$syn','$fore','$localfore','sample','$sample','sample','sample','sample','sample','sample','sample','sample',$id)");

What's going on?

PS. Line 2 points at the start of VALUES

This is very bad practice to save post data directly. Instead you can use $syn = mysql_real_escape_string($_POST['syn']); and $syn put into your sql query.

Try this

$sql = "INSERT INTO tblforecast (Issued,Valid,Synopsis,Forecast,Local_Forecast,Station11,Station12,Station13,Station14,Station15,Station16,Station17,Station18,Station19,Forecaster)
                                   VALUES ('{$datetime}',24,'{$_POST[syn]}','{$_POST[fore]}','{$_POST[localfore]}','sample','sample','sample','sample','sample','sample','sample','sample','sample',$id)";
echo sql;

$save = mysql_query($sql);

If you write the query in a more eadable way, you can spot some mistakes:

INSERT INTO
    tblforecast
(
    Issued
    ,Valid
    ,Synopsis
    ,Forecast
    ,Local_Forecast
    ,Station11
    ,Station12
    ,Station13
    ,Station14
    ,Station15
    ,Station16
    ,Station17
    ,Station18
    ,Station19
    ,Forecaster
) VALUES (
    '$datetime'
    ,24
    ,'$_POST[syn]'
    ,'$_POST[fore]'
    ,'$_POST[localfore]'
    ,'sample'
    ,'$sample]' // <-- is that supposed to be there?
    ,'sample'
    ,'sample'
    ,'sample'
    ,'sample'
    ,'sample'
    ,'sample'
    ,'sample'
    ,$id // <-- where are the closing brackets?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM