[英]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. 问题:1)它仅显示一个地址,即使在db中有2个地址用于同一客户端。
2) I'm pretty new at this. 2)我很新。 Am I doing it right or there is a fastest (less code) option to do this?
我是做对了还是有最快的(较少代码)选项来做到这一点? Thanks!!
谢谢!!
The problem is within your while loop: 问题出在您的while循环内:
$addressID_array = array ($row['addressID']);
This assigns a new array every time the while loops to the variables. 每当while循环到变量时,这将分配一个新数组。 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. 关于您的第二个问题:这并不是真正的答案,因为我们不知道您需要遵循哪些要求。
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 '" />';
}
Check out : https://en.wikipedia.org/wiki/SQL_injection 签出: 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.
我在其中放了一个简短的函数来执行SQL命令。 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.
因此,如果使用$ row取回信息,则它将位于$ row [0] [<Fields>],$ row [1] [<Fields>]等中。
The problem with the above is - every time you do the "array()" it makes a new array. 上面的问题是-每次您执行“ array()”都会创建一个新数组。 So it wipes what you had in there before.
因此,它可以擦除您之前的内容。 :-)
:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.