gpt4 book ai didi

php - 更改表保存问题中的值

转载 作者:行者123 更新时间:2023-11-29 18:32:18 24 4
gpt4 key购买 nike

此代码应检查 LikedOne 的行 col 交集与用户名为 jim 的行是否等于文本“空”。

 $stmt1 = $conn->prepare("SELECT likedOne FROM UserData WHERE username = ?");
$stmt1->bind_param('s',$username);
//$username = $_POST["username"];
$username ="jim";
$stmt1->execute();
$stmt1->store_result();

$res = $stmt1->fetch();

if ( $res == "empty"){
echo "debug 3";
$sql = $conn->prepare("UPDATE UserData SET likedOne=? WHERE username=?");
$sql->bind_param('ss',$TUsername,$Username);
// $TUsername = $_POST["TUsername"];
// $Username = $_POST["username"];
$TUsername = "test";
$Username = "jim";
$sql->execute();


}

第一次将其更改为测试,但它仍然打印调试 3,这意味着它仍然将 $res 注册为“空”,即使它应该是“测试”。

编辑无效!

    $stmt1 = $conn->prepare("SELECT likedOne FROM UserData WHERE username = ?");
$stmt1->bind_param('s',$username);
//$username = $_POST["username"];
$username ="jim";
$stmt1->execute();
$stmt1->bind_result($res);
$found_row = $stmt1->store_result();
if ( $found_row && $res == "empty"){
echo "debug 3";
$sql = $conn->prepare("UPDATE UserData SET likedOne=? WHERE username=?");
$sql->bind_param('ss',$TUsername,$Username);
// $TUsername = $_POST["TUsername"];
// $Username = $_POST["username"];
$TUsername = "test";
$Username = "jim";
$sql->execute();
}

最佳答案

$stmt1->fetch() 不会返回 likedOne 列的内容。如果返回了一行,则返回 TRUE;如果结果集中没有更多行,则返回 NULL;如果发生错误,则返回 FALSE

要检索准备好的语句返回的数据,您需要使用$stmt1->bind_result()

$stmt1 = $conn->prepare("SELECT likedOne FROM UserData WHERE username = ?");
$stmt1->bind_param('s',$username);
//$username = $_POST["username"];
$username ="jim";
$stmt1->execute();
$stmt1->bind_result($res);
$found_row = $stmt1->store_result();

if ($found_row && $res == "empty") {
...
}

我不确定为什么执行此操作的代码不起作用,但没有必要执行两个查询,您可以在一个查询中完成。

$sql =  $conn->prepare("UPDATE UserData SET likedOne=? WHERE username=? AND likedOne = 'empty'");
$sql->bind_param('ss',$TUsername,$Username);
//$TUsername = $_POST["TUsername"];
//$Username = $_POST["username"];
$TUsername = "test";
$Username = "jim";
$sql->execute();

关于php - 更改表保存问题中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45584427/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com