【问题标题】:String cutting off during insert query [duplicate]插入查询期间字符串切断[重复]
【发布时间】:2013-05-27 02:03:15
【问题描述】:

为什么要这样做?

代码:

<?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 输出:

$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

所以字符串在查询字符串之前和之后都很好,但是在查询中它在第 49 个字符处被截断。我错过了什么愚蠢的东西吗?看来我的查询语法是正确的...

【问题讨论】:

  • 你有(evalid, userid, url, desc) VALUES ('$evalid', '$id', '$url', '$desc'),不应该是(evalid, userid, url, desc) VALUES ('$evalid', '$userid', '$url', '$desc')吗?
  • desc 是 mysql 关键字...重命名为任何其他
  • 不,这个表的userid是用户的$id。
  • 自我注意...不要使用 desc 作为字段名,永远...:)
  • @Rich701 你可以,但在转义之后。 :)

标签: php mysql syntax insert


【解决方案1】:

问题在于,您有一个未转义的 reserved keyword

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

您需要使用反引号将它们转义,这种方式。 desc 是 MySQL 中的保留关键字。像上面一样逃脱它们。

【讨论】:

  • 我刚刚更改了字段名称 file_desc,一切正常,谢谢。
  • 欢迎您!如果你想使用desc 本身,你可以使用这种方式。您可以复制我的答案中的代码并将其粘贴到您的应用程序中以使其工作。 :)
  • 使用未转义的保留字会触发错误,而不是插入部分字符串
  • @Dagon 它确实触发了一个错误。请查看输出。
  • 哦,他的意思是错误消息中的字符串被截断;好吧,这完全无关紧要:-)
猜你喜欢
  • 1970-01-01
  • 2018-10-15
  • 2017-08-07
  • 2017-03-01
  • 2014-12-25
  • 1970-01-01
  • 2013-10-06
  • 1970-01-01
  • 2010-11-29
相关资源
最近更新 更多