Variable returning Undefined

0

I have this code snippet and the inputs, when I click to edit the city and click to save works normal, now if I do not change the city, it passes as undefined and I can not save. How can I do it so that when I do not change the city store the same way?

if ($street === $streetOriginal && $town === $townOriginal){

    $stmtSaveAddress = $conn->prepare("UPDATE address SET street = :street, number = :number, town = :town, complement = 
                                      :complement, city_id = (select city.city_id from city where city.link = :cityLink)
                                      WHERE address_id = :address");

    $stmtSaveAddress->bindValue(':street', $street);
    $stmtSaveAddress->bindValue(':number', ($number <= 0 ? NULL : $number));
    $stmtSaveAddress->bindValue(':town', $town);
    $stmtSaveAddress->bindValue(':cityLink', $city);
    $stmtSaveAddress->bindValue(':complement', $complement);
    $stmtSaveAddress->bindValue(':address', $addressId);


    echo 'city: '.$city;



    if ($stmtSaveAddress->execute()) {
        echo 'trueaddress';
    }else{
        echo 'falseaddress';

   }

Form:

<div class="inputs-without-icon" id="box-city">
   <input id="city" type="text" maxlength="100" placeholder="*Cidade" list="cities-list" value="<?php echo "{$Menu->getAddressCityName()} - {$Menu->getAddressState()}"; ?>" onkeyup="charByCharCity(this.value);">
   <input id="street" type="text" placeholder="*Rua" maxlength="70" value="<?php echo $Menu->getAddressStreet(); ?>">   
</div>
<datalist id="cities-list"><option data-number='0' value='Nome - Estado'>Cardápios</datalist>

This is my form.

    
asked by anonymous 13.10.2016 / 16:17

2 answers

0
//Se quando vir undefined não é pra alterar o dado você faz assim:
if(isset($city)&&$city!=null){
   if ($stmtSaveAddress->execute()) {
       echo 'trueaddress';
   }else{
       echo 'falseaddress';
   }
}
    
13.10.2016 / 16:29
0

You want to do the independent update if there was a change in the data or not, correct?

In this case you just have to do the update without going through any logical tests.

    $stmtSaveAddress = $conn->prepare("UPDATE address SET street = :street, number = :number, town = :town, complement = 
                                  :complement, city_id = (select city.city_id from city where city.link = :cityLink)
                                  WHERE address_id = :address");

$stmtSaveAddress->bindValue(':street', $street);
$stmtSaveAddress->bindValue(':number', ($number <= 0 ? NULL : $number));
$stmtSaveAddress->bindValue(':town', $town);
$stmtSaveAddress->bindValue(':cityLink', $city);
$stmtSaveAddress->bindValue(':complement', $complement);
$stmtSaveAddress->bindValue(':address', $addressId);


echo 'city: '.$city;



if ($stmtSaveAddress->execute()) {
    echo 'trueaddress';
}else{
    echo 'falseaddress';
}
    
13.10.2016 / 16:33