PHP/MYSQL。更新“for”循环中的多行失败`
我有以下HTML表格:PHP/MYSQL。更新“for”循环中的多行失败`,php,sql,Php,Sql,我有以下HTML表格: <form method="post" action="update-table.php"> <table class="table-data" style="width: 960px;"> <thead> <tr> <td class="sorting" rowspan="1" colspan="1" style="width: 193p
<form method="post" action="update-table.php">
<table class="table-data" style="width: 960px;">
<thead>
<tr>
<td class="sorting" rowspan="1" colspan="1" style="width: 193px;">ID</td>
<td class="sorting" rowspan="1" colspan="1" style="width: 54px;">Live</td>
</tr>
</thead>
<tbody>
<tr class="odd">
<td class="nth-1"><input type="text" value="12" name="id"></td>
<td class="nth-2"><input type="checkbox" checked="checked" name="live"></td>
</tr>
<tr class="even">
<td class="nth-1"><input type="text" value="11" name="id"></td>
<td class="nth-2"><input type="checkbox" checked="checked" name="live"></td>
</tr>
<tr class="odd">
<td class="nth-1"><input type="text" value="10" name="id"></td>
<td class="nth-2"><input type="checkbox" checked="checked" name="live"></td>
</tr>
</tbody>
</table>
<input type="submit" />
但我有一个错误:
警告:为C:\Documents and Settings\USER\Desktop\Dropbox\wamp\u at\u work\update-table.php中的foreach()提供的参数无效,第33行
第33行是:foreach($id=>live的显示顺序){
有什么问题吗?非常感谢您的建议。我假设人们无法更改ID。因此,请执行以下操作:
<td class="nth-2">
<input type="hidden" name="live[12]" value="0">
<input type="checkbox" name="live[12]" value="1" checked>
</td>
这将得到一个名为$\u POST['live']
的数组,其中包含0或1,具体取决于他们是否单击了它
隐藏字段是因为如果他们不单击它,则不会发送任何内容,这可能更难解析,因此首先我发送一个0,然后如果选中复选框,则覆盖它
如果人们可以更改ID,您需要对其进行一些修改。如果可以,请留下评论。问题是您正在复制输入元素的“名称”属性。您要做的是将数组发送回服务器,即: 更新了扩展答案:
<?php
$inp_orders = !empty($_POST['live']) ? $_POST['live'] : array();
// proccess post
if ($inp_orders)
{
foreach ($inp_orders as $id => $live) {
$id = (int) $id;
$live = (bool) $live;
// update orders
$sql = "UPDATE news SET display_order = $live WHERE id = $id";
$result = mysql_query($sql);
if($result) {
echo 'News updated';
}else {
die("Query failed");
}
}
}
// get orders from db
$res = mysql_select("SELECT * FROM news");
$view_orders = array();
while ($row = mysql_fetch_assoc($res))
{
$view_orders['id'] = $row['id'];
$view_orders['live'] = $row['live'];
}
?>
<form method="post" action="">
<table class="table-data" style="width: 960px;">
<thead>
<tr>
<td class="sorting" rowspan="1" colspan="1" style="width: 193px;">ID</td>
<td class="sorting" rowspan="1" colspan="1" style="width: 54px;">Live</td>
</tr>
</thead>
<tbody>
<?php foreach ($view_orders as $order): $id = (int) $order['id']; $odd = true; ?>
<tr class="<?php echo $odd ? 'odd' : 'even' ?>">
<td class="nth-1"><?php echo $id ?></td>
<td class="nth-2"><input type="checkbox" <?php echo $order['live'] ? 'checked="checked"' : '' ?> name="live[<?php echo $id ?>]" /></td>
</tr>
<?php $odd = $odd ? false : true; endforeach; ?>
</tbody>
</table>
<input type="submit" />
</form>
身份证件
居住
name=“live[]”/>
哪里定义了$display\u order
我没有看到它。在您尝试循环其值之前,任何地方都没有声明变量$display\u order
。您还应该将输入的名称更改为id[]
和live[]
因为您希望所有值(而不仅仅是一个值)都作为数组。修剪数组也没有意义。设置显示顺序的位置在哪里?对于您的信息(主题除外):$live=(isset($\u POST['live'])?1:0;有点奇怪,因为if语句本身已经返回true或false,所以赋值为1或0(对或错)不是必需的…@Juhana-没有设置在任何位置;/I如何收集所有值并将其放入数组$display\u order?@Ariel-这是正确的。ID是固定的。$\u POST['live']将是0或1。@Dan Grossman-不,ID为的输入将是只读的。我们只想更新复选框值(1或0)。有意思。那你为什么要把它作为文本输入到自己的表格单元格中呢?@dan grossman你完全可以依赖顺序!事实上你的live[]解决方案
明确地依赖于顺序。调用字段ID并输入其顺序将非常奇怪——特别是因为如果这样做,您就没有ID字段了,因此您将很难将其插入数据库。所以我猜他是什么意思,而不是他做了什么。@Dan Grossman-发布它的值-并输入在ID上插入$live,就是这样。
<?php
$inp_orders = !empty($_POST['live']) ? $_POST['live'] : array();
// proccess post
if ($inp_orders)
{
foreach ($inp_orders as $id => $live) {
$id = (int) $id;
$live = (bool) $live;
// update orders
$sql = "UPDATE news SET display_order = $live WHERE id = $id";
$result = mysql_query($sql);
if($result) {
echo 'News updated';
}else {
die("Query failed");
}
}
}
// get orders from db
$res = mysql_select("SELECT * FROM news");
$view_orders = array();
while ($row = mysql_fetch_assoc($res))
{
$view_orders['id'] = $row['id'];
$view_orders['live'] = $row['live'];
}
?>
<form method="post" action="">
<table class="table-data" style="width: 960px;">
<thead>
<tr>
<td class="sorting" rowspan="1" colspan="1" style="width: 193px;">ID</td>
<td class="sorting" rowspan="1" colspan="1" style="width: 54px;">Live</td>
</tr>
</thead>
<tbody>
<?php foreach ($view_orders as $order): $id = (int) $order['id']; $odd = true; ?>
<tr class="<?php echo $odd ? 'odd' : 'even' ?>">
<td class="nth-1"><?php echo $id ?></td>
<td class="nth-2"><input type="checkbox" <?php echo $order['live'] ? 'checked="checked"' : '' ?> name="live[<?php echo $id ?>]" /></td>
</tr>
<?php $odd = $odd ? false : true; endforeach; ?>
</tbody>
</table>
<input type="submit" />
</form>