I want to update details in two tables in mysql, the one with the server details is updating correctly, but im struggling to update the checkbox values (try to remove or insert into the checkbox table in mysql) whereby the user selects the checkbox needed to be added or to be removed from the database.
Here is my FORM:
<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>
Here is my Update Script:
<?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();
?>
The server details is updating correctly, but the information from the checboxes from the form is not updating in their mysql table.