2009-04-16 39 views
0

我有以下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); 
} 
} 

什么是做一个更新,或者根据$用户名对用户名字段相匹配的内容插入一个优选的方法是什么?

回答

1
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()会做你想做的。

+0

是的。另一个查询是参数化的,为什么不是这个? – bobince 2009-04-16 01:34:00

+0

$ xblah打印出bool(false)? – 2009-04-16 02:22:25