php将多个输入字段添加到mysql

时间:2012-03-22 11:49:17

标签: php mysql

我有这段代码:

<html>
<body>

<form id="myForm" method="post" action="add-data.php"> 
        <input type="submit">
        <input type="text" name="pollquestion"> 
        <input type="text" name="polloption1"> 
        <input type="text" name="polloption2"> 
</form> 
<a href="javascript:addOption();">Add option</a> 

<script> 
        var optionNumber = 3;
        function addOption() { 
                var theForm = document.getElementById("myForm"); 
                var newOption = document.createElement("input"); 
                newOption.name = "polloption"+optionNumber+""; // poll[optionX]
                newOption.type = "text";
                theForm.appendChild(newOption); 
                optionNumber++;
        } 
</script>
</body>
</html>

如果我添加更多输入,我会有这样的事情:

<input name="pollquestion" type="text">
<input name="polloption1" type="text">
<input name="polloption2" type="text">
<input name="polloption3" type="text">
<input name="polloption4" type="text">
<input name="polloption5" type="text">
<input name="polloption6" type="text">

php代码是这样的:

$qu = $_POST['pollquestion'];
$op1 = $_POST['polloption1'];
$op2 = $_POST['polloption2'];
$query = "INSERT into `".$db_table."` (question, option1, option2) VALUES ('" . $qu . "','" . $op1 . "','" . $op2 . "')";

如何为每个添加的行添加此数据到mysql?谢谢!

4 个答案:

答案 0 :(得分:1)

很多人的一种方式......

$query = "INSERT into `$db_table` SET `question` = '".mysql_real_escape_string($_POST['pollquestion'])."'";

foreach (range(1,6) as $idx) {
   if (!empty($_POST['polloption'.$idx])) {
      $query .= ", `option$idx` = '".mysql_real_escape_string($_POST['polloption'.$idx])."'";
   }
}

当然mysql_real_escape_string对于避免http://en.wikipedia.org/wiki/SQL_injection

非常重要

答案 1 :(得分:1)

首先,您需要知道要提交的选项数量,以便向表单添加另一个常量输入:

<input type="hidden" id="numOptions" name="numOptions"/>

addOption()函数中更新其值(在递增optionNumber之前):

document.getElementById( "numOptions" ).value = optionNumber;

在服务器端,您需要动态创建查询:

$options = array();
$values  = array();
$numOptions = intval( $_POST[ "numOptions" ] );

for ( $i = 1; $i <= $numOptions; $i++ )
{
    $options[] = "option$i";
    $values [] = "'" . mysql_real_escape_string( $_POST[ "polloption$i" ] ) . "'";
}

$query = "INSERT INTO $db_table(" . implode( ',', $options ) . ") VALUES( '" .
         implode( ',', $values );

请注意收到的字符串的转义!防止SQL注入非常重要。

答案 2 :(得分:1)

HTML

<input name="title" type="text">
<input name="descr" type="text">

<input name="question[1]" type="text">
<input name="option[1][1]" type="text">
<input name="option[1][2]" type="text">
<input name="option[1][3]" type="text">
<input name="right[1]" type="radio" value=1>
<input name="right[1]" type="radio" value=2>
<input name="right[1]" type="radio" value=3>

<input name="question[2]" type="text">
<input name="option[2][1]" type="text">
<input name="option[2][2]" type="text">
<input name="option[2][3]" type="text">
<input name="right[2]" type="radio" value=1>
<input name="right[2]" type="radio" value=2>
<input name="right[2]" type="radio" value=3>

PHP

$title = mysql_real_escape_string($_POST['title'])
$descr = mysql_real_escape_string($_POST['descr'])
$query = "INSERT into `polls` (title,descr) VALUES ('$title', '$descr')";
$id    = $db->query($query);

foreach ($_POST['question'] as $num => $q) {
  $q    = mysql_real_escape_string($q)
  $query = "INSERT into `poll questions` (poll,question) VALUES ($id,'$q')";
  $db->query($query);
  foreach ($_POST['option'][$num] as $i => $opt) {
    $right = ($_POST['right'][$num]) == $i)?1:0;
    $opt   = mysql_real_escape_string($opt)
    $num   = intval($num);
    $query = "INSERT into `poll options` (poll,num,option,right) 
             VALUES ($id,$num,'$opt',$right)";
  }
}

答案 3 :(得分:0)

您可以迭代$ _POST,将键与常规模式匹配,类似:

foreach($_POST as $key => $value) {
    preg_match('/(\w+)(\d+)/Uis', $key, $m);
    if($m[1] == 'polloption') {
        // concatenate new values to your query
    }
}

记住关系数据库,表中有固定数量的属性。所以你应该添加 fixed 选项。