这个问题跟随另一个问题link,但代码和问题不同,所以我问另一个问题更好。希望不是重复。
我有一个编辑表格。这是从DB中检索数据的代码:
$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] : "") . '"/>';
}
问题:
当我编辑表单时,即我输入一个新的电话号码,然后点击提交按钮,表单将重新加载所有新数据(areacode,phonenumber),但新的phoneID不会在表单中回显。 / p>
但它在我开发时用作测试的while循环中回响。 如果我在地址栏中重新键入页面地址,则新的phoneID会在表单中回显。
我做错了什么?
修改 根据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) }
SOLUTION:
在查看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对不起,问题在于我在(我已经很久)的问题中没有包含的唯一一块代码:(!
答案 0 :(得分:1)
你的循环可以像这样简单:
$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. '"/>';
}