2014-10-09 28 views
0

請幫助我..我在插入單選按鈕到數據庫中的值有問題。我的代碼是每行動態單選按鈕。我如何將值插入數據庫?幫我。我在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) 
 
\t \t \t VALUES(?,?,?,?,?)"; 
 
\t \t \t $success = $sql->prepare($query); 
 
\t \t \t //bind parameters for markers, where (s = string, i = integer, d = double, b = blob) 
 
\t \t \t $success->bind_param('issss', $id, $staff_id, $module_id, $question_id, $ans); 
 
    
 
\t if($success->execute()){ 
 
\t echo '<script type="text/javascript">alert("Soalan berjaya disimpan.");</script>'; 
 
\t \t \t \t \t 
 
\t \t \t } 
 
\t \t \t else{ 
 
\t \t \t \t $errors['registration'] = "Tidak Berjatya"; 
 
\t \t \t \t } 
 

 
$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> \t 
 
</tr></td> 
 
</table></center> \t 
 

 
</body> 
 
</html>

+0

什麼是你的for循環的邏輯? – 2014-10-09 03:55:30

+0

我需要創建調查模塊,用戶需要通過使用一個提交按鈕選中這些單選按鈕來完成調查。不同的用戶有不同的問題。我設法區分基於用戶角色的問題。我現在的問題是我找不到用戶如何通過勾選具有1-5值的單選按鈕來單擊這些多個或動態問題行。對於你的細節,我需要每個問題每行5個單選按鈕(1到5)。現在,我設法點擊每一行單選按鈕,但未能將回答的值插入數據庫的答案表中。 – Daario 2014-10-09 04:04:40

回答

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'];

相關問題