【问题标题】:PHP UPDATE prepared statementPHP UPDATE 准备好的语句
【发布时间】:2021-01-28 11:00:29
【问题描述】:

我正在尝试学习使用准备好的语句的正确方法来避免 SQL 注入等。

当我执行脚本时,我从脚本中收到一条消息,说 0 Rows Inserted,我希望这会说 1 Rows Inserted 并且当然更新表。我不完全确定我准备好的陈述,因为我已经做了一些研究,我的意思是它因示例而异。

当我更新我的表格时,我需要声明所有字段还是只更新一个字段??

任何信息都会很有帮助。

index.php

<div id="status"></div>

    <div id="maincontent">
    <?php //get data from database.
        require("classes/class.Scripts.inc");
        $insert = new Scripts();
        $insert->read();
        $insert->update();?>

       <form action="index2.php" enctype="multipart/form-data" method="post" name="update" id="update">
              <textarea name="content" id="content" class="detail" spellcheck="true" placeholder="Insert article here"></textarea>
        <input type="submit" id="update" name="update" value="update" />
    </div>

classes/class.Scripts.inc

public function update() {
    if (isset($_POST['update'])) {
        $stmt = $this->mysqli->prepare("UPDATE datadump SET content=? WHERE id=?");
        $id = 1;
        /* Bind our params */                           
        $stmt->bind_param('is', $id, $content);
        /* Set our params */
        $content = isset($_POST['content']) ? $this->mysqli->real_escape_string($_POST['content']) : '';

        /* Execute the prepared Statement */
        $stmt->execute();
        printf("%d Row inserted.\n", $stmt->affected_rows);

    }                   
}

【问题讨论】:

  • 你试过看看会发生什么吗?
  • real_escape_string() 对于准备好的语句不是必需的
  • 在你的SQL中,第一个参数是内容,第二个是ID。当您调用 bind_param 时,您已经颠倒了顺序 - 尝试在 bind_param 中交换顺序
  • @PatrickKostjens 我确实尝试过,但它根本不会影响桌子
  • @andrewsi 你的权利!谢谢!

标签: php mysqli sql-update prepared-statement


【解决方案1】:
$stmt = $this->mysqli->prepare("UPDATE datadump SET content=? WHERE id=?");
/* BK: always check whether the prepare() succeeded */
if ($stmt === false) {
  trigger_error($this->mysqli->error, E_USER_ERROR);
  return;
}
$id = 1;
/* Bind our params */
/* BK: variables must be bound in the same order as the params in your SQL.
 * Some people prefer PDO because it supports named parameter. */
$stmt->bind_param('si', $content, $id);

/* Set our params */
/* BK: No need to use escaping when using parameters, in fact, you must not, 
 * because you'll get literal '\' characters in your content. */
$content = $_POST['content'] ?: '';

/* Execute the prepared Statement */
$status = $stmt->execute();
/* BK: always check whether the execute() succeeded */
if ($status === false) {
  trigger_error($stmt->error, E_USER_ERROR);
}
printf("%d Row inserted.\n", $stmt->affected_rows);

你的问题:

我从我的脚本中收到一条消息,说 0 Rows Inserted

这是因为您在绑定参数时颠倒了参数的顺序。因此,您正在 id 列中搜索 $content 的数值,该数值可能被解释为 0。因此 UPDATE 的 WHERE 子句匹配零行。

我需要声明所有字段还是只更新一个字段??

在 UPDATE 语句中只设置一列是可以的。其他列不会更改。

【讨论】:

  • 第 10 行的第一个参数是什么?
  • @AnnonomusPerson:每个参数一个字母。 's' 表示字符串,'i' 表示整数等。阅读php.net/manual/en/mysqli-stmt.bind-param.php
  • @BillKarwin 对我来说 $status variable 正在显示 integer 值。我使用了echo $status,输出为1
  • @NimmagaddaGowtham,当您回显 true 值时,PHP 将打印 1
  • @stib,它通过引用绑定到变量。它不会在您绑定到它们时复制它们的值。然后你可以修改这个值,execute()那个时候把这个值发送给MySQL。它发送您execute() 时变量的当前值。这意味着您可以将变量绑定到准备好的语句,然后运行循环,修改值并在循环内多次使用execute()。在这种情况下,无需在每次循环迭代期间重新绑定变量。
【解决方案2】:

事实上,准备好的语句并不像其他答案中显示的那么复杂。恰恰相反,准备好的语句是执行查询的最简单、最整洁的方式。以你的情况为例。你只需要三行代码!

$stmt = $this->mysqli->prepare("UPDATE datadump SET content=? WHERE id=?");
$stmt->bind_param('si', $content, $id);
$stmt->execute();
  1. 使用占位符准备查询
  2. 然后绑定变量(提示:您可以安全地对任何变量使用“s”)
  3. 然后执行查询。

就像 1-2-3 一样简单!

请注意,手动检查每个函数的结果实在是太疯狂了,它只会使您的代码膨胀而没有任何好处。相反,您应该一劳永逸地configure mysqli to report errors automatically。为此,请在mysqli_connect()/new mysqli 之前添加以下行:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

结果将与 trigger_error 几乎相同,但没有一行额外的代码!如您所见,如果使用得当,代码可以非常简单明了。

【讨论】:

    【解决方案3】:

    我想清理 Bill Karwin 的精彩代码

    $stmt = $this->mysqli->prepare("UPDATE datadump SET content=? WHERE id=?") or die ($this->mysqli->error);
    
    $id = 1;
    
    // Bind our params
    // BK: variables must be bound in the same order as the params in your SQL.
    // Some people prefer PDO because it supports named parameter.
    $stmt->bind_param('si', $content, $id) or die ($stmt->error);
    
    // Set our params
    // BK: No need to use escaping when using parameters, in fact, you must not, 
    // because you'll get literal '\' characters in your content. */
    $content = (string)$_POST['content'] ?: '';
    
    /* Execute the prepared Statement */
    $status = $stmt->execute() or die ($stmt->error);
    
    
    printf("%d Row inserted.\n", $stmt->affected_rows);
    

    我建议使用“or die”而不是 if 子句 我建议强制变量类型取值:

    // If id brings value: '12abc', PHP automatically stops it at 12
    $id = (int)$_ POST ["id"];
    

    【讨论】:

    • 你没有清理代码,反而让代码变得更糟,并且引入了新的漏洞。请参阅 YCS 的回答如何正确操作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-27
    • 1970-01-01
    • 2010-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多