我正在从mysql数据库加载一个表。我已经包含在一个表单中,因为我希望用户能够对数据库进行更改并提交。数据从db加载,但是当我更改一个值并点击提交时,它总是发送表的最后一行的数据,而不是我更改的值。如何将整个表发送到数据库? $stid
是正确的。
<?php
$q3 = mysqli_query($link,"SELECT * FROM stores_op_hours WHERE Store_Id='$stid'
order by field(Day_Name,'sun','mon','tue','wed','thu','fri','sat')" );
?>
<form action="store-admin.php" method="post">
<table class="table ophours">
<thead class="thead-inverse">
<tr><th>Day</th><th>Open Time</th><th>Close Time</th><th>Notes</th></tr>
</thead>
<tbody>
<?php
while($rw = mysqli_fetch_array($q3)){
$day = $rw['Day_Name'];
$op = $rw['Open_Time'];
$cl = $rw['Close_Time'];
$day2 = strtoupper($day);
$notes = $rw['Store_Op_Notes'];
?>
<input type="hidden" name="day" value="<?php echo $day; ?>" >
<input type="hidden" name="stid" value="<?php echo $stid; ?>" >
<tr><td><input type="text" readonly name="day2" value="<?php echo $day2; ?>" ></td>
<td><input type="text" name="open" value="<?php echo $op; ?>" > </td>
<td><input type="text" name="close" value="<?php echo $cl; ?>" > </td>
<td><input type="text" name="notes"<?php echo $notes; ?> ></td></tr>
<?php
}
?>
</tbody>
</table>
<input type="submit" id="ophourchange" name="ophourchange" value="Save Changes" >
</form>
POST代码:
if(isset($_POST['ophourchange'])){
$stid = $_POST['stid'];
$day = $_POST['day'];
$open = $_POST['open'];
$close = $_POST['close'];
$notes = $_POST['notes'];
echo "<script type='text/javascript'>alert('stid". $stid."')</script>";
echo "<script type='text/javascript'>alert('day". $day."')</script>";
echo "<script type='text/javascript'>alert('open". $open."')</script>";
echo "<script type='text/javascript'>alert('close". $close."')</script>";
echo "<script type='text/javascript'>alert('notes". $notes."')</script>";
$upd_st_op = " UPDATE stores_op_hours SET Open_Time='$open', Close_Time='$close', Store_Op_Notes='$notes' WHERE Store_Id='$stid' AND Day_Name='$day' ";
$res = mysqli_query($link, $upd_st_op);
if (!mysqli_query($link,$res))
{
echo "<script type='text/javascript'>alert('Error". mysqli_error($link)."')</script>";
}else{
//echo "Store Updated";
echo "<script type='text/javascript'>alert('Store Open Hours Updated')</script>";
}
}
答案 0 :(得分:1)
创建行时,请使用行id
,假设表格中有一个自动递增的id
列:
while($rw = mysqli_fetch_array($q3)){
// You need to have this column here
$id = $rw['id'];
$day = $rw['Day_Name'];
$op = $rw['Open_Time'];
$cl = $rw['Close_Time'];
$day2 = strtoupper($day);
$notes = $rw['Store_Op_Notes'];
?>
<input type="hidden" name="day[<?php echo $id; ?>]" value="<?php echo $day; ?>" >
<input type="hidden" name="stid[<?php echo $id; ?>]" value="<?php echo $stid; ?>" >
<tr><td><input type="text" readonly name="day2[<?php echo $id; ?>]" value="<?php echo $day2; ?>" ></td>
<td><input type="text" name="open[<?php echo $id; ?>]" value="<?php echo $op; ?>" > </td>
<td><input type="text" name="close[<?php echo $id; ?>]" value="<?php echo $cl; ?>" > </td>
<td><input type="text" name="notes[<?php echo $id; ?>]"<?php echo $notes; ?> ></td></tr>
<?php
}
然后你的sql语句(你现在只是在测试但是稍后会解决所有的注入问题,毫无疑问)将id
作为另一个匹配的参数:
"UPDATE `stores_op_hours`
SET `Open_Time` = '$open',
`Close_Time` = '$close',
`Store_Op_Notes` = '$notes'
WHERE
`id` = '{$id}'";
答案 1 :(得分:0)
为每个输入命名,如
<input type="hidden" name="data[0][day]" value="" />
<input type="hidden" name="data[0][stid]" value="" />
<input type="hidden" name="data[1][day]" value="" />
<input type="hidden" name="data[1][stid]" value="" />
然后在php
$_POST['data'];
将是一个数组
答案 2 :(得分:0)
请你改变你的while循环如下: -
<?php
$i = 0;
while($rw = mysqli_fetch_array($q3)){
$day = $rw['Day_Name'];
$op = $rw['Open_Time'];
$cl = $rw['Close_Time'];
$day2 = strtoupper($day);
$notes = $rw['Store_Op_Notes'];
?>
<input type="hidden" name="data[<?php echo $i;?>]['day']" value="<?php echo $day; ?>" >
<input type="hidden" name="data[<?php echo $i;?>]['stid']" value="<?php echo $stid; ?>" >
<tr>
<td><input type="text" readonly name="data[<?php echo $i;?>]['day2']" value="<?php echo $day2; ?>" ></td>
<td><input type="text" name="data[<?php echo $i;?>]['open']" value="<?php echo $op; ?>" > </td>
<td><input type="text" name="data[<?php echo $i;?>]['close']" value="<?php echo $cl; ?>" > </td>
<td><input type="text" name="data[<?php echo $i;?>]['notes']" value="<?php echo $notes; ?>" ></td>
</tr>
<?php
$i++;
}
?>
然后您可以使用$ _POST [&#39;数据&#39;];
测试数据<?php
echo '<pre>';
print_r($_POST['data']);
?>
它可能对你有帮助