I have this:
if(isset($_POST["Submit"])) {
$updatequery = @mysql_query("UPDATE users SET FirstName='".$_POST['firstname']."', LastName='".$_POST['lastname']."', Address='".$_POST['address']."', City='".$_POST['city']."', PostalCode='".$_POST['postalcode']."', HomePhone='".$_POST['homephone']."', AltPhone='".$_POST['altphone']."', HomeWebsite='".$_POST['homewebsite']."', EmailAddress='".$_POST['email']."', Paypal='".$_POST['paypal']."', Alertpay='".$_POST['alertpay']."', Payoneer='".$_POST['payoneer']."' WHERE Username = '".$_SESSION['Username']."'");</i>
Now unfortunately when a user updates their info (it’s a profile page), it updates everything which means if I left the Paypal email empty (because I already had it there) it updates the Paypal email with emptiness.
How do I solve this?
Just to note, this is a really basic example and I wouldn’t recommend using this in a live environment, but it should get you going in the right direction:-
First, create an array mapping your column names to your
$_POSTvariables, eg:Next, loop over your newly created
$arrayand check if the value exists in your$_POSTarray, like so:Notice the
$dataarray that has been created, this stores thecolumn = "value"data. You can then just implode the$dataarray into your SQL string:Which would output (with some fictional
$_POSTdata):Just to reiterate, I would not advise that you use this code as it is. It is imperative that you validate and sanitise the user input beforehand!