I've finished my homework project but I'm trying to go a little bit further. My project requires me to take information from a form, link that information to a variable and then either create, delete, update or look for that information in the MySQL database.
As part of the update, I'm trying to update only the fields for an address, city, state, zip or phone number. However, I only want to update fields that came from information in the form. If a user leaves one of the above fields blank, I don't want that blank field updating the database. So if there is information in the database and the user leaves a null field in the form, I don't want that null field blanking out the field in the database.
I tried setting up each of the fields to have an if statement so that if the information is not null that it will update the database
Here is my code that I am starting with.
$newPerson = "UPDATE `FriendsFamily`.`Contact`
SET
`address` = '$address',
`city` = '$city',
`state` = '$state',
`zip` = '$zip',
`phone` = '$phone'
where `fname` = '$fname'
and `lname` = '$lname'";
and here is something I tried several variations of.
$newPerson = "UPDATE FriendsFamily
.Contact
if('$address' != null)
{
SET `address` = $address;
}
if('$city' != null)
{
SET `city` = $city;
}
if('$zip' != null)
{
SET `zip` = '$zip';
}
if('$phone' != null)
{
SET `phone` = '$phone'
}
where `fname` = '$fname'
and `lname` = '$lname'";
and I also tried this.
$newPerson = "UPDATE FriendsFamily
.Contact
if('$address' != null)
{
SET `address` = $address,
}
if('$city' != null)
{
SET `city` = $city,
}
if('$zip' != null)
{
SET `zip` = '$zip',
}
if('$phone' != null)
{
SET `phone` = '$phone',
}
where `fname` = '$fname'
and `lname` = '$lname'";
Is what I want to do even possible, if so how do I do it.