[英]How to update the database if I change the dropdown list element and checkbox value
我創建了一個表單,其中放置了一個下拉列表和一組復選框。
如果用戶從該下拉列表中選擇了某個項目,而不是應在數據庫中更新特定項目(例如,第一個用戶從下拉列表中選擇了部門A,則A應該進入數據庫,並且如果一段時間之后同一用戶將部門從A更改為B當用戶按下更新按鈕時,數據庫中的數據應該代替數據庫中的A。
在復選框組中,如果用戶更改(選擇或取消選擇)復選框與先前的輸入相比,則數據庫中的復選框值應更新。(例如,如果用戶選中了兩個復選框,則特定復選框的值應在數據庫中更新,並在一段時間后更新用戶選中了所有三個復選框或僅選中了一個復選框,並且當用戶單擊更新按鈕時,特定更改應在數據庫中更新
我在這里放了我的代碼。 如果有人比您知道解決方案,請幫助。
<?php
$connection = mysql_connect("localhost", "root", "");
$db = mysql_select_db("db_new", $connection);
$query=mysql_query("SELECT * FROM student_info where id=17");
$row= mysql_fetch_row($query);
$rollno = $row[1];
$name = $row[2];
$division= $row[3];
$std = $row[4];
$gender = $row[5];
$subject=mysql_query("select * from student_info where id=17");
while($fetch=mysql_fetch_object($subject))
{
$r=$fetch->subject;
$sub=explode(",",$r);
}
if(count($_GET)>0){
$rollno=$_GET['rollno'];
$name=$_GET['name'];
$std=$_GET['std'];
$gender=$_GET['gender'];
$update=mysql_query("update student_info set roll_no='$rollno', name='$name',
std='$std', gender='$gender' where id='17'",$connection);
}
mysql_close($connection);
?>
<!DOCTYPE html>
<html>
<head>
<title>Edit Form</title>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="jquery%20validate/jquery.validate.min.js"></script>
<script src="jquery%20validate/additional-methods.min.js"></script>
<script src="form.js"></script>
</head>
<body>
<form id="editform" method="GET" action="task.php">
<div>
<h1>Form</h1>
<label><b>Roll No:</b></label>
<input id="rollno" name="rollno" type="text" value="<?php echo $rollno ?>"> <br><br>
<label><b>Name:</b></label>
<input id="name" name="name" type="text" value="<?php echo $name ?>"> <br><br>
<label><b>Division:</b></label>
<select id="division">
<option value=" " <?php if (!empty($division) && $division == ' ') echo 'selected = "selected"'; ?>>--select--</option>
<option value="A" <?php if (!empty($division) && $division == 'A') echo 'selected = "selected"'; ?>>A</option>
<option value="B" <?php if (!empty($division) && $division == 'B') echo 'selected = "selected"'; ?>>B</option>
<option value="C" <?php if (!empty($division) && $division == 'C') echo 'selected = "selected"'; ?>>C</option>
<option value="D" <?php if (!empty($division) && $division == 'D') echo 'selected = "selected"'; ?>>D</option>
</select><br><br>
<label><b>Std:</b></label>
<input id="std" name="std" type="text" value="<?php echo $std ?>"><br><br>
<label><b>Gender:</b></label>
<input type="radio" name="gender" value="male" <?php if($gender=='male'){ echo "checked=checked";}?>>Male
<input type="radio" name="gender" value="female" <?php if($gender=='female'){ echo "checked=checked";}?>>Female<br><br>
<label><b>Subject:</b></label><br>
<input type="checkbox" name="box[]" class="subject" value="maths" <?php if (in_array("maths", $sub)){echo "checked";}?>>maths<br>
<input type="checkbox" name="box[]" class="subject" value="science" <?php if (in_array("science", $sub)){echo "checked";}?>>science<br>
<input type="checkbox" name="box[]" class="subject" value="english" <?php if (in_array("english", $sub)){echo "checked";}?>>english<br>
<input id="submit" type="submit" value="submit">
<input id="update" type="submit" value="update">
</div>
</form>
</body>
</html>
您可以使用jquery ajax在select和dropdown更改時更新數據。
$.post('page.php',{parameter:value},function(data){
//operation performed after insertion
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.