-1

I'm trying to update the users address, which is in multiple different columns, one for each part of the address.

It doesn't produce any errors in the error_log, nor the site, and when I click submit all it does is refresh and show a blank page.

$Street = (!empty($_POST['street'])) ? $_POST['street'] : false;
$City = (!empty($_POST['city'])) ? $_POST['city'] : false;
$State = (!empty($_POST['state'])) ? $_POST['state'] : false;
$ZIP = (!empty($_POST['ZIP'])) ? $_POST['ZIP'] : false;
$Submit = (!empty($_POST['submit'])) ? true : false;
$ErrorArray = array();
if ($Submit) {
    if (!$Street || !$City || !$State || !$ZIP) {
        if (!$Street) {
            array_push($ErrorArray, 'Please enter your Street Number.');
        } if (!$City) {
            array_push($ErrorArray, 'Please enter a City');
        } if (!$State) {
            array_push($ErrorArray, 'Please enter a State.');
        } if (!$ZIP) {
            array_push($ErrorArray, 'Please enter a ZIP.');
        }
    }
    else {

        $UpdateAdd = $db->prepare("UPDATE User SET User.Street, User.City, User.State, User.ZIP VALUES(':Street', ':City', ':State', ':ZIP') WHERE User.ID = '".$gU->ID."'");
        $UpdateAdd->bindParam(':Street', $Street, PDO::PARAM_STR);
        $UpdateAdd->bindParam(':City', $City, PDO::PARAM_STR);
        $UpdateAdd->bindParam(':State', $State, PDO::PARAM_STR);
        $UpdateAdd->bindParam(':ZIP', $ZIP, PDO::PARAM_STR);
        $UpdateAdd->execute();
        array_push($ErrorArray, 'Address Updated!');
    }
}
Mocking
  • 1,764
  • 2
  • 20
  • 36
brandon
  • 21
  • 5
  • You're pushing error messages into the array but you aren't doing anything with the array. It's like putting values into a variable without printing the variable or writing the variable anywhere. – Mocking Dec 02 '17 at 02:50
  • The array is echo'd further on down the page. '; if (!empty($ErrorArray)) { echo '
    '.implode('
    ', $ErrorArray).'
    '; } echo '
    – brandon Dec 02 '17 at 02:52
  • Where are you instantiating $db? Did you make sure the connection was successful? – Mocking Dec 02 '17 at 02:55
  • I have a page being included at the beginning on line 1, didnt display it here, and Yes the connection is successful because I have multiple other things connecting to it. – brandon Dec 02 '17 at 02:57
  • Only thing I can think of is that you aren't passing a post value for 'submit'. – Mocking Dec 02 '17 at 03:01
  • Try put an 'else' statement for the main 'if' statement and print an error message for that. – Mocking Dec 02 '17 at 03:03
  • I'm not seeing any use of database error display, as putting the single quotes around the placeholders may be throwing sql prepare errors. – IncredibleHat Dec 02 '17 at 03:12
  • I took the single quotes out and still haven't received any errors. – brandon Dec 02 '17 at 03:21
  • You state: *"It doesn't produce any errors in the error_log, nor the site, and when I click submit all it does is refresh and show the page blank."* - That's because you're not using proper error handling. Consult the duplicate to look at what you did wrong and Google "string literals mysql" and you will see it there also and at the same time, you'd of learned something new. – Funk Forty Niner Dec 02 '17 at 03:50

1 Answers1

0

Check your HTML form and make sure your submit button has a name attribute of 'submit'?

<input type="submit" name="submit">

After that check, change the PHP line from:

$Submit = (!empty($_POST['submit'])) ? true : false;

To:

$Submit = (isset($_POST['submit'])) ? true : false;

Since you are checking for existance instead of a value for that input.

  • Checked the submit tag, and everything looked fine, changed the $Submit to $Submit = (isset($_POST['submit'])) ? true : false; and it did the same thing, refreshed, didn't update the database and left a blank page. – brandon Dec 02 '17 at 02:41
  • TEST: if ($Submit) { echo 'submit: true
    '; if (!$Street || !$City || !$State || !$ZIP) { echo 'field test: true'; exit;
    – Nerds of Technology Dec 02 '17 at 02:51
  • It refreshes the page and page is blank. – brandon Dec 02 '17 at 02:55
  • Using `empty()` against possible empty inputs is best and especially for what they're using now. However, should there be any radio/checkboxes, then using `isset()` is best for those. – Funk Forty Niner Dec 02 '17 at 03:52