我有一个类,该类在调用该类时使用__construct创建连接。这是在其自己的单独文件中。
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
class db{
public $dsn = "mysql:host=localhost;dbname=cms";
public $user = "root";
public $pass = 1234;
public $link;
private function connect(){
$this->link = new PDO($this->dsn,$this->user,$this->pass);
}
public function __construct(){
$this->connect();
}
}
?>
在以下代码中,我接受了一些用户输入,并尝试使用pdo准备好的语句将其插入。当我点击提交时什么也没发生。 db表中没有错误,也没有数据。
<?php
include "includes/admin_header.php";
include "includes/admin_nav.php";
include "includes/functions.php";
if(isset($_POST["add_post"])){
$id = $_POST["post_category_id"];
$title = $_POST["title"];
$author = $_POST["post_author"];
$image = $_FILES["post_image"]["name"];
$temp_image = $_FILES["post_image"]["tmp_name"];
$content = $_POST["post_content"];
$comment_count = 5;
$tags = $_POST["post_tags"];
$status = $_POST["post_status"];
move_uploaded_file($temp_image,"images/$image");
$sql = "INSERT INTO posts(post_catagory_id,post_title,post_author,post_image,post_content,post_comment_count,post_tags,post_status)
VALUES(:a,:b,:c,:d,:e,:f,:g,:h)";
$stmt = $connect->link->prepare($sql);
$stmt->bindvalue(":a",$id);
$stmt->bindvalue(":b",$title);
$stmt->bindvalue(":c",$author);
$stmt->bindvalue(":d",$image);
$stmt->bindvalue(":e",$content);
$stmt->bindvalue(":f",$comment_count);
$stmt->bindvalue(":g",$tags);
$stmt->bindvalue(":h",$status);
$stmt->execute();
}
?>
db()类文件在包含的标题中链接。该类在包含的functions.php中被调用。任何人都可以帮助从mysql获取正确的错误吗?
编辑:我在执行后添加了以下if语句,以查看是否可以掌握它
if($stmt){echo "works";}else{echo "something went wrong";}
由于某种原因,它回显了“工作”,但是我没有在数据库中获取任何数据。有人能发现问题吗?