【问题标题】:How to insert value in dynamic radio button into database?如何将动态单选按钮中的值插入数据库?
【发布时间】:2014-11-04 13:08:33
【问题描述】:

请帮助我..我在将单选按钮中的值插入数据库时​​遇到问题。我的代码是每行的动态单选按钮。如何将值插入数据库?帮我。我是 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>

【问题讨论】:

  • 你的for循环的逻辑是什么??
  • 我需要创建调查模块,用户需要通过一个提交按钮检查这些单选按钮来完成调查。不同的用户有不同的问题。我设法根据用户角色区分问题。我的问题是现在我找不到用户如何通过勾选值为 1-5 的单选按钮来单击这些多个或动态问题行。对于您的详细信息,我需要为每行的每个问题设置 5 个单选按钮(1 到 5)。现在我设法单击每一行单选按钮,但未能将回答的值插入到数据库中的答案表中。

标签: php


【解决方案1】:

按下提交按钮后,单选按钮的值将传递给您的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'];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-12
    • 1970-01-01
    • 2022-01-22
    • 2018-09-18
    • 1970-01-01
    相关资源
    最近更新 更多