如何将动态单选按钮中的值插入数据库?

时间:2014-10-09 03:43:45

标签: php

请帮帮我..我在将单选按钮中的值插入数据库时​​遇到问题。我的代码是每行动态单选按钮。如何将值插入数据库?帮我。我是PHP编程的新手。需要专家帮助。 TQ

<?php
session_start();

$sql = new mysqli('localhost', 'root', '', 'cpsdatabase');

// Create an array to catch any errors in the registration form.
$errors = array();
if (!empty($_POST) && empty($errors))
{
       $query = "INSERT INTO answer (id, staff_id, module_id, question_id, ans) 
			VALUES(?,?,?,?,?)";
			$success = $sql->prepare($query);
			//bind parameters for markers, where (s = string, i = integer, d = double,  b = blob)
			$success->bind_param('issss', $id, $staff_id, $module_id, $question_id, $ans);
    
	if($success->execute()){
	echo '<script type="text/javascript">alert("Soalan berjaya disimpan.");</script>';
					
			}
			else{
				$errors['registration'] = "Tidak Berjatya";
				}

$success->close();
}
?>

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>

<form action="usersurvey.php" method="post">
<?php
$con=mysqli_connect("localhost","root","","cpsdatabase");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sectionid = $_SESSION['section_id'];
$result = mysqli_query($con,"SELECT * FROM question WHERE section_id='$sectionid' AND module_id='1'");

?>

<table border='3' width=900 cellpadding=3 cellspacing=1 align=center >
<tr>
<th><font size=4>Soalan</font></th>
<th><font size=4>1</font></th>
<th><font size=4>2</font></th>
<th><font size=4>3</font></th>
<th><font size=4>4</font></th>
<th><font size=4>5</font></th>
</tr>

<?php for ($i = 0; $row = mysqli_fetch_array($result); $i++) : ?>
<tr>  

<td><?=$row["question_name"];?><input type="hidden" name="question_name[]" value="<?=$row["question_name"];?>"> </div></td>
<input type="hidden" name="staff_id" id="staff_id"></td>  
<input type="hidden" name="module_id" id="module_id"></td>  
<input type="hidden" name="question_id" id="question_id"></td>  
<td><input type="radio" name="ans[<?php echo $i; ?>]" value="1"></td>  
<td><input type="radio" name="ans[<?php echo $i; ?>]" value="2"></td>  
<td><input type="radio" name="ans[<?php echo $i; ?>]" value="3"></td>
<td><input type="radio" name="ans[<?php echo $i; ?>]" value="4"></td>  
<td><input type="radio" name="ans[<?php echo $i; ?>]" value="5"></td>  
<tr>
<?php endfor; ?>

</table>

<input type="submit" name="submit" value="Submit" /><center>
</form>
<br><br>	
</tr></td>
</table></center>	

</body>
</html>

1 个答案:

答案 0 :(得分:0)

按下提交按钮后,单选按钮的值将传递到usersurvey.php。使用$_POST[ParameterName]获取帖子中的值。

if (!empty($_POST) && empty($errors)) { $id = $_POST['radiobuttonvalue1']; $staff_id = $_POST['radiobuttonvalue2']; $module_id = $_POST['radiobuttonvalue3']; $question_id = $_POST['radiobuttonvalue4']; $ans = $_POST['radiobuttonvalue5'];