samedi 11 juin 2016

PHP - MYSQLI - UPDATE DATABASE

I want users to UPDATE any field(s) they want in d database - table but I don't want the UPDATE .. SET to erase existing records with empty submission if they submit without changing all the fields.. but changed only the ones they want to..

$sql = "UPDATE table SET username = '$username', email = '$email',
fname = '$fname', lname = '$lname', address = '$address', city = '$city',
country = '$country', phone = '$phone', aboutme = '$aboutme' WHERE email = '$email'";

If the user only updates address and phone then submits his entry.. this instruction erases other fields that is not filled in the form.... I don't want that to happen. Kindly look into this. Thanks

Please I have tried your suggestion but its not working for me.. may I am doing something wrong -- I am new to PHP - Here is my code below: $sql = "UPDATE user_profile SET "; if ($username!="") $sql ."username = '$username',"

if ($fname!="") $sql ."fname = '$fname',"

if ($lname!="") $sql ."lname = '$lname',"

if ($address!="") $sql ."address = '$address',"

if ($city!="") $sql ."city = '$city',"

if($country!="") $sql ."country = '$country', "

if($phone!="") $sql ."phone = '$phone', "

if($aboutme!="") $sql ."aboutme = '$aboutme' "

$sql ."WHERE email = '$email'";

$query = mysqli_query($database,$sql); if($query) { $message = "Update Successful!"; //echo "update successful"; }

Aucun commentaire:

Enregistrer un commentaire