每个问题都有3个答案,数据库中分配的分数为0,1或2。
如何使用问题ID和答案ID以及姓名和电子邮件保存每个答案? 在使用此脚本显示的数据库的问题行中大约有50个问题:
<?php
include "config.php";
$db = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// Testar uppkoppling:
if (mysqli_connect_errno()){
// Databaskoppling error
exit("Couldn't connect to the database: ".mysqli_connect_error());
}
$result = mysqli_query($db,"SELECT * FROM que");
echo '<form action="taemot.php" method="post" id="MyForm">';
while($row = mysqli_fetch_array($result)) {
echo '' . $row['que_question'] . '
<input type="radio" name="' . $row['que_answer0'] . '" value="' . $row['que_answer0'] . '">
<input type="radio" name="' . $row['que_answer1'] . '" value="' . $row['que_answer1'] . '">
<input type="radio" name="' . $row['que_answer2'] . '" value="' . $row['que_answer2'] . '"><br>';
}
echo ' <input type="text" name="name" value="Namn"><br>
<input type="text" name = "email" value="Epostadress"><br>
<input type="submit" value="Submit"> </form>';
?>
taemot.php
<?
$name=$_POST['name'];
$email=$_POST['email'];
mysql_connect("XXXX", "XXXX", "XXXX")
or die(mysql_error()); mysql_select_db("database") or die(mysql_error());
mysql_query("INSERT INTO `data` VALUES ('$name', '$email')");
Print "Your information has been successfully added to the database."; ?>
答案 0 :(得分:1)
尝试将输入名称作为数组
// Assign $i to define which question number is on
<input type="radio" name="ans[$i]" value="'. $row['que_answer0'] .'">
<input type="radio" name="ans[$i]" value="'. $row['que_answer1'] .'">
<input type="radio" name="ans[$i]" value="'. $row['que_answer2'] .'">
$i++;
for taemont.php
$aryAns = $_POST['ans'];
foreach( $aryAns AS $key => $value ) { //$key is the question number, $value is the question answers
mysql_query("INSERT INTO data ....");
}
答案 1 :(得分:0)
使用此解决方案,数据库中的$ key行发布值$ i而不是问题ID? 这个解决方案如何解决多个问题,因为在以下情况下大约有50个问题:
echo '' . $row['que_question'] . '
现在只有一个值可以点击50个问题。
请参阅此图片以了解:http://s29.postimg.org/d4llvjajr/questions.jpg
$result = mysqli_query($db,"SELECT * FROM que");
echo '<form action="taemot.php" method="post" id="MyForm">';
while($row = mysqli_fetch_array($result)) {
echo '' . $row['que_question'] . '
<input type="radio" name="ans[$i]" value="'. $row['que_answer0'] .'">
<input type="radio" name="ans[$i]" value="'. $row['que_answer1'] .'">
<input type="radio" name="ans[$i]" value="'. $row['que_answer2'] .'">
';$i++;
}
echo ' <input type="text" name="name" value="Namn"><br>
<input type="text" name = "email" value="Epostadress"><br>
<input type="submit" value="Submit"> </form>';
taemot.php:
$aryAns = $_POST['ans'];
foreach( $aryAns AS $key => $value ) { //$key is the question number, $value is the question answers
mysql_query("INSERT INTO `data` VALUES ('$name', '$email', '$key', '$value')");
}
Print "Your information has been successfully added to the database."; ?>