循环更新数据形式PHP的MySQL未来的html [英] loop to update data coming from form php mysql html
问题描述
我有我创建的更新从一个PHP表单来的数据的数据库以下code。 $ _ POST ['变量']
是不同的阵列。
我遇到的问题是,当我回声 $更新
现场状态和显示设备领域的值是不正确的顺序。例如,如果我选中复选框3.它会返回的结果的第一行启用的值。任何建议将帮助谢谢
I have the following code that I created to update the database with the data coming from a a php form. $_POST['variables']
are different arrays.
the issue I am having is when I echo $updater
the field status and the field display values are not in the correct order. for example if I check the checkbox 3. it will return the value enabled on the first line of the results. any suggestions would help thank you
//update data
$priority = $_POST['priority']; // this will be an array
$enable = $_POST['enable'];
$height = $_POST['height'];
$position = $_POST['position'];
$widgetid = $_POST['widgetid'];
$display = $_POST['display'];
$i = -1;
foreach($priority as $priori)
{
++$i;
$row_enable = $enable[$i];
$row_height = $height[$i];
$row_prio = $priority[$i];
$positio = $position[$i];
$disp = $display[$i];
$widgeti = $widgetid[$i];
if (isset($enable[$i]))
$enables ="y";
else
$enables ="n";
if (isset($display[$i]))
$displ = "y";
else
$displ = "n";
//DO THIS FOR THE REST AND THEN YOUR UPDATE QUERY
$updater = "UPDATE hpoptions SET position='$positio', height='$row_height', status='$enables', display='$displ', priority='$row_prio' WHERE userid='$ud' and widgetid='$widgeti'";
echo $updater."<br>";
} // ends here
推荐答案
有不能保证你会得到你的阵列所需顺序,除非你强迫它的HTML。你可能有这样的事情:
There is no guarantee you will get your arrays in the desired order, unless you force it in the HTML. You probably have something like this:
<input type="text" name="position[]">
<input type="text" name="height[]"> ...
<input type="hidden" name="widgetid[]" value="w1">
...
<input type="text" name="position[]">
<input type="text" name="height[]"> ...
<input type="hidden" name="widgetid[]" value="w2">
...
您需要一个额外的维度添加到阵列上的连接字段名codeD。你需要为每个字段组唯一的ID,我相信你的为widgetid
正是如此,对不对?所以,你可以这样做:
You need to add an extra dimension to the arrays encoded on the field name. You need an unique id for each field group, and I believe your widgetid
is exactly that, right? So you can do:
<input type="text" name="data[w1][position]">
<input type="text" name="data[w1][height]"> ...
...
<input type="text" name="data[w2][position]">
<input type="text" name="data[w2][height]"> ...
...
请注意,你甚至不需要一个名为场为widgetid
了,因为标识的连接上的每个字段名codeD。在PHP中,你这样做遍历结果:
Notice you don't even need a field called widgetid
anymore, since the ids will be encoded on every field name. In PHP, you do this to loop through the results:
foreach($_POST['data'] as $widgetid => $data) {
// Make sure to check if the values won't make your SQL query vulnerable to injection!
// http://stackoverflow.com/questions/332365/xkcd-sql-injection-please-explain
$widgetid = mysql_real_escape_string($widgetid);
$position = is_numeric($data['position']) ? $data['position'] : 0;
// ...
// Build your update query here
}
这篇关于循环更新数据形式PHP的MySQL未来的html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!