这个问题紧随另一个问题 link ,但是代码和问题不同,所以我认为最好问另一个问题。希望不是重复的。
我有一个编辑表单。这是从数据库检索数据的代码:
$stmt = $conn->prepare("
SELECT
phonetype.phonetypeID,
phonetype.phonetype,
phone.phoneID,
phone.countrycode,
phone.areacode,
phone.phonenumber,
phone.extension
FROM phonetype
LEFT JOIN phone
ON phonetype.phonetypeID=phone.phonetypeID
and phone.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 {
$resultaddress = $stmt->get_result();
while ($row = $resultaddress->fetch_assoc()) {
$phonetypeID_array[] = (isset($row['phonetypeID']) ? $row['phonetypeID'] : "");
$phonetype_array[] = (isset($row['phonetype']) ? $row['phonetype'] : "");
$phoneID_array[] = (isset($row['phoneID']) ? $row['phoneID'] : "");
$countrycode_array[] = (isset($row['countrycode']) ? $row['countrycode'] : "");
$areacode_array[] = (isset($row['areacode']) ? $row['areacode'] : "");
$phonenumber_array[] = (isset($row['phonenumber']) ? $row['phonenumber'] : "");
$extension_array[] = (isset($row['extension']) ? $row['extension'] : "");
echo $row['phonetypeID'];
echo $row['phonetype'];
echo $row['phoneID'];
echo $row['countrycode'];
echo $row['areacode'];
echo $row['phonenumber'];
echo $row['extension'] . '<br>';
}
} /* end else */
这是表单的代码
for ($i = 0; $i < 4; $i++) {
echo '<input type="text" name="phoneID[]" id="" value="' . (isset($phoneID_array[$i]) ? $phoneID_array[$i] : "") . '"/>';
echo '<input type="text" name="countrycode[]" id="" size="3" maxlength="3" value="' . (isset($countrycode_array[$i]) ? $countrycode_array[$i] : "") . '"/>';
echo '<input type="text" name="areacode[]" id="" value="' . (isset($areacode_array[$i]) ? $areacode_array[$i] : "") . '"/>';
echo '<input type="text" name="phonenumber[]" id="" value="' . (isset($phonenumber_array[$i]) ? $phonenumber_array[$i] : "") . '"/>';
}
问题:
当我编辑表单时,即输入新的电话号码,然后单击提交按钮,表单会重新加载所有新数据(区号、电话号码),但新的电话 ID 不会在表单中回显。
但它与我在开发时用作测试的 while 循环相呼应。 并且如果我在地址栏中重新输入页面地址,新的电话 ID 将在表单中回显。
我做错了什么?
编辑: 根据 cal_b 建议,如果我测试:
var_dump($phoneID_array);
刚加载页面时的输出是:
array(1) { [0]=> int(28) }
array(2) { [0]=> int(28) [1]=> string(0) "" }
array(3) { [0]=> int(28) [1]=> string(0) "" [2]=> string(0) "" }
array(4) { [0]=> int(28) [1]=> string(0) "" [2]=> string(0) "" [3]=> int(29) }
添加新手机时的输出:
array(5) { [0]=> string(2) "28" [1]=> string(0) "" [2]=> string(0) "" [3]=> string(2) "29" [4]=> int(28) }
array(6) { [0]=> string(2) "28" [1]=> string(0) "" [2]=> string(0) "" [3]=> string(2) "29" [4]=> int(28) [5]=> int(58) }
array(7) { [0]=> string(2) "28" [1]=> string(0) "" [2]=> string(0) "" [3]=> string(2) "29" [4]=> int(28) [5]=> int(58) [6]=> int(59) }
array(8) { [0]=> string(2) "28" [1]=> string(0) "" [2]=> string(0) "" [3]=> string(2) "29" [4]=> int(28) [5]=> int(58) [6]=> int(59) [7]=> int(29) }
解决方案:
查看 var_dump 并阅读有关数组的 php.net 手册后,我明白了我做错了什么。
在 SUBMIT 代码部分,我加载数组中的所有 $_post
/*put all the entries into an arrays */
$phoneID_array = $_POST['phoneID'];
$phonetypeID_array = $_POST['phonetypeID'];
$countrycode_array = $_POST['countrycode'];
$areacode_array = $_POST['areacode'];
$phonenumber_array = $_POST['phonenumber'];
$extension_array = $_POST['phoneextension'];
准备变量并循环插入
for ($i = 0; $i < count($phonetypeID_array); $i++) {
$phoneID = mysqli_real_escape_string($conn, $phoneID_array[$i]);
$countrycode = mysqli_real_escape_string($conn, $countrycode_array[$i]);
........................
我认为当你对数组使用相同的名称时,就像你对变量所做的那样,它只是分配新值,但显然我错了。如果您使用相同的变量名称,它将把新值添加到以前的值中。 (我还是不太明白为什么,如果有人能写两行来解释一下,我将不胜感激)。
所以解决办法是: a) 在将相同名称分配给另一个数组之前取消设置数组 或者 b) 只需使用不同的名称
PS 抱歉,问题出在我没有包含在我的(已经很长)问题中的唯一代码块中:(!
最佳答案
你的循环可以像这样简单:
$rows = array();
while($row = $resultaddress->fetch_assoc()) {
$rows[]=$row;
}
然后你像这样打印:
foreach ($rows as $row) {
$phoneID = (isset ($row['phoneID']) ? $row['phoneID'] : "");
$countrycode = (isset ($row['countrycode']) ? $row['countrycode'] : "");
$areacode = (isset ($row['areacode']) ? $row['areacode'] : "");
$phonenumber = (isset ($row['phonenumber']) ? $row['phonenumber'] : "");
echo '<input type="text" name="phoneID[]" id="" value="' .$phoneID. '"/>';
echo '<input type="text" name="countrycode[]" id="" value="' .$countrycode. '"/>';
echo '<input type="text" name="areacode[]" id="" value="' .$areacode. '"/>';
echo '<input type="text" name="phonenumber[]" id="" value="' .$phonenumber. '"/>';
}
关于php - 点击提交按钮后无法从数组中的数据库回显一个值(id),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34936265/