更改表格保存问题中的值

时间:2017-08-09 07:39:24

标签: php mysql

此代码应检查LikedOne与用户名为jim的行的行col交集是否等于text" empty"。

 $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();
}

1 个答案:

答案 0 :(得分:0)

$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();