0

我有以下sql语句..

$sql = "select tableid, comment, ID, description from reservations 
        where uniqueid = '" . $reservationid . "'";

$reservationid 通过 POST 表单传递。选择了不同字段的非常相似的语句在另一个页面上似乎可以正常工作。但是当我在页面上回显 sql 语句时,我得到

select tableid, comment, ID, description 
from reservations 
where uniqueid = '51e5f949c1828'

似乎跳过了最后的单引号

我也试过

$sql = "select tableid, comment, ID, description from reservations 
        where uniqueid = '$reservationid'";

结果相同。如果我查看页面源代码,我不明白为什么我会看到这个..

select tableid, comment, ID, description from reservations 
where uniqueid = '51e5f949c1828</form

那个杂散的</form> 部分到底是从哪里来的?

4

5 回答 5

1

您的表格中似乎有一些错误。发送的数据未正确完成,因此表单 html 的一部分与它一起发送。如果不发布您如何发送数据,则无法真正说出。您必须更正此问题才能使代码正常工作。

您提交的 PHP 没有任何问题,但是它很容易成为 SQL 注入攻击等的目标。因此最好使用例如 sprintf。 http://php.net/manual/en/function.sprintf.php

于 2013-08-01T11:03:16.647 回答
1

但是当我在页面上回显 sql 语句时,我得到

select tableid, comment, ID, description from reservations where uniqueid = <'51e5f949c1828 

如果您“回显”到您的浏览器窗口,则浏览器很有可能会默默地</form>从显示中删除。我敢打赌,如果您在显示查询时查看页面的源代码,您也会看到... where uniqueid = '51e5f949c1828</form>

你确定$reservationid包含你所期望的吗?

您能否显示更多代码。喜欢你的 html 表单吗?那里可能存在格式错误的 html 和/或未闭合的引号。


顺便说一句,您永远不应该在查询中直接从不受信任的来源注入变量。这会导致SQL 注入

于 2013-08-01T11:03:32.183 回答
0

使用 sprintf() - 这是组合字符串等这类事情的更好方法。

于 2013-08-01T10:58:39.640 回答
0

我认为您的单引号和双引号混淆了。

$sql = "select tableid, comment, ID, description from reservations where uniqueid = "' . $reservationid . '"";
于 2013-08-01T11:00:47.487 回答
0

你也可以这样查询

$sql = "select tableid, comment, ID, description 
       from reservations where uniqueid = '$reservationid'";

或者干脆

$sql = "select tableid, comment, ID, description 
        from reservations where uniqueid = $reservationid ";
于 2013-08-01T11:03:52.507 回答