views:

146

answers:

2

I have the following conditional code, which will insert a new record if a record with the appropriate username does not exist, and update the record if it does exist. This works fine.

However, at the moment, if I insert a new record, and only insert the firstname and lastname, and maybe address details with say phone information being left blank this is fine. If I then wish to update the record with just the phone record, the name and address information gets replaced with nothing.

What I would like to know, is if it is possibly to have an easy way to populate my php html form for updating the information, with the content of the field I will be updating? I am using

 <input type="text" name="uniquename" />

To obtain user input, which is then passed to a javascript function which is then passed back to the below php code. If this is not possible, is there an easy way to work some sql/php magic, to only update a field corresponding the userinput which is not empty?

$usernameQuery = "select username from USERS where username = '" . $con->escape_string($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);
}
A: 

You just have to build your update statement and bind parameter dynamically. e.g.

$userQuery = "UPDATE ";

if (!empty($firstname)) {
  $userQuery += "firstname = ?,";
}

...    

if (!empty($firstname)) {
  $bindArgs[] = $firstname;
}
...

so your query won't update columns that are not supposed to change.

oykuo
Hmm, thanks. What is happening with bindArgs?
Joshxtothe4
My PHP skill is a little bit rusty but I think you can pass $bindArgs into the bind_param function.I believe you can pass arrays into variable number arguments function in PHP.
oykuo
so they are two seperate solutions? I would just have bindARgs for each variable?
Joshxtothe4
No no they are the same solution.Because you build up your query conditionally you will have to build up your bind variable list dynamically too.
oykuo
What you do is that you build up your $bindArgs array then pass it into the bind_param function this way:$userInfo->bind_param("sssssssssssssssssss", $bindArgs)like I said my PHP skill is a bit rusty but I think this works because variable-length argument list function should accept array too.
oykuo
Btw, what I meant was you build up your $bindArgs array this way:if (!empty($firstname)) $bindArgs[] = $firstname;if (!empty($lastname)) $bindArgs[] = $lastname;if (!empty($flaggedauctions)) $bindArgs[] = $flaggedauctions;...
oykuo
The problem is though, that there may already be data in the record. Or there may be no data. If there is data, I don't want to overwrite it with empty data. If there is new data, I want to overwrite it with the new data.
Joshxtothe4
Yes this is why I suggested you build your query dynamically because this statement "UPDATE User SET firstname = 'ABC' WHERE id = 3562" will update only the "firstname" column to 'ABC' for user record 3562 but leave last name untouched
oykuo
there may be a problem, tough. what, if i already entered a telephone number and want to remove it? clearing the input/text would cause the !empty(tel) to be false and skip the update, leaving the original value in place.
Schnalle
Schnalle, yes you are right in that case just check for empty string in addition to empty.
oykuo
A: 

More generally, note that:

  • It's wise to specify your column names in INSERT ... VALUES because otherwise if someone adds a column you'll be annoyed and if someone reorders the columns you'll be confused.

  • There's a race condition between your select and your update ... another process could insert/delete a row while you're still thinking, causing your insert/update to fail Consider either:

    • INSERT ... ON DUPLICATE KEY UPDATE ...
    • REPLACE INTO ...
Sharkey
how would insert on duplicate or replace fix the race condition?
Joshxtothe4