1

这是我用来尝试创建多行以插入数据库的形式。

  <input name="input[]" type="checkbox" value="0" id="input_0" /> Directional<br /> <br/>
  <input name="input[]" type="checkbox" value="1" id="input_1" /> Technical <br />
  <br />
  <input name="input[]" type="checkbox" value="2" id="input_2" /> Reference <br />
  <br />
  <input name="input[]" type="checkbox" value="3" id="input_3" /> Research <br />
  <br />
  <input name="input[]" type="checkbox" value="4" id="input_4" /> Phone <br /></h3>

  <input type="submit" name="button" id="button" value="Submit Tally" style="height: 25px; width: 100px">
  </Form>

当前使用 implode 从复选框表单将多行插入 mysql 数据库的问题。循环可以解决这个问题吗?选中复选框时,我可以看到数组 (#,#,#,#)

  $type = $_POST['input'];
 // $sid = $_SERVER['REMOTE_ADDR'];
  $user = $_POST['user'];

  if(count($type) > 0)
  {
  $type_string = implode(',', $type);

  }
  $sql = "INSERT INTO tally (tid, sid, uid, date, time, catid) 
      VALUE (NULL, 1, '$user', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, '$type_string')";


        mysql_query($sql) or die(mysql_error());
     echo "Success";

     print_r($type_string);
4

3 回答 3

0

计算如下的值$type

  $i=0;
  foreach ($_POST as $v) {
   if(isset($v['input'.$i])) {
    $type = $v; 
    $i++;
   }
  }
于 2012-09-22T00:10:40.297 回答
0

在您从作为输入接收的 $type 变量创建一些数组并将其输入到您的查询中之后,循环肯定会起作用。

但我希望您不要像这样在某些网站上使用此代码。正如 maiotano84 已经评论的那样,创建查询的方式有点危险。很容易对您的数据做一些不需要的事情。至少你应该把你的变量通过一个函数,比如

string mysql_real_escape_string ( string $unescaped_string [, resource $link_identifier = NULL ] )

这是php.net站点上有关此功能的更多信息。

于 2012-09-21T23:16:38.983 回答
0
  <Form action="" method="POST">

  <input name="input[]" type="checkbox" value="0" id="input_0" /> Directional<br /> <br/>
  <input name="input[]" type="checkbox" value="1" id="input_1" /> Technical <br />
  <br />
  <input name="input[]" type="checkbox" value="2" id="input_2" /> Reference <br />
  <br />
  <input name="input[]" type="checkbox" value="3" id="input_3" /> Research <br />
  <br />
  <input name="input[]" type="checkbox" value="4" id="input_4" /> Phone <br /></h3>

  <input type="submit" name="button" id="button" value="Submit Tally" style="height: 25px; width: 100px">

  </Form>

  <?php
  $type = $_POST['input'];
 // $sid = $_SERVER['REMOTE_ADDR'];
  $user = $_POST['user'];

  for($i=0;$i<count($type);$i++){
    $sql = "INSERT INTO tally (tid, sid, uid, date, time, catid) 
      VALUE (NULL, 1, '$user', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, ".$type[$i].")";  
  }
  ?>
于 2014-11-30T11:27:38.197 回答