我想通过添加多个主题来更新主题和其他字段,但问题是它只保存了我检查过的主题之一。
我该如何解决这个问题?
以下是我的代码:
<?php
$status = "";
if(isset($_POST['new']) && $_POST['new']==1)
{
$host="localhost";//host name
$username="root"; //database username
$word="";//database word
$db_name="tuichk";//database name
$tbl_name="data"; //table name
$con=mysqli_connect("$host", "$username", "$word","$db_name")or die("cannot connect");//connection string
$id=$_REQUEST['id'];
$name =$_REQUEST['name'];
$stu_ic = $_REQUEST['stu_ic'];
$address = $_REQUEST['address'];
$contact = $_REQUEST['contact'];
$checkbox1=$_REQUEST['subject'];
$chk="";
$update="update data set name='".$name."', stu_ic='".$stu_ic."', address='".$address."', contact='".$contact."', sub='".$checkbox1."' where id='".$id."'";
mysql_query($update) or die(mysql_error());
$status = "Record Updated Successfully. </br></br><a href='view.php'>View Updated Record</a>";
echo '<p style="color:#FF0000;">'.$status.'</p>';
}else {
?>
这是我的html表单
<form name="form" method="post" action="">
<input type="hidden" name="new" value="1" />
<input name="id" type="hidden" value="<?php echo $row['id'];?>" />
<p><input type="text" name="name" placeholder="Enter Name" required value="<?php echo $row['name'];?>" /><input type="text" name="stu_ic" placeholder="Enter Student IC" required value="<?php echo $row['stu_ic'];?>" /></p>
<p><input type="text" name="address" placeholder="Enter Address" required value="<?php echo $row['address'];?>" /><input type="text" name="contact" placeholder="Enter Contact" required value="<?php echo $row['contact'];?>" /></p>
<div style="text-align:center">
<div style="width:400px;border-radius:6px;margin:0px auto">
<table border="1">
<tr>
<td colspan="2">Select Subject:</td>
</tr>
<tr>
<td>Bahasa Melayu</td>
<td><input type="checkbox" name="subject" value="Bahasa Melayu"></td>
</tr>
<tr>
<td>English</td>
<td><input type="checkbox" name="subject" value="English"></td>
</tr>
<tr>
<td>Mathematics</td>
<td><input type="checkbox" name="subject" value="Mathematics"></td>
</tr>
<tr>
<td>Science</td>
<td><input type="checkbox" name="subject" value="Science"></td>
</tr>
<tr>
<td>Sejarah</td>
<td><input type="checkbox" name="subject" value="Sejarah"></td>
</tr>
<tr>
<td>Geography</td>
<td><input type="checkbox" name="subject" value="Geography"></td>
</tr>
<tr>
<td>Additional Mathematics</td>
<td><input type="checkbox" name="subject" value="Additional Mathematics"></td>
</tr>
<tr>
<td>Chemistry</td>
<td><input type="checkbox" name="subject" value="Chemistry"></td>
</tr>
<tr>
<td>Physics</td>
<td><input type="checkbox" name="subject" value="Physics"></td>
</tr>
<tr>
<td>Biology</td>
<td><input type="checkbox" name="subject" value="Biology"></td>
</tr><tr>
<td>Principle Of Accounting</td>
<td><input type="checkbox" name="subject" value="Principle Of Accounting"></td>
</tr><tr>
<td>Ekonomi Asas</td>
<td><input type="checkbox" name="subject" value="Ekonomi Asas"></td>
</tr><tr>
<td>Perdagangan</td>
<td><input type="checkbox" name="subject" value="Perdagangan"></td>
</tr>
</table>
</div>
</form>
答案 0 :(得分:1)
HTML输入应为
<input type="checkbox" name="subject[]" value="Subject1">
<input type="checkbox" name="subject[]" value="Subject2">
在PHP中有很多选择。
选项1:
将主题保存为字符串
$subjects = implode(',', $_POST['subject']);
检索为字符串并转换为数组
$subjects = explode(',', $field);
选项2:可以保存为JSON并检索为JSON并对其进行解码。
答案 1 :(得分:1)
这是我的代码inc html形式:
<?php
$status = "";
if(isset($_POST['new']) && $_POST['new']==1)
{
$host="localhost";//host name
$username="root"; //database username
$word="";//database word
$db_name="tuichk";//database name
$tbl_name="data"; //table name
$con=mysqli_connect("$host", "$username", "$word","$db_name")or die("cannot connect");//connection string
$id=$_REQUEST['id'];
$name =$_REQUEST['name'];
$stu_ic = $_REQUEST['stu_ic'];
$address = $_REQUEST['address'];
$contact = $_REQUEST['contact'];
$checkbox1=$_REQUEST['subject'];
$subjects = implode(',', $_POST['subject']);
$subjects = explode(',', $field);
$chk="";
$update="update data set name='".$name."', stu_ic='".$stu_ic."', address='".$address."', contact='".$contact."', sub='".$checkbox1."' where id='".$id."'";
mysql_query($update) or die(mysql_error());
$status = "Record Updated Successfully. </br></br><a href='view.php'>View Updated Record</a>";
echo '<p style="color:#FF0000;">'.$status.'</p>';
}else {
?>
<div>
<form name="form" method="post" action="">
<input type="hidden" name="new" value="1" />
<input name="id" type="hidden" value="<?php echo $row['id'];?>" />
<p><input type="text" name="name" placeholder="Enter Name" required value="<?php echo $row['name'];?>" /><input type="text" name="stu_ic" placeholder="Enter Student IC" required value="<?php echo $row['stu_ic'];?>" /></p>
<p><input type="text" name="address" placeholder="Enter Address" required value="<?php echo $row['address'];?>" /><input type="text" name="contact" placeholder="Enter Contact" required value="<?php echo $row['contact'];?>" /></p>
<div style="text-align:center">
<div style="width:400px;border-radius:6px;margin:0px auto">
<table border="1">
<tr>
<td colspan="2">Select Subject:</td>
</tr>
<tr>
<td>Bahasa Melayu</td>
<td><input type="checkbox" name="subject[]" value="Bahasa Melayu"></td>
</tr>
<tr>
<td>English</td>
<td><input type="checkbox" name="subject[]" value="English"></td>
</tr>
<tr>
<td>Mathematics</td>
<td><input type="checkbox" name="subject[]" value="Mathematics"></td>
</tr>
<tr>
<td>Science</td>
<td><input type="checkbox" name="subject[]" value="Science"></td>
</tr>
<tr>
<td>Sejarah</td>
<td><input type="checkbox" name="subject[]" value="Sejarah"></td>
</tr>
<tr>
<td>Geography</td>
<td><input type="checkbox" name="subject[]" value="Geography"></td>
</tr>
<tr>
<td>Additional Mathematics</td>
<td><input type="checkbox" name="subject[]" value="Additional Mathematics"></td>
</tr>
<tr>
<td>Chemistry</td>
<td><input type="checkbox" name="subject[]" value="Chemistry"></td>
</tr>
<tr>
<td>Physics</td>
<td><input type="checkbox" name="subject[]" value="Physics"></td>
</tr>
<tr>
<td>Biology</td>
<td><input type="checkbox" name="subject[]" value="Biology"></td>
</tr><tr>
<td>Principle Of Accounting</td>
<td><input type="checkbox" name="subject[]" value="Principle Of Accounting"></td>
</tr><tr>
<td>Ekonomi Asas</td>
<td><input type="checkbox" name="subject[]" value="Ekonomi Asas"></td>
</tr><tr>
<td>Perdagangan</td>
<td><input type="checkbox" name="subject[]" value="Perdagangan"></td>
</tr>
</table>
</div>
</form>
<p><input name="submit" type="submit" value="Update" /></p>
</form>
<?php } ?>