1

我想用以下代码更新我的表。但更新不成功。

 $sql_edit = "UPDATE subscription SET name=?,email=? WHERE name=? & email=?";
        $q_edit = $pdo->prepare($sql_edit);
        $q_edit->execute(array($_POST['new_name'],$_POST['new_email'],$_POST['old_name'],$_POST['old_email']));
4

2 回答 2

0

我怀疑这是因为您的 $_POST 变量之一未设置。下面的代码检查 post 变量是否已设置,如果未设置,则将其设置为null. 我已经更改了您使用的 & 符号,它应该是AND

$sql_edit = "UPDATE subscription SET name=?,email=? WHERE name=? AND email=?";
        $q_edit = $pdo->prepare($sql_edit);
        $do_update = $q_edit->execute(array(
            (isset($_POST['new_name']) ? $_POST['new_name'] : null),
            (isset($_POST['new_email']) ? $_POST['new_email'] : null),
            (isset($_POST['old_name']) ? $_POST['old_name'] : null),
            (isset($_POST['old_email']) ? $_POST['old_email'] : null)
        ));

// Also check that it was executes successfully in your code
if (!$do_update)
{
echo "Failed to update";
}
else
{
echo "Update successful";
}

我建议在发布它们之前检查值,而不是像那样将它们放入数组中。

要检查的另一件事是您的连接未设置为隐式事务,否则您需要将更改提交到数据库(或回滚):

$pdo->rollBack();

$pdo->commit();

这仅在您的列设置为允许空值时才有效。

于 2013-10-15T09:50:18.080 回答
0

使用ANDinWHERE条件而不是&as &is BITWISE 运算符

    $sql_edit = "UPDATE subscription SET name=?,email=? WHERE name=? AND email=?";
    $q_edit = $pdo->prepare($sql_edit);
    $q_edit->execute(array($_POST['new_name'],$_POST['new_email'],$_POST['old_name'],$_POST['old_email']));
于 2013-10-15T09:53:36.853 回答