简体   繁体   English

使用foreach循环以数组的形式将数据保存到mysql数据库

[英]save data in form of arrays to mysql database using a foreach loop

I have a form that submits multiple values using php. 我有一个使用php提交多个值的表单。 code is below: 代码如下:

    echo "<form action='?ud=".$ud."' method='post'>Name: <input type='text' name='fname' />";
$resultw = mysql_query("SELECT * FROM options where userid='$ud' ORDER BY priority ASC");

while($row = mysql_fetch_array($resultw))
  {  
  echo "
 <input type='hidden' name='widgetid[]' value='".$row['widgetid']."' />
  <span id='".$row['widgetid']."' style='color:white;font-size:13px;'>".$row['items'] .    "</span><br></div><div style='' class='portlet_content'>
   Enabled <input title='Enabled/Disabled' type='checkbox' value='enable[]' name='enable[]' ".$checked." id='checkbox".$row['id']."' />  
   Height <input title='set height' name='height[]' value='".$row['height']."' type='text' name='textfield' id='textfield".$row['id']."' />
 ";
  } 

  echo '<input type="submit" /></form>';?> 

as you can see its a loop thats get the values from db and echoes a form. 正如你可以看到它的一个循环,它从db获取值并回显一个表单。 I made it as simple as possible it has more input boxes but i removed them and removed the styling to make it simpler to read. 我让它尽可能简单,它有更多的输入框,但我删除了它们并删除了样式,使其更易于阅读。 when I hit Submit I would like to be able to have all those values updated to the database through a loop. 当我点击提交时,我希望能够通过循环将所有这些值更新到数据库。 I tried foreach and struggled. 我尝试了foreach并且努力了。 any suggestions 有什么建议么

First of all, your code has a security bug in that you are directly putting a variable into a SQL query. 首先,您的代码存在安全性错误,因为您直接将变量放入SQL查询中。 That allows for SQL injection. 这允许SQL注入。 Instead, you should put the variable $ud through mysql_real_escape_string before inserting into the query or, significantly better, use MySQLi/PDO and prepared statements. 相反,您应该在插入查询之前将变量$ ud放在mysql_real_escape_string中,或者更好的是,使用MySQLi / PDO和预处理语句。

Have you checked that the form is correctly echoed onto the page? 您是否检查过表单是否正确回显到页面上? View the source (or use Firefox and Firebug) to double check that it has been properly inserted. 查看源(或使用Firefox和Firebug)以仔细检查它是否已正确插入。

Then you will need to have a code block that is initiated when it receives a POST request. 然后,您将需要一个在收到POST请求时启动的代码块。 That code will get an array back for each of your variables eg 该代码将为每个变量返回一个数组,例如

 $widget_ids = $_POST['widgetid']; #this will be an array
 $enable = $_POST['enable'];
 $height = $_POST['height'];

You can do this for each of your POSTed variables and then just loop round the widget ids doing an update query for each one eg 您可以为每个POSTed变量执行此操作,然后循环遍历窗口小部件ID,为每个变量查询执行更新查询,例如

$i = -1;
foreach($widget_ids as $widget_id) {
      $row_enable = $enable[++$i];
      $row_height = $height[$i];
      //DO THIS FOR THE REST AND THEN YOUR UPDATE QUERY
}

Just to be pedantic, your HTML is also a bit messy and incorrect. 只是为了迂腐,你的HTML也有点混乱和不正确。 For a form I would use label elements for each label and don't use br's for new lines. 对于表单,我会为每个标签使用标签元素,不要对新行使用br。 You also have a div with no beginning and then a div with no ending. 你也有一个没有开头的div,然后是没有结尾的div。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM