Every question have 3 answers with the score of 0, 1, or 2 assigned in the database.
How can i save each answer with the question id and answer id with the name and the email? It's about 50 questions in the questions row in the database that is displaying with this script:
<?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."; ?>
Try have a input name as Array
// 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 ....");
}
With this solutions $key row in database posts the value $i instead of question id? How can this solution work with multiple questions, since there is about 50 questions when:
echo '' . $row['que_question'] . '
Right now only 1 value is clickable of 50 questions.
See this image to understand: 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."; ?>