gpt4 book ai didi

php - 条件准备语句不切换

转载 作者:行者123 更新时间:2023-11-29 15:10:27 25 4
gpt4 key购买 nike

我有以下 PHP 代码,其目的是在给定用户名的用户名字段为空的情况下插入数据,或者在用户名存在时更新数据。目前,插入之前工作正常,但它永远不会切换到更新子句。

但是现在,插入子句无法识别我的测试变量,没有明显的原因。我得到的错误是:

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given

Notice: Undefined variable: checkUsername

这是最近的。

if($cmd=="submitinfo"){

$usernameQuery = "select username from USERS where username = $username";

$xblah = $con->query($usernameQuery);
while ($row = mysqli_fetch_assoc($xblah))
{
$checkUsername = $row['username'];

}

if ($checkUsername == null) {

$userQuery = "INSERT INTO USERS VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
if ($userInfo = $con->prepare($userQuery)) {
$userInfo->bind_param("ssssssssssssssssssss", $username, $firstname, $lastname, $flaggedauctions, $lastauction, $street1, $city1, $postcode1, $street2, $city2, $postcode2, $phone, $mobilephone, $fax, $email, $website, $bank, $banknumber, $accountnumber, $comments);
$userInfo->execute();
$userInfo->close();
echo "true";
} else {
echo "false";
}
print_r($con->error);
}

else if ($checkUsername == $username) {

$userQuery = "UPDATE USERS SET firstname = ?, lastname = ?, flaggedauctions = ?, lastauction = ?, street1 = ?, city1 = ?, postcode1 = ?, street2 = ?, city2 = ?, postcode2 = ?, phone = ?, mobilephone = ?, fax = ?, email = ?, website = ?, bank = ?, banknumber = ?, accoutnumber = ? WHERE username = ?";
if ($userInfo = $con->prepare($userQuery)) {
$userInfo->bind_param("sssssssssssssssssss", $firstname, $lastname, $flaggedauctions, $lastauction, $street1, $city1, $postcode1, $street2, $city2, $postcode2, $phone, $mobilephone, $fax, $email, $website, $bank, $banknumber, $accountnumber, $username);
$userInfo->execute();
$userInfo->close();
echo "true";
} else {
echo "false";
}
print_r($con->error);
}
}

根据 $username 与用户名字段匹配的内容进行更新或插入的首选方法是什么?

最佳答案

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given

这是你的错误信息?我认为你的问题是这一行:

$xblah = $con->query($usernameQuery);

对 $xblah 执行 var_dump。我怀疑你的查询失败了。可能是因为您尚未从输入中获取 $username 。而对于爱根来说,一定要逃离它! mysql_real_escape_string() 会做你想做的事。

关于php - 条件准备语句不切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/754432/

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