Mysql更新与表单
mysql update with form
遇到麻烦,我认为我需要提供大部分代码来查看正在发生的事情。试图输出一个简单的相机表。它们作为表单的一部分存储,用户可以更改并点击应用按钮。更改应该更新数据库。问题是,当我点击应用时它不会更新任何行。例外是最后一行(列表中的最后一个相机)。
正如你所看到的,我回显了某些变量,它们看起来很好。完全困惑…
<?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>";
?>
更新查询应该是(非数字值使用单引号),
$query_insert_camera ="UPDATE `#__cameras`
SET `camera_status` ='$camera_status' WHERE `camera_id`=$camera_id";
相关文章:
- Javascript更新孙窗口中的表单元素
- 如何创建对表单的自我更新响应
- 表单文本框未自动更新
- 如何在ExtJS中用表单更新商店
- 节点.js表单更新有和没有 ajax
- 第三方(随机网站访问者)是否有可能通过 php 表单更新.html索引文件中的某些.js代码
- 使用JS/RRails和下拉表单更新URL查询字符串
- 表单更新“;重置”;按钮以获取新数据
- 通过Django/Python中的自定义表单更新用户数据
- 如何从没有数据库的另一个页面创建用HTML表单更新的页面
- Rails4:通过Ajax进行的表单更新不起作用->模板错误
- 使用jquery中的click()事件从表单更新Highchart
- 通过表单更新JSON
- 从Sencha应用程序中的表单更新服务器上的xml文件
- Gs函数传递'undefined'到jsfunction(动态表单更新)
- 不能用动态数组id从流星表单更新mongo值
- Javascript表单更新点击
- 用HTML表单更新XML
- Javascript刷新发生在子表单更新数据库之前
- Rails动态表单更新