开发者

Multiple post data to Mysql DB

I have problem with: I want to create admin for add questions to quiz system. Structure is:

<label>Question 1</label>
<input type='text' name='question' value=''/>  

<label>Possible reply</label>
<input type='text' name='1' /> 
<input type='text' name='2' />
...
<input type='text' name='6' />    
<label>**Correct reply&开发者_高级运维lt;/label>
<input type='text' name='correct' /> 


<label>Question 2 </label>
<input type='text' name='question' value=''/>     
<label>Possible reply </label>
<input type='text' name='1' /> 
<input type='text' name='2' />
...
<input type='text' name='6' />    
<label>Correct reply </label>
<input type='text' name='correct' />     

<label>Question 3 </label>
...
<input type='submit' name='submit' value='submit'>

And I need multiple questions post to Mysql db tables: question, 1,2,3,4,5,6, correct.

I was create this:

<?php
if(isset($_POST['submit']))
 {
$question  $_POST['question '];
$a = $_POST['1'];
$b = $_POST['2'];
$c = $_POST['3'];
$d = $_POST['4'];
$e = $_POST['5'];
$f = $_POST['6'];
correct = $_POST['correct '];

$result=mysql_query("insert into test (question, 1, 2, 3, 4, 5, 6, correct) values ('$result', '$a', '$b', '$c', '$d', '$e', '$f',  '$correct' )");
  }

else
{

?>
<label>Question 1 </label>
<input type='text' name='question' value=''/>  

<label>Possible reply </label>
<input type='text' name='1' /> 
<input type='text' name='2' />
...
<input type='text' name='6' />

<label>Correct reply </label>
<input type='text' name='correct' /> 


<label>Question 2 </label>
<input type='text' name='question' value=''/>  

<label>Possible reply </label>
<input type='text' name='1' /> 
<input type='text' name='2' />
...
<input type='text' name='6' />

<label>Correct reply </label>
<input type='text' name='correct' /> 


<label>Question 3 </label>
...
<input type='submit' name='submit' value='submit'>

<?         
}

But this send only 1 question to DB.


If you can't use

<input type='text' name='question_1' value=''/>  
<input type='text' name='question_1' value=''/>  
<input type='text' name='question_1' value=''/>  

Write in HTML:

<input type='text' name='question[]' value=''/>  
<input type='text' name='reply1[]' />  
<input type='text' name='reply2[]' />  
<input type='text' name='reply3[]' />  
<input type='text' name='reply4[]' />  
<input type='text' name='reply5[]' />  
<input type='text' name='reply6[]' />  
<input type='text' name='correct[]' /> 

In PHP:

<?php
if (isset($_POST['submit'])){
    $questions=$_POST['question'];
    $reply1=$_POST['reply1'];
    $reply2=$_POST['reply2'];
    $reply3=$_POST['reply3'];
    $reply4=$_POST['reply4'];
    $reply5=$_POST['reply5'];
    $reply6=$_POST['reply6'];
    $correct=$_POST['correct'];
    foreach($questions as $key=>$value){
         $result=mysql_query("insert into test (question, 1, 2, 3, 4, 5, 6, correct) values ('$value', '".$reply1[$key]."', '".$reply2[$key]."', '".$reply3[$key]."', '".$reply4[$key]."', '".$reply5[$key]."', '".$reply5[$key]."',  '".$correct[$key]."' )");

    }

}


It would be nice if you can state your database structure and expected result, because it may not work the way you think and we'll never know until we see it.

If you want 1 answer per row, you need to use a multiple row insertion - How to insert multiple rows in single insert statement?.


try that:

<input type='text' name='question1' value=''/>  
<input type='text' name='question2' value=''/>

instead of

<input type='text' name='question1' value=''/>     
<input type='text' name='question1' value=''/>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜