PHP MySQL的:从数据库中选择并填写表格

问题描述:

我想从数据库中填充客户端的地址. 我使用此代码从db中进行选择:

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 */

,此代码显示表单:

 <?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>'; 
              }
              ?>

问题: 1)它只显示一个地址,即使在同一个客户端中有两个地址,也是如此.

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

2)我对此很陌生.我是做对了还是有最快的(更少的代码)选项来做到这一点? 谢谢!!

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

问题在您的while循环内:

The problem is within your while loop:

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

每次while循环到变量时,这都会分配一个新数组.这些分配行都应更改为

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.