String cutting off during insert query [duplicate]

一曲冷凌霜 提交于 2020-01-07 07:59:09

问题


Why is this doing what it's doing?

Code:

<?php
    if (isset($_POST['sourceInsert'])) {
        $url = $db_con3->real_escape_string($_POST['url']);
        $desc = $db_con3->real_escape_string($_POST['desc']);
        echo '$urlbefore is ' . $url . '<br />'; ///for troubleshooting
        $result = $db_con3->query("INSERT INTO gdrive_links (evalid, userid, url, desc) VALUES ('$evalid', '$id', '$url', '$desc')");

        echo '$urlafter is ' . $url . '<br />'; ///For troubleshooting
        echo $db_con3->error; ///For troubleshooting
    }
?>

HTML output:

$urlbefore is https://docs.google.com/file/d/0B0tcjQ3FxlB6dWlMTkNQVjBwVDA/edit?usp=sharing
$urlafter is https://docs.google.com/file/d/0B0tcjQ3FxlB6dWlMTkNQVjBwVDA/edit?usp=sharing
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 'desc) VALUES ('1284017', '1', 'https://docs.google.com/file/d/0B0tcjQ3FxlB6dWlMT' at line 1

So the string is fine before and after the query string, but in the query it's being cut off at the 49th character. Am I missing something dumb? It looks like my query syntax is correct...


回答1:


The problem is because, you have a reserved keyword, unescaped.

$result = $db_con3->query("INSERT INTO gdrive_links (`evalid`, `userid`, `url`, `desc`) VALUES ('$evalid', '$id', '$url', '$desc')");

You need to escape them using backticks, this way. desc is a reserved keyword in MySQL. Escape them like above.



来源:https://stackoverflow.com/questions/16765485/string-cutting-off-during-insert-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!