codeispoetry
codeispoetry

Reputation: 373

php mysql: select from database and populate form

I would like to populate addresses for client from my db. I use this code to select from db:

$stmt = $conn->prepare("SELECT * FROM peopleaddress WHERE peopleID=?");
    if ( !$stmt ) {die(printf("Error: %s.\n", mysqli_stmt_error($stmt) ) );}
else if ( !$stmt->bind_param('i', $peopleID ) ) {die(printf("Error: %s.\n", mysqli_stmt_error($stmt) ) );}
else if ( !$stmt->execute() ) { die(printf("Error: %s.\n", mysqli_stmt_error($stmt) ) ); }
else {
    $result = $stmt->get_result();

    while($row = $result->fetch_assoc()) {
        $addressID_array = array ($row['addressID']); 
        $addresstype_array = array ($row['addresstype']); 
        $addressactive_array = array ($row['active']);
        $street_array = array ($row['street']);
        $city_array = array ($row['city']);
        $town_array = array ($row['town']);
        $state_array = array ($row['state']);
        $zip_array = array ($row['zip']);
        $country_array = array ($row['country']);
        $latitude_array = array ($row['latitude']);
        $longitude_array = array ($row['longitude']);

    }   
} /* end else */

and this code to display the form:

 <?php      
              for ($i = 0; $i < count($addressID_array); $i++) {
                  echo '<input type="text" name="street[]" id="" placeholder="street" value="';
                            if (isset ($street_array[$i])){echo $street_array[$i];}  echo '" />';
                  echo '<input type="text" name="city[]" id="city" placeholder="city" value="';
                            if (isset ($city_array[$i])){echo $city_array[$i]; } echo '" />';
                  echo '<input type="text" name="zip[]" id="zip" placeholder="postalcode" value="';
                            if (isset ($zip_array[$i])){echo $zip_array[$i]; } echo '" />'; 
                  echo '<input type="text" name="town[]" id="town" placeholder="town" value="';
                            if (isset ($town_array[$i])){echo $town_array[$i]; } echo '" />'; 
                  echo '<input type="text" name="state[]" id="state" value="';
                            if (isset ($state_array[$i])){echo $state_array[$i];} echo '" />'; 
                  echo '<input type="text" name="country[]" id="country" value="';
                            if (isset ($country_array[$i])) {echo $country_array[$i];} echo '" />'; 
                  echo '<input type="text" name="addresstype[]" id="" value="';
                            if (isset ($addresstype_array[$i])) {echo $addresstype_array[$i];} echo '" />';                       
                  echo '<input type="text" name="addressactive[]" id="" value="';
                            if (isset ($addressactive_array[$i])) {echo $addressactive_array[$i];} echo '" />';                                           echo '<input type="text" name="latitude[]" id="latitude" READONLY value="';
                            if (isset ($latitude_array[$i])) {echo $latitude_array[$i];} echo '" />';  
                  echo '<input type="text" name="longitude[]" id="longitude" READONLY value="';
                            if (isset ($longitude_array[$i])) {echo $longitude_array[$i];} echo '" /> <br>'; 
              }
              ?>

Problems: 1) it only display one address, even if in db there are 2 addresses for the same client.

2) I'm pretty new at this. Am I doing it right or there is a fastest (less code) option to do this? Thanks!!

Upvotes: 1

Views: 226

Answers (3)

Marinus
Marinus

Reputation: 551

You are overwriting the values in your array by doing this

$addressID_array = array ($row['addressID']); 

instead of

$addressID_array[] = $row['addressID']; 

Update: Also, it would be a good idea to iterate over your data, instead of making an array of data and then reading that array again. Use this:

else {
$result = $stmt->get_result();
}   
// Then in display
while($row = $result->fetch_assoc()) {
    echo '<input type="text" name="street[]" id="" placeholder="street" value="';
    if (isset ($row['street'])){echo $row['street'];}  echo '" />';
}

Upvotes: 0

Shadow
Shadow

Reputation: 34305

The problem is within your while loop:

$addressID_array = array ($row['addressID']);

This assigns a new array every time the while loops to the variables. These assignment lines should all be changed like

$addressID_array[] = $row['addressID'];

As for your 2nd question: it is not really answerable because we do not know the requirements you need to work against.

Upvotes: 1

Mark Manning
Mark Manning

Reputation: 1467

Check out : https://en.wikipedia.org/wiki/SQL_injection

Look down at the "Hexadecimal Conversion" part. I put a short function to do SQL commands in there. When you get the information back it will be in an array. So if you used $row to get the information back it would be in $row[0][<Fields>], $row[1][<Fields>], and so on.

The problem with the above is - every time you do the "array()" it makes a new array. So it wipes what you had in there before. :-)

Upvotes: 0

Related Questions