Daario
Daario

Reputation: 23

How to insert value in dynamic radio button into database?

Please help me..I have problem on inserting value in radio button into database. My code is dynamic radio button per row. How can I insert the value into the database? Help me. Im new in PHP programming. Need expert help here. 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>

Upvotes: 0

Views: 1983

Answers (1)

John Ephraim Tugado
John Ephraim Tugado

Reputation: 765

After pressing the submit button the values of your radio buttons will be passed to your usersurvey.php. Use the $_POST[ParameterName] to get the values from your post.

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

Upvotes: 0

Related Questions