php jQuery MySQL 更新某一行数据

soldierNa 2016-06-12 09:39:37
目标: 机房有多台工作站台,为了方便学生查看可用资源、合理分配机器的使用,想在机房的服务器上创建一网 页,来显示工作站使用相信息。

开发环境: 操作系统win 7 32位
采用WAMP,其版本为2.4,对应的PHP版本为 5.4.16 MySQL为 5.6.12 ,采用MySQL Workbench创建数据库。

问题1: 为了对表格进行操作,使用了jQuery,现在可以返回某一列,但是不会返回当前行? 采用update来更交数据库,想通过where id=行号或是键值 来更新当前单元格的内容,代码可见workstation.php。

问题2: 在服务器上可编辑表格,但使用其它机器访问该网页,表格不可编辑,不知这个是什么原因?

主页面代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>强我国防,神采飞扬!</title>
</head>
<body bgcolor="#FFFFFF">
<p><strong>欢迎加入B104大家庭 </strong></p>
<p>强我国防,神采飞扬! </p>
<p> <a href="workstation/workstation.php" title="工作站使用情况">工作站使用情况</a></strong></span></p>
</body>
</html>


工作站使用情况统计文件workstation.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>机房工作站使用统计</title>
<style type="text/css" media="all">
@import url("css/style.css");

</style>
<script type="text/javascript" src="js/jquery-1.2.2.js"></script>
<script type="text/javascript" src="js/jquery.jeditable.js"></script>

<script type="text/javascript">
$(function(){
$('.edit').editable('save.php',
{
width :120,
height :18,
cancel : '取消',
submit : '确定',
indicator : '<img src="css/loader.gif">',
tooltip : '单击可以编辑...',
});


})

</script>
</head>



<body>

<p><strong>机房工作站使用统计 </strong></p>
<p> </p>
<p>
更改记录后,请按F5进行更新!
</p>
<p>目前机房工作站总核数为100核,当前已使用*核,还有*核可用。</p>

<?php
$mysqli=new mysqli('127.0.0.1','root','ngvjai','workstation');
if($mysqli->connect_error)
{
die('Connect Error ('.$mysqli->connect_errno.')'.$mysqli->connect_error);
}
/*else
{
echo '数据库连接成功'."<br>"."<br>";
}*/
$mysqli->query("SET NAMES utf8");


//操作数据库
$sql="select * from threadstatistics";
$res=$mysqli->query($sql);

//使用表格格式化数据
echo "<table width=800 border=1>";
echo "<tr align=center><td>编号</td><td>机器IP</td><td>总核数</td><td>姓名</td>><td>占用核数</td>><td>何时占用</td></tr>";

while($row=$res->fetch_row()) //遍历SQL语句执行结果把值赋给数组
{
?>
<tr>
<td align="center" > <?php echo $row[0];?></td>
<td align="center" class="edit" id="IP"><?php echo $row[1];?></td>
<td align="center" class="edit" id="TotalThreads"><?php echo $row[2];?></td>
<td align="center" class="edit" id="name"><?php echo $row[3];?></td>
<td align="center" class="edit" id="threadsUsed"><?php echo $row[4];?></td>
<td align="center" id="beginTime"><?php echo $row[5];?></td>
</tr>
<?php
}
echo "</table><br/>";

//关闭资源
$res->free();//释放内存
$mysqli->close();//关闭连接
?>
</body>
</html>



数据库连接connect.php


<?php
$host = 'localhost';
$db_user = 'root';
$db_pass = 'ngvjai';
$db_name = 'workstation';

$mysqli=new mysqli($host,$db_user,$db_pass);

if($mysqli->connect_error)
{
die('Connect Error ('.$mysqli->connect_errno.')'.$mysqli->connect_error);
}
else
{
//echo '数据库连接成功'."<br>"."<br>";
//连接数据库
$mysqli->select_db('workstation');
//设置字符集
$mysqli->query("SET NAMES utf8");
}

?>



数据保存文件save.php

<?php
//连接数据库
include_once("connect.php");

//将修改值存入数据库
//---获取当前列名
$field=$_POST['id'];

//---获取前端提交的字段对应的内容
$val=$_POST['value'];

if ($field=='name')
$sql="update threadstatistics set $field='$val', beginTime=NOW() where id =12";
else
$sql="update threadstatistics set $field=$val, beginTime=NOW() where id =12";


$results=$mysqli->query($sql) or die("数据表threadstatistics 修改记录失败!".mysql_error());

if($results)
{
echo $val;
}
else
{
echo "数据出错";
}


$mysqli->close();
?>




workstation.sql数据库文件内容:

CREATE DATABASE IF NOT EXISTS `workstation` /*!40100 DEFAULT CHARACTER SET gbk */;
USE `workstation`;
-- MySQL dump 10.13 Distrib 5.6.13, for Win32 (x86)
--
-- Host: 127.0.0.1 Database: workstation
-- ------------------------------------------------------
-- Server version 5.6.12-log

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `threadstatistics`
--

DROP TABLE IF EXISTS `threadstatistics`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `threadstatistics` (
`ID` int(11) NOT NULL,
`IP` int(11) DEFAULT NULL,
`TotalThreads` int(11) DEFAULT NULL,
`name` varchar(200) DEFAULT NULL,
`threadsUsed` int(11) DEFAULT NULL,
`beginTime` datetime DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `threadstatistics`
--

LOCK TABLES `threadstatistics` WRITE;
/*!40000 ALTER TABLE `threadstatistics` DISABLE KEYS */;
INSERT INTO `threadstatistics` VALUES (1,582,120,'张三',230,'2016-06-12 11:44:06'),(2,582,120,'李四',230,'2016-06-12 11:44:06'),(3,582,120,'呆蛙',230,'2016-06-12 11:44:06'),(4,582,120,'小七',230,'2016-06-12 11:44:06'),(5,582,120,NULL,230,'2016-06-12 11:44:06'),(6,582,120,NULL,230,'2016-06-12 11:44:06'),(7,582,120,NULL,230,'2016-06-12 11:44:06'),(8,582,120,NULL,230,'2016-06-12 11:44:06'),(9,582,220,NULL,2030,'2016-06-12 11:44:06'),(10,582,120,NULL,2230,'2016-06-12 11:44:06'),(11,582,120,NULL,230,'2016-06-12 11:44:06'),(12,582,120,NULL,230,'2016-06-12 17:50:25'),(13,582,120,NULL,230,'2016-06-12 11:44:06'),(14,582,120,NULL,230,'2016-06-12 11:44:06'),(15,582,120,NULL,230,'2016-06-12 11:44:06'),(16,582,120,NULL,230,'2016-06-12 11:44:06'),(17,582,120,NULL,230,'2016-06-12 11:44:06'),(18,582,120,NULL,230,'2016-06-12 11:44:06'),(19,582,120,NULL,230,'2016-06-12 11:44:06'),(20,582,120,NULL,230,'2016-06-12 11:44:06'),(21,582,120,NULL,230,'2016-06-12 11:44:06'),(22,582,120,NULL,230,'2016-06-12 11:44:06'),(23,582,120,NULL,230,'2016-06-12 11:44:06'),(24,582,120,'',230,'2016-06-12 11:44:06'),(25,582,120,NULL,230,'2016-06-12 11:44:06'),(26,582,120,NULL,230,'2016-06-12 11:44:06'),(27,582,120,NULL,230,'2016-06-12 11:44:06'),(28,582,120,NULL,230,'2016-06-12 11:44:06'),(29,582,120,NULL,230,'2016-06-12 11:44:06'),(30,582,120,NULL,230,'2016-06-12 11:44:06');
/*!40000 ALTER TABLE `threadstatistics` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2016-06-12 17:54:41



...全文
1131 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
e4lich1985 2016-06-24
  • 打赏
  • 举报
回复
js报错了,看下js错误吧
soldierNa 2016-06-12
  • 打赏
  • 举报
回复
现在的效果:



在服务器上可以编辑
采用其他电脑访问,就不能编辑了

4,251

社区成员

发帖
与我相关
我的任务
社区描述
国内外优秀PHP框架讨论学习
社区管理员
  • Framework
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧