我有以下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);
}
}
什麼是做一個更新,或者根據$用戶名對用戶名字段相匹配的內容插入一個優選的方法是什麼?
是的。另一個查詢是參數化的,爲什麼不是這個? – bobince 2009-04-16 01:34:00
$ xblah打印出bool(false)? – 2009-04-16 02:22:25