2012-03-22 60 views
-1

我有這樣的代碼: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?謝謝!許多

+0

什麼你問的是不明確 – 2012-03-22 12:01:09

回答

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

首先,你需要知道你有多少選擇提交這樣新增一個常量輸入形式:

<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注入非常重要。

0

可以遍歷$ _POST,有規律,這樣的事情匹配的密鑰:

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

記住關係型數據庫,你已經在你的桌子固定屬性的數量。所以你應該加固定的選項數。

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)"; 
    } 
} 
+0

它只廣告數據到調查表,這一點,如果我刪除的foreach {}語句 – bizkit1 2012-03-22 13:50:25

+0

這並非是準備一個真正的代碼工作 – 2012-03-22 14:02:40

+0

我需要它爲學校的項目和我真的初學者到PHP,所以我不'噸瞭解不多 – bizkit1 2012-03-22 14:22:15