0

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.

Kyle Heath
  • 13
  • 4

1 Answers1

0

Yes this is possible

However, there are some problems with your conditions. First, if you want to print the value of a variable inside a string, it needs to be in double quotations (") as opposed to single quotations ('). This question contains a very good explanation of the difference between the two.

$str = 'foobar';
echo '$str';  // prints: $str
echo "$str";  // prints: foobar

Since you are comparing the literal string '$str' with the constant null the condition is always going to be true

Furthermore, if you simply need to check that a value is not null, or compare it to another value, casting it to a string may cause some problems. Instead, you can simply compare the variable directly, without the quotation marks.

$foo = null;
$bar = 'bar';

if($str != null) {
    echo '$foo is not null';
}
else {
    echo '$foo is null';
}

if($bar != null) {
    echo '$bar is not null';
}
else {
    echo '$bar is null';
}

/*
 * The above outputs:
 * $foo is null
 * $bar is not null
 */

I would also suggest taking a look at the difference between double and triple equal in php, as well as PHP's own comparison table which contains good examples of comparison methods like isset() and empty() and some quirks about comparisons using ==

William Perron
  • 1,288
  • 13
  • 18