Jump to content

mysql update with form


tpepernic

Recommended Posts

Having trouble where I think I need to provide most of my code to see what is happening. Trying to output a simple table of cameras. They are stored as part of a form with data filled in already.  A user can change and hit the apply button for any camera in the row. The changes should update the db for that camera only (just that one row). The problem is that when I hit apply it doesn't make the change to the row. The exception is the very last row (last camera in the list).  I tried to simplify the mysql update query to just one select (camera_status) to figure out what is going on.

 

The problem is that if I echo camera_status it is showing the wrong value. i.e. if I change the form select from Enabled to Disabled.  Something wrong with how I'm doing the POST? I appreciate any guidance.

 

<?php 
// Dont allow direct linking
defined('_JEXEC') or die('Direct Access to this location is not allowed.');
//get current user
$user =& JFactory::getUser();
// get a reference to the database
$db = &JFactory::getDBO();

if (isset($_POST['apply_changes'])) {

    //process changes to camera options     
    $camera_id = (int) $_POST['camera_id'];
    echo "CAMERA ID ".$camera_id;
    $camera_status = check_input($_POST['camera_status']);
    echo "CAMERA STATUS ".$camera_status;
    $camera_name = check_input($_POST['camera_name']);
    $camera_quality = check_input($_POST['camera_quality']);

    $query_insert_camera = 'UPDATE `#__cameras` SET `camera_status` ="'.$camera_status.'" WHERE `camera_id`='.$camera_id;
    echo "CAMERA query ".$query_insert_camera;
    $db->setQuery($query_insert_camera);
    $db->query();
}

function check_input($data, $problem='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        show_error($problem);
    }
    return $data;
}

function show_error($myError)
{
    echo $myError;
    exit();
}   

echo "<html>";
echo "<head>";

?>

<script type="text/javascript">
function oncameraSubmit(camera_id)
{
//TODO get camera name i.e.  "Apply changes to <camera name>"
  document.active_cameras.camera_id.value = camera_id;
  return confirm('Apply changes?');
}
</script>
<?php
$query_camera_name = "SELECT camera_id, camera_name, camera_status, camera_quality, camera_hash, camera_type FROM #__cameras WHERE user_id=".$user->id." AND camera_status!='DELETED'";
$db->setQuery($query_camera_name);
//get number of cameras so we can build the table accordingly
$db->query();
$num_rows = $db->getNumRows();
// We can use array names with loadAssocList.
$result_cameras = $db->loadAssocList(); 

echo "</head>";
echo "<body>";

//check if we have cameras running (basically someone logged in)
if (!isset($result_cameras))
{
    //TODO check if query failed
}
else 
{

    if ($num_rows == 0)
    {           
        echo '<b><i><center>You currently have no cameras setup.  Add a Camera below.</center></i></b>';
    }
    else
    {
        ?>
        <form name="active_cameras" action="<?php htmlentities($_SERVER['REQUEST_URI']); ?>" method="POST">
        <input type="hidden" name="camera_id" value="" />
        <table id="webcam-table">
        <thead>
            <tr>
                <th>Camera Type</th>
                <th>Name</th>
                <th>Quality</th>
                <th>Status</th>
                <th>Camera Actions</th>
            </tr>
        </thead>
        <tbody>
<?php
        for($i=0;$i<$num_rows;$i++)
        {

            //camera_status
            if ($result_cameras[$i]["camera_status"] == "ENABLED")
            {
                $enabled_option =  "value='ENABLED' selected='selected'"; 
                $disabled_option = "value='DISABLED'";
            }
            else
            {
                $enabled_option =  "value='ENABLED'";
                $disabled_option = "value='DISABLED' selected='selected'";
            }

            //camera_quality
            if ($result_cameras[$i]["camera_quality"] == "HIGH")
            {
                $high_option =  "value='HIGH' selected='selected'"; 
                $medium_option = "value='MEDIUM'";
                $mobile_option =  "value='MOBILE'";
            }
            else if ($result_cameras[$i]["camera_quality"] == "MEDIUM")
            {
                $high_option =  "value='HIGH'";
                $medium_option = "value='MEDIUM' selected='selected'";
                $mobile_option =  "value='MOBILE'";
            }
            else if ($result_cameras[$i]["camera_quality"] == "MOBILE")
            {
                $high_option =  "value='HIGH'";
                $medium_option = "value='MEDIUM'";
                $mobile_option =  "value='MOBILE' selected='selected'";
            }
            else
            {
                //TODO proper logging
            }

            //camera_type
            if ($result_cameras[$i]["camera_type"] == "WEBCAM")
            {
                $webcam =  "value='WEBCAM' selected='selected'"; 
                $axis = "value='AXIS'";
                $other =  "value='IPCAM'";
            }
            else if ($result_cameras[$i]["camera_type"] == "AXIS")
            {
                $webcam =  "value='WEBCAM'";
                $axis = "value='AXIS' selected='selected'";
                $other =  "value='IPCAM'";
            }
            else if ($result_cameras[$i]["camera_type"] == "IPCAM")
            {
                $webcam =  "value='WEBCAM'";
                $axis = "value='AXIS'";
                $other =  "value='IPCAM' selected='selected'";
            }
            else
            {
                //TODO proper logging
            }
            echo $result_cameras[$i]["camera_id"];

?> 

            <tr>
                <td>
                    <?php echo $result_cameras[$i]["camera_type"] ?>
                </td>
                <td>
                <input type="text" size="32" maxlength="64" name="camera_name" value="<?php echo $result_cameras[$i]["camera_name"]; ?>" />
                </td>
                <td>
                    <select name="camera_quality">
                    <option <?php echo $high_option; ?>>High</option>
                    <option <?php echo $medium_option; ?>>Medium</option>
                    <option <?php echo $mobile_option; ?>>Mobile</option>
                    </select>
                </td>
                <td>
                    <select name="camera_status">
                    <option <?php echo $enabled_option; ?>>Enabled</option>
                    <option <?php echo $disabled_option; ?>>Disabled</option>
                    </select>
                </td>
                <td>
                    <input type="submit" name="apply_changes" value="Apply" onClick="oncameraSubmit(<?php echo $result_cameras[$i]["camera_id"]; ?>);"/>
                       
                    <input type="submit" name="delete" value="Delete" onClick="oncameraDelete(<?php echo $result_cameras[$i]["camera_id"]; ?>);"/>
                       
                    <input type="submit" name="video" value="Launch" onClick="oncameraLaunch(<?php echo $result_cameras[$i]["camera_id"]; ?>);"/>

                </td>
            </tr>

            <?php
        }

        echo "</tbody>";
        echo "</table>";
        echo "</form>";
    }
}


echo "</body>";
echo "</html>";

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.