0

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.

2
  • 1
    Ill be the first to say you shouldn't be using the mysql_* functions as they are depreciated. Try a PDO instead or mysqli. Commented Nov 6, 2014 at 15:22
  • 1
    You are vulnerable to sql injection attacks. Sit back and relax. Your server will be pwn3d soon and render your problem moot. Commented Nov 6, 2014 at 15:24

1 Answer 1

2

The problem is, if you do not check the checkbox, So you need to check like this:

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] . "'");
}

NOTE:

  • As the other says in the comment. Do not use mysql_* functions, they are deprecated. Use mysqli_* or PDO instead.

  • Avoid sql injections by escaping your variables in your query!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.