我想更新mysql中两个表的详细信息,服务器详细信息正在更新,但我很难更新复选框值(尝试删除或插入mysql中的复选框表),用户选择复选框需要添加或从数据库中删除。
这是我的表格:
<form role="form" method="post" action="update_serv.php" >
<div class="form-group">
<label>Server IP:</label>
<input class="form-control" id="ip" name="ip" value="<?php echo $server_ip;?>" required>
<p class="help-block">Example 192.168.100.12</p>
</div>
<div class="form-group">
<label>Server Name:</label>
<input class="form-control" id="na" name="na" value="<?php echo $server_name;?>" required>
<p class="help-block">Example Email Server</p>
</div>
<div class="form-group">
<label>Server Description:</label>
<input class="form-control" id="de" name="de" value="<?php echo $server_description;?>" required>
</div>
<div class="form-group">
<label>Server Ports:</label>
<label class="checkbox-inline" id="po">
<input type="checkbox" value="None" >None
</label>
<?php
// Get Server Information
$query = "SELECT port_no FROM _servers WHERE (server_id = '$servid') ";
$result = mysql_query($query)or die ('Unable to run query:'.mysql_error());
while($row = mysql_fetch_assoc($result)){
$sport_no = $row['port_no'];
}
$query = "SELECT id, name, port_no FROM ports ORDER BY name ASC";
$result = mysql_query($query)or die ('Unable to run query:'.mysql_error());
while($row = mysql_fetch_assoc($result)){
$port_id = $row['id'];
$port_no = $row['port_no'];
$port_name = $row['name'];
?>
<label class="checkbox-inline"><input type="checkbox" name="po[]" type="checkbox" id="po[]" value="<?php echo $row['port_no'] ;?>" <?php if ($port_no == $sport_no) {
echo 'checked';
}?>><?php echo $row['port_no'] ;?> (<?php echo $row['name'] ;?>)</label>
<?php
}
?>
</div>
<input type="hidden" name="u" id="u" value="<?php echo $servid;?>" />
<button type="submit" class="btn btn-success">Update Server</button>
</form>
这是我的更新脚本:
<?php
// Update Server Details
// Get Server ID
$servid = $_POST['u'];
// Get Server IP
$servip = $_POST['ip'];
// Get Server NAME
$servname = $_POST['na'];
// Get Server DESCRIPTION;
$servdescription = $_POST['de'];
// Get Server PORTS
$servports = $_POST['po'];
// Include Configuration File
include ("_data/_conf.php");
///// Update Server Details
$sql="UPDATE servers SET server_ip = '$servip', server_name = '$servname', server_description = '$servdescription' WHERE unique_id = '$servid'";
$result=mysql_query($sql)or die ('Unable to run query:'.mysql_error());
///// Update Ports in Table _servers
//for ($i=0; $i<sizeof($servports);$i++) {
//$sql="UPDATE _servers SET server_name = '$servname', server_ip = '$servip', port_no = ".$servports[$i]." WHERE server_id = '$servid'";
//$result=mysql_query($sql)or die ('Unable to run query:'.mysql_error());
$portCount = count($_POST["po"]);
for($i=0;$i<$portCount;$i++) {
mysql_query("UPDATE _servers SET server_name='" . $_POST["na"][$i] . "', server_ip='" . $_POST["ip"][$i] . "', server_id='" . $_POST["u"][$i] . "', port_no='" . $_POST["po"][$i] . "' WHERE server_id='" . $_POST["u"][$i] . "'");
}
// }
header('Location: u_serv.php?u='.$servid.'&updated=true');
exit();
?>
服务器详细信息正在正确更新,但来自表单中的checbox的信息未在其mysql表中更新。
答案 0 :(得分:2)
问题是,如果你没有选中复选框,那么你需要像这样检查:
for ($i = 0; $i < $portCount; $i++) {
$checked = 0;
if (!empty($_POST["po"][$i])) {
$checked = 1;
}
mysql_query("UPDATE _servers SET server_name='" . $_POST["na"][$i] . "', server_ip='" . $_POST["ip"][$i] . "', server_id='" . $_POST["u"][$i] . "', port_no='" . $checked . "' WHERE server_id='" . $_POST["u"][$i] . "'");
}
注意:强>
正如对方在评论中所说。不要使用mysql_ *函数,不推荐使用它们。请改用mysqli_ *或PDO。
通过在查询中转义变量来避免sql注入!