我正在努力使跳转表单的过程面向对象风格,因此,如果我的代码不整洁或有缺陷,请很好-在这里,我将通过jQuery传递一些帖子到一个类,以在用户选中复选框时更新记录:
这是数据库连接
class db {
private $host ;
private $username;
private $password;
private $dbname;
protected function conn()
{
$this->host = "localhost";
$this->username = "root";
$this->password = "";
$this->dbname = "mytest";
$db = new mysqli($this->host, $this->username, $this->password, $this->dbname);
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
return $db;
}
}
这是更新类
class updOrders extends db {
public $pid;
public $proc;
public function __construct()
{
$this->pid = isset($_POST['pid']) ? $_POST['pid'] : 0;
$this->proc = isset($_POST['proc']) ? $_POST['proc'] : 1;
// $stmt = $this->conn()->query("UPDATE tblorderhdr SET completed = ".$this->proc." WHERE orderid = ".$this->pid);
$stmt = $this->conn()->prepare("UPDATE tblorderhdr SET completed = ? WHERE orderid = ?");
$stmt->bind_param('ii', $this->proc, $this->pid);
$stmt->execute();
if($stmt->error)
{
$err = $stmt->error ;
} else {
$err = 'ok';
}
/* close statement */
$stmt->close();
echo json_encode($err);
}
}
$test = new updOrders;
当我注释掉prepare语句并直接(注释掉)运行查询时,它会更新,当我尝试将它作为prepare语句运行时,它会返回错误“ MySQL服务器已消失”。
非常感谢我用尽了所有帮助!
PS:本地主机上的Apache / XAMPP
最佳答案
经过对my.ini和php.ini的大量调整,但都无济于事,看来问题出在与数据库的连接内。以防万一其他人遇到相同的问题,这是(相关的部分)更新后的代码:
$db = $this->conn();
$stmt = $db->prepare("UPDATE tblorderhdr SET completed = ? WHERE orderid = ?");
$stmt->bind_param('ii', $this->proc, $this->pid);
$stmt->execute();
谁能告诉我为什么这样有效,而以前的版本却无效吗?