Mysql更新与表单

mysql update with form

本文关键字:表单 更新 Mysql      更新时间:2023-09-26

遇到麻烦,我认为我需要提供大部分代码来查看正在发生的事情。试图输出一个简单的相机表。它们作为表单的一部分存储,用户可以更改并点击应用按钮。更改应该更新数据库。问题是,当我点击应用时它不会更新任何行。例外是最后一行(列表中的最后一个相机)。

正如你所看到的,我回显了某些变量,它们看起来很好。完全困惑…

<?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"]; ?>);"/>
                    &nbsp;&nbsp;&nbsp;
                    <input type="submit" name="delete" value="Delete" onClick="oncameraDelete(<?php echo $result_cameras[$i]["camera_id"]; ?>);"/>
                    &nbsp;&nbsp;&nbsp;
                    <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>";
?>

更新查询应该是(非数字值使用单引号),

$query_insert_camera ="UPDATE `#__cameras` 
     SET `camera_status` ='$camera_status' WHERE `camera_id`=$camera_id";