简体   繁体   English

从PHP插入查询时出现MySQL语法错误

[英]MySQL Syntax error on Insert Query from PHP

I'm getting a non-descriptive syntax error on a MYSQL query from PHP. 我在来自PHP的MYSQL查询中收到非描述性语法错误。 If I "echo" the text of the query and paste it into a MySQL query window, the code works. 如果我“回显”查询的文本并将其粘贴到MySQL查询窗口中,则该代码有效。 Here is the SQL for the query, the error code, and the error message... 这是查询的SQL,错误代码和错误消息...

INSERT INTO ADVERTISEMENTS (`user_id`, `ad_name`, `click_url`, `img_url`, `bg_color`, `start_date`, `end_date`, `timer_delay`, `add_date`) VALUES (2, 'Test New Ad', 'http://www.google.com', 'red_arrow.png', '#000000', '1980-05-11 00:00:00', '2020-05-01 00:00:00', 5, '2013-07-14 22:21:59'); 

Error Code: 1064 错误代码:1064
Error Msg: You have an error in your SQL syntax; 错误消息:您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 检查与您的MySQL服务器版本相对应的手册,以在第1行的''附近使用正确的语法

Here is the PHP code I am using... 这是我正在使用的PHP代码...

$link = mysqli_connect($UM_Settings["database_options"]["server_name"], $UM_Settings["database_options"]["username"], $UM_Settings["database_options"]["password"], $UM_Settings["database_options"]["database_name"]);

$advertisementNameNew = mysqli_real_escape_string($link, $_POST['advertisementNameNew']);
$destinationURLNew = mysqli_real_escape_string($link, $_POST['destinationURLNew']);
$dropboxUploadFile = mysqli_real_escape_string($link, $_POST['dropboxUploadFile']);
$backgroundColorNew = mysqli_real_escape_string($link, $_POST['backgroundColorNew']);
$bannerStartDateNew = DateStringToMySQL($_POST['bannerStartDateNew']);
$bannerEndDateNew = DateStringToMySQL($_POST['bannerEndDateNew']);
$bannerSetTimerNew = intval($_POST['bannerSetTimerNew']);
$tmpUserID = UM_GetCookie("UM_UserID");
$tmpAddDate = DateStringToMySQL('now');

echo "INSERT INTO ADVERTISEMENTS(`user_id`, `ad_name`, `click_url`, `img_url`, `bg_color`, `start_date`, `end_date`, `timer_delay`, `add_date`) VALUES ($tmpUserID, '$advertisementNameNew', '$destinationURLNew', '$dropboxUploadFile', '$backgroundColorNew', '$bannerStartDateNew', '$bannerEndDateNew', $bannerSetTimerNew, '$tmpAddDate');<br />";

if (!mysqli_query($link, "INSERT INTO ADVERTISEMENTS(`user_id`, `ad_name`, `click_url`, `img_url`, `bg_color`, `start_date`, `end_date`, `timer_delay`, `add_date`) VALUES ($tmpUserID, '$advertisementNameNew', '$destinationURLNew', '$dropboxUploadFile', '$backgroundColorNew', '$bannerStartDateNew', '$bannerEndDateNew', $bannerSetTimerNew, '$tmpAddDate');")) {
    printf("Error Code: %s\n",  mysqli_errno($link));
    echo "<br />";
    printf("Error Msg: %s\n",  mysqli_error($link));
}

I know that the database connection is working. 我知道数据库连接正在工作。 I am able to select and update tables. 我可以选择和更新表格。 I can also insert into other tables with different queries. 我还可以使用其他查询将其插入其他表。

I am open to any suggestions. 我愿意接受任何建议。

Thank you in advance for your help! 预先感谢您的帮助!

I see a few errors in your query strings. 我在您的查询字符串中看到一些错误。

First, all your variables are passed as literal strings: "... VALUES ($tmpUserID, '$advertisementNameNew', ..." should be "... VALUES (".$tmpUserID.", '".$advertisementNameNew."', ...". 首先,将所有变量作为文字字符串传递:“ ... VALUES($ tmpUserID,'$ advertisementNameNew',...”应为“ ... VALUES(“。$ tmpUserID。”,'“。$ advertisementNameNew。 “,...”。

Second, I see missing quotes around $bannerSetTimerNew. 其次,我看到$ bannerSetTimerNew周围缺少引号。

Third, there is an extra ;. 第三,有一个额外的;。

here's how I would write the query: 这是我编写查询的方式:

if (!mysqli_query($link, "INSERT INTO ADVERTISEMENTS (user_id, ad_name, click_url, img_url, bg_color, start_date, end_date, timer_delay, add_date) VALUES (".$tmpUserID.", '".$advertisementNameNew."', '".$destinationURLNew."', '".$dropboxUploadFile."', '".$backgroundColorNew."', '".$bannerStartDateNew."', '".$bannerEndDateNew."', '".$bannerSetTimerNew."', '".$tmpAddDate."')")) { ...

I didnt test it though. 我虽然没有测试。

hope this helps. 希望这可以帮助。

I see a ; 我看到一个; at the end of the query. 在查询末尾。 Are you sure that should be there? 您确定应该在那里吗?

There are two things 1. Remove the ; 有两件事:1.删除; from at the end of the query. 从查询末尾开始。 2. I hope timer_delay field has datatype "Int" if its a VARCHAR then you will have to include quotes for that field value. 2.我希望timer_delay字段的数据类型为“ Int”(如果它是VARCHAR),那么您将必须对该字段值加引号。

I hope this will help. 我希望这将有所帮助。

Passerby, thank you for your comment. 路人,谢谢您的评论。 This was my first experience with using mysqli, I changed my query to use the "bind_param" method, and everything works now. 这是我第一次使用mysqli,我将查询更改为使用“ bind_param”方法,现在一切正常。 For anyone else with a similar problem, here is the corrected code... 对于其他有类似问题的人,这里是更正的代码...

        $mysqli = new mysqli($UM_Settings["database_options"]["server_name"], $UM_Settings["database_options"]["username"], $UM_Settings["database_options"]["password"], $UM_Settings["database_options"]["database_name"]);
    if ($mysqli->connect_errno) {
        echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
    }

    $advertisementNameNew = $_POST['advertisementNameNew'];
    $destinationURLNew = $_POST['destinationURLNew'];
    $dropboxUploadFile = $_POST['dropboxUploadFile'];
    $backgroundColorNew = $_POST['backgroundColorNew'];
    $bannerStartDateNew = DateStringToMySQL($_POST['bannerStartDateNew']);
    $bannerEndDateNew = DateStringToMySQL($_POST['bannerEndDateNew']);
    $bannerSetTimerNew = intval($_POST['bannerSetTimerNew']);
    $tmpUserID = UM_GetCookie("UM_UserID");
    $tmpAddDate = DateStringToMySQL('now');

    /* Prepared statement, stage 1: prepare */
    if (!($stmt = $mysqli->prepare("INSERT INTO `ADVERTISEMENTS` (`user_id`, `ad_name`, `click_url`, `img_url`, `bg_color`, `start_date`, `end_date`, `timer_delay`, `add_date`) VALUES (?,?,?,?,?,?,?,?,?)"))) {
        echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
    }

    if (!$stmt->bind_param("issssssis",$tmpUserID, $advertisementNameNew, $destinationURLNew, $dropboxUploadFile, $backgroundColorNew, $bannerStartDateNew, $bannerEndDateNew, $bannerSetTimerNew, $tmpAddDate)) {
        echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
    }

    if (!$stmt->execute()) {
        echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
    }


    $_GET['ad_id'] = $stmt->insert_id;
    $stmt->close(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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