简体   繁体   English

MySQL 中的 PHP 更新查询未更新但 SELECT 和 INSERT 有效

[英]PHP update query in MySQL not updating but SELECT and INSERT works

If I try to update an entry in my database, it doesn't work, as in not update.如果我尝试更新我的数据库中的条目,它不起作用,因为没有更新。 But when I try to select a value from the same database, that works just fine.但是当我尝试从同一个数据库中选择一个值时,效果很好。 I'm not sure why this is so.我不确定为什么会这样。

Update query:更新查询:

$id = 1;

try {
    $conn = db();
    $sql = "UPDATE instagram SET token=?, expires=? WHERE id=$id";
    $stmt = mysqli_stmt_init($conn);

    if (!mysqli_stmt_prepare($stmt, $sql)) {
        throw new Exception($conn->error);
    }

    mysqli_stmt_bind_param($stmt, "si", $tokenAccess, $tokenExpires);
    mysqli_stmt_execute($stmt);

    // on error
    if (!mysqli_stmt_execute($stmt)) {
        throw new Exception($conn->error);
    }

    var_dump($stmt);

    $stmt->close();
    $conn->close();
}
catch (Exception $e) {
    print_r($e);
}

var_dump for $stmt returns: $stmt var_dump返回:

 object(mysqli_stmt)#4 (10) { ["affected_rows"]=> int(1) ["insert_id"]=> int(0) ["num_rows"]=> int(0) ["param_count"]=> int(2) ["field_count"]=> int(0) ["errno"]=> int(0) ["error"]=> string(0) "" ["error_list"]=> array(0) { } ["sqlstate"]=> string(5) "00000" ["id"]=> int(1) }

It's probably in plain sight and I'm missing the obvious.它可能很明显,我错过了明显的。 A pointer in the right direction is much appreciated.非常感谢指向正确方向的指针。

UPDATE:更新:

My table setup is as follows:我的表设置如下: 表设置

It has one entry where:它有一个条目,其中:

  • token = ABCdefg123adc........ <-- example string, contains numbers and letters token = ABCdefg123adc........ <-- 示例字符串,包含数字和字母
  • expires = 60过期 = 60
  • created_at = 2019-12-01 08:57:26 created_at = 2019-12-01 08:57:26
  • id = 1. id = 1。

I tried to access the table via:我尝试通过以下方式访问该表:

$sql = "INSERT INTO instagram (token, expires) VALUES (?, ?)";

This, like SELECT , works.这,就像SELECT一样,有效。

I also found that if (!mysqli_stmt_execute($stmt)) was sending the entry twice, so I took that out.我还发现if (!mysqli_stmt_execute($stmt))发送了两次条目,所以我把它if (!mysqli_stmt_execute($stmt))了。 Still, updating the entry doesn't work.尽管如此,更新条目不起作用。

I have also echoed the values for $tokenAccess and $tokenExpires .我还回应了$tokenAccess$tokenExpires的值。 The result contains 60 for $tokenExpires and the long-lived access token for $tokenAccess , as expected.正如预期的$tokenAccess ,结果包含$tokenExpires 60$tokenExpires的长期访问令牌。

I was having similar problems when I was learning mysqli years ago.几年前我在学习 mysqli 时遇到了类似的问题。 I am guessing that no one told you can enable error reporting for mysqli functions.我猜没有人告诉你可以为 mysqli 函数启用错误报告。 You can do it by adding this line before you open a connection:您可以通过在打开连接之前添加此行来实现:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

I can see you are adding a lot of unnecessary code.我可以看到您添加了很多不必要的代码。 You don't need all these if statements and you don't need to throw exceptions manually.您不需要所有这些if语句,也不需要手动抛出异常。 PHP can do it for you. PHP 可以为您完成。

You don't need to close the statement and you don't need to close the connection.您不需要关闭语句,也不需要关闭连接。 Never catch exceptions just to print out the error message!永远不要为了打印出错误信息而捕获异常!

Using OOP is also going to make your code cleaner and easier to spot mistakes.使用 OOP 还可以使您的代码更清晰,更容易发现错误。

I can't see what your db() function looks like, but it should hopefully look something like this.我看不到您的db()函数是什么样的,但希望它看起来像这样。 Take a look how much simpler this code is:看看这段代码有多简单:

function db(): \mysqli {
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    $mysqli = new \mysqli('localhost', 'username', 'password', 'testdb');
    $mysqli->set_charset('utf8mb4'); // always set the charset
    return $mysqli;
}

$conn = db();

$id = 1;
$sql = "UPDATE instagram SET token=?, expires=? WHERE id=?";
$stmt = $conn->prepare($sql);
$stmt->bind_param('sss', $tokenAccess, $tokenExpires, $id);
$stmt->execute();

About your real problem:关于你真正的问题:
There probably was no problem at all.应该没有问题吧。 affected_rows indicates that 1 row has been updated. affected_rows表示已更新 1 行。

You are using prepared statements so, your codes in where clause should be like this WHERE id = ?";您使用的是准备好的语句,所以 where 子句中的代码应该是这样的WHERE id = ?";

$id = '1'; 
$tokenAccess = $_POST['token']; 
$tokenExpires = $_POST['expires'];
//Make sure you are passing these values to php 
try {
    $conn = db();
    $sql = "UPDATE instagram SET token=?, expires=? WHERE id = ?";
    $stmt = mysqli_stmt_init($conn);

    if (!mysqli_stmt_prepare($stmt, $sql)) {
        throw new Exception($conn->error);
    }

    mysqli_stmt_bind_param($stmt, "ssi", $tokenAccess, $tokenExpires, $id);
    //If $tokenExpires uses - OR / OR ETC.. you need to set it to varchar 
    // on error
    if (!mysqli_stmt_execute($stmt)) {
        throw new Exception($conn->error);
    }
    //you can return affected rows mysqli_stmt_affected_rows($stmt);
    var_dump($stmt);

    $stmt->close();
    $conn->close();
}
catch (Exception $e) {
    print_r($e);
}

See here : How to debug Php code?请参阅此处: 如何调试 Php 代码?

Here is how you can use : https://www.php.net/manual/en/mysqli-stmt.affected-rows.php以下是您可以使用的方法: https : //www.php.net/manual/en/mysqli-stmt.affected-rows.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM