tags:

views:

108

answers:

2

I have the following PHP code, which is meant to insert data in the event that the username field is empty for a given username, or update data if a username exists. At the moment, the insert was previously working fine, but it would never switch to the update clause.

Now however, the insert clause fails to recognise my test variable, for no apparent reason. The errors I get are:

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

and

Notice: Undefined variable: checkUsername

which are quite recent.

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

What is a preferred way to do an update or an insert depending on the content of $username matched against the username field?

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

That's your error message? I think your problem is this line:

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

Do a var_dump on $xblah. I suspect your query is failing. Probably because you haven't fetched $username from your input. And for love of root, be sure to escape it! mysql_real_escape_string() will do what you want.

Kalium
Yep. The other query is parameterised, why not this one?
bobince
$xblah prints out bool(false) ?
Joshxtothe4
@Josh That means your query is failing. Make sure you actually have input in $username.
Kalium
@Kalium var_dump $username prints out a string of 15 chracters, the correct username. I am getting username from GET, and the input is definitly there.
Joshxtothe4
@Josh Then escape the username correctly, and try running the query manually. Your query may be malformed.
Kalium
Escaping makes no difference, and the query works perfectly manually.
Joshxtothe4
Check what you get from $con->error() after the query fails. That should give you something more specific.
Kalium
Hmm, how odd. It gives: Unknown column 'usernamestring' in 'where clause'
Joshxtothe4
it seems to be trying to use $username in place of username..., except when I print out the query I get: elect username from USERS where username = usernamestring, which is correct.
Joshxtothe4
Try this: $usernameQuery = "select username from USERS where username = '" . $con->escape_string($username) . "'";
Kalium
Basically, you're doing a string comparison. You *MUST* quote the string that is input.
Kalium
Yes...it works perfectly now...it must have been something really odd in that username, as $userquery was fine before...
Joshxtothe4
You're welcome. Just remember to always always *ALWAYS* quote your strings and escape your input.
Kalium
Now my only concern is that I want to use my update query, without overwriting existing fields unless there is new input.which will probably need a seperate question.
Joshxtothe4
Many many thanks, I have to go through and make sure everything is escaped/parameterised etc..
Joshxtothe4
I think you're OK. The method you're using is tolerable, and probably about as good as it's going to get without a full abstraction layer involved.
Kalium
A: 

If you are using MySQL and the username is unique, you can use this handy SQL:

INSERT INTO users (?, ?, ...) ON
DUPLICATE KEY UPDATE firstname=?,
lastname=?, ...)

See http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html for full documentation.

Jeff Ober
So I should use this in place of the conditional code I currently have?
Joshxtothe4