1

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!!

1
  • in your while-loop, with each iteration, you assign a completely new array to your variables, thus overwriting the former value. Commented Jan 18, 2016 at 6:35

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks Shadow, I did it, but now the form is populated with: <br /><b>Notice</b>: Array to string conversion in.... and it refers to the lines where the code populates the form... !!
I think you left the array() function in your code as in @marinus' 1st version, which is incorrect.. Can you pls show me what you have tried exactly? Edit: which he has now corrected.
Yes, the display code is later in the text, after that the $stmt is closed. <?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 '" />'; }
This one ? while($row = $resultaddress->fetch_assoc()) { $addressID_array[] = array ($row['addressID']); $addresstype_array[] = array ($row['addresstype']); ...
I told you to remove the array() function out of the code, pls check my answer gain!
|
0

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. :-)

1 Comment

Thanks Mark. I still have trouble to understand the array coding... anytime there is an array I have a panic attack :) !!! I hope I will get used to work with them! PS Are you suggesting that my code is vulnerable to injection?!!!!
0

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 '" />';
}

2 Comments

Thanks Marinus, I did it, but now the form is populated with: <br /><b>Notice</b>: Array to string conversion in.... and it refers to the lines where the code populates the form... !!
The display code is later in the text, after that the $stmt is closed. How can I use the while loop there?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.