私はlvl1itquepaperからデータを取得し、4ラジオボタンでそれを表示し、別のテーブル呼び出しlvl1itresultにユーザ入力を保存しますが、私が選択したオプション生徒が他のオプションを選択しても、データベースに保存されるのはオプション4です。このためphantのラジオボタンからユーザ入力を保存する
<?php
include('../dbconnect.php');
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Online Examination System</title>
</head>
<body>
<div id="container">
<h1>Level 1 IT Question Paper</h1>
<h2>Please read the question carefully and answer it confidently. Good Luck All!</h2>
<?php
if(isset($_POST['Submit']))
{
$sql="SELECT * from lvl1itquepaper";
$run_que = mysqli_query($mysqli, $sql);
$check_que = mysqli_num_rows($run_que);
while ($row=$run_que->fetch_assoc())
{
$questionno = $row['questionno'];
$question = $row['question'];
$option1 = $row['option1'];
$option2 = $row['option2'];
$option3 = $row['option3'];
$option4 = $row['option4'];
$ans_array = array($option1, $option2, $option3, $option4);
$student_ans = $row['option1'];
$student_ans = $row['option2'];
$student_ans = $row['option3'];
$student_ans = $row['option4'];
$sql="Insert into lvl1itresult (questionno, question, studentans, username) values ('.$questionno.', '$question', '$student_ans', '".$_SESSION['login_user']."')";
$submit = $mysqli->query($sql);
}
}
?>
<form method= "post">
<?php
echo "Welcome, ";
$sql="SELECT * from lvl1itstudent WHERE username= '".$_SESSION['login_user']."'";
$find_student = mysqli_query($mysqli, $sql);
$check_student = mysqli_num_rows($find_student);
if ($check_student>0){
while($row = $find_student->fetch_assoc())
{
echo $row['username'];
}
}
echo "<br><br><br><br>";
$sql="SELECT * from lvl1itquepaper";
$run_que = mysqli_query($mysqli, $sql);
$check_que = mysqli_num_rows($run_que);
if($check_que>0){
while ($row=$run_que->fetch_assoc())
{
$questionno = $row['questionno'];
$question = $row['question'];
$option1 = $row['option1'];
$option2 = $row['option2'];
$option3 = $row['option3'];
$option4 = $row['option4'];
$ans_array = array($option1, $option2, $option3, $option4);
shuffle($ans_array);
echo "".$questionno. "." .$question."<br>";
echo "<input type='radio' name='.$questionno.' value='".$ans_array[0]."'>".$ans_array[0]."<br>";
echo "<input type='radio' name='.$questionno.' value='".$ans_array[1]."'>".$ans_array[1]."<br>";
echo "<input type='radio' name='.$questionno.' value='".$ans_array[2]."'>".$ans_array[2]."<br>";
echo "<input type='radio' name='.$questionno.' value='".$ans_array[3]."'>".$ans_array[3]."<br><br>";
}
}
else {
echo "there is no data in database";
}
?>
<input type="submit" value = "Submit" name= "Submit" style= "width:60px; height:30px";>
</form>
</div>
</body>
**警告**:mysqliを使用する場合は、[パラメータ化されたクエリ](http://php.net/manual/en/mys qli.quickstart.prepared-statements.php)と['bind_param'](http://php.net/manual/en/mysqli-stmt.bind-param.php)を使ってクエリにユーザデータを追加します。 **重大な[SQLインジェクションのバグ](http://bobby-tables.com/)を作成したため、文字列の補間または連結を使用してこれを実行しないでください。 ** '$ _POST'、' $ _GET'、**任意の**ユーザデータを直接クエリーに入れないでください。誰かがあなたのミスを悪用しようとすると非常に危険です。 – tadman
質問には複数のオプションがありますが、ループ中に複数の質問がありますか? – pAsh
ya lvl1itquepaperから取得する質問と4つのオプション(4つのラジオボタン)で各質問があります。 – sim