我的表單無法更新任何數據,請幫助我,它在「array_keys()上的顯示錯誤期望參數1爲數組,null爲給定的」在第4行和「無效參數爲foreach()提供的第5行ramadan.php。array_keys()期望參數1爲數組,空給出
這裏我想顯示所有的數據從sql查詢和更新所有的另一個查詢。
它在我的其他網頁的工作很好,但在這種情況下,它無法工作..
我的形式
<form action="ramadan.php" method="POST">
<?
$result = mysqli_query($dbh,"SELECT * FROM ramadan WHERE id IN (1, 2, 3)");
if(!$result) {
die("Database query failed: " . mysqli_error());
}
while($row = mysqli_fetch_assoc($result)) {
$id=$row['id'];
$ramadan=$row['ramadan'];
$date=$row['date'];
$taraweeh=$row['taraweeh'];
echo '
<tr>
<td><input type="text" name="ramadan['.$id.']" class="form-control" value="'.$ramadan.'"></td>
<td><input type="text" name="date['.$id.']" class="form-control" value="'.$date.'"></td>
<td><input type="text" name="taraweeh['.$id.']" class="form-control" value="'.$taraweeh.'"></td>
</tr>
';}
?>
<input type="submit" class="btn btn-success" name="submit" id="submit" value="Submit">
</form>
ramadan.php
include("../db.php");
global $dbh;
if(isset($_POST['submit'])){
$ids = array_keys($_POST['name']);
foreach ($ids as $id) {
$ramadan = mysqli_real_escape_string($dbh,$_POST['ramadan'][$id]);
$date = mysqli_real_escape_string($dbh,$_POST['date'][$id]);
$taraweeh = mysqli_real_escape_string($dbh,$_POST['taraweeh'][$id]);
$id = mysqli_real_escape_string($dbh,$id);
// update data in mysql database
$update = mysqli_query($dbh,"UPDATE ramadan SET hl='$hl', ramadan='$ramadan', date='$date', taraweeh='$taraweeh' WHERE id = '$id'");
}
// if successfully updated.
}
您還沒有發佈'$ _ POST [ '名']'任何地方。 – Alex
看到這是一個問題我的工作很好:http://stackoverflow.com/a/29984790/4453049 – koc