Xin chào Trước hết, vâng, tôi biết rằng có một câu hỏi tương tự khác nhưng nó không thực sự giúp tôi Vì vậy, để làm rõ mọi thứ Đây là toàn văn lỗi vì tôi không có đủ chỗ để nhập nó vào tiêu đề của mình
Lỗi nghiêm trọng : Lỗi chưa gặp: Gọi hàm thành viên bind_param() trên bool trong C:\xampp\htdocs\movieworld\app\database\db.php:21 Dấu vết ngăn xếp: #0 C:\xampp\htdocs\movieworld\app\database\db.php(92): execQuery(‘INSERT INTO pos…’, Array) #1 C:\xampp\htdocs\movieworld\app\controllers\posts.php(77): tạo(‘posts’, Array) #2 C:\xampp\htdocs\movieworld\admin\posts\create.php(2): bao gồm(‘C:\xampp\htdocs…’) #3 {main} được ném vào C:\xampp\htdocs\movieworld\app\database\db.php trên dòng 21
Ok, vậy hãy để tôi giải thích tôi đang gặp khó khăn như thế nào mặc dù tôi có một hướng dẫn mà tôi đang theo dõi. Tôi đang theo dõi hướng dẫn trên blog của Awa Melvine (Cách tạo Blog cơ sở dữ liệu PHP & MySQL #23 | xác thực bài đăng – YouTube) nhưng vì một số lý do Khi tôi đang cố gắng đăng một cái gì đó, tôi gặp phải lỗi này và tôi không chắc tại sao và tôi hy vọng các bạn sẽ làm được
TUYÊN BỐ MIỄN TRỪ TRÁCH NHIỆM KHÔNG ĐÁNH GIÁ NHỮNG NHẬN XÉT CỦA NÓ BẰNG TIẾNG HY LẠP
đây là Db.php
<?php
session_start();
require('connect.php');
function Printer($value) // Printer
{
echo "<pre>", print_r($value, true), "</pre>";
die();
}
function executeQuery($sql, $data)
{
global $conn;
$stmt = $conn->prepare($sql);
$values = array_values($data); //pernoume tis times pou tha mpou sto stin conditions
$types = str_repeat('s', count($values)); //blepoume to idos tou value pou pirame
$stmt->bind_param($types, ...$values);//edo ginete kai me ... alla malon exo palia version
$stmt->execute();
return $stmt;
}
// epistrefi oles tis times pou tairiazoun me ta conditions sto data base table
function selectAll($table, $conditions = [])
{
global $conn;
$sql = "SELECT * FROM $table";
if (empty($conditions)) {
$stmt = $conn->prepare($sql);
$stmt->execute();
$records = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
return $records;
} else {
$i = 0;
foreach ($conditions as $key => $value) {
if ($i === 0) {
$sql = $sql . " WHERE $key=?";
} else {
$sql = $sql . " AND $key=?";
}
$i++;
}
$stmt = executeQuery($sql, $conditions);
$records = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
return $records;
}
}
//peripou to idio me epano apla pleon einai mono ena array anti gia emfoleumena
function selectOne($table, $conditions)
{
global $conn;
$sql = "SELECT * FROM $table";
$i = 0;
foreach ($conditions as $key => $value) {
if ($i === 0) {
$sql = $sql . " WHERE $key=?";
} else {
$sql = $sql . " AND $key=?";
}
$i++;
}
$sql = $sql . " LIMIT 1"; //auto einai gia na stamatisi epitopou an bri kapoio admin kai username pou tairiazi
$stmt = executeQuery($sql, $conditions);
$records = $stmt->get_result()->fetch_assoc();
return $records;
}
function create($table, $data)
{
global $conn;
// $sql = "INSERT INTO $table SET username=?, admin=?, email=?, password=?"
$sql = "INSERT INTO $table SET ";
$i = 0;
foreach ($data as $key => $value) {
if ($i === 0) {// gia auto elegxoume edo tin prwti timi
$sql = $sql . " $key=?";
} else {
$sql = $sql . ", $key=?"; //to "," edo mpaini se periptosi pou den einai i proti epologi diladi , admin=?, email=?, password=?
}
$i++;
}
$stmt = executeQuery($sql, $data);
$id = $stmt->insert_id; //etsi otan ftiaxnoume records mporoume na epistrepsoume apla to id
return $id;
}
function update($table, $id, $data)
{
global $conn;
// $sql = "UPDATE users SET username=?, admin=?, email=?, password=? WHERE id=?"
$sql = "UPDATE $table SET ";
$i = 0;
foreach ($data as $key => $value) {
if ($i === 0) {
$sql = $sql . " $key=?";
} else {
$sql = $sql . ", $key=?";
}
$i++;
}
$sql = $sql . " WHERE id=?";
$data['id'] = $id;
$stmt = executeQuery($sql, $data);
return $stmt->affected_rows;
}
function delete($table, $id)
{
global $conn;
$sql = "DELETE FROM $table WHERE id=?";
$stmt = executeQuery($sql, ['id' => $id]);
return $stmt->affected_rows;
}
function getPublishedPosts()
{
global $conn;
$sql = "SELECT p.*, u.username FROM posts AS p JOIN users AS u ON p.user_id=u.id WHERE p.published=?";
$stmt = executeQuery($sql, ['published' => 1]);
$records = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
return $records;
}
function getPostsByTopicId($topic_id)
{
global $conn;
$sql = "SELECT p.*, u.username FROM posts AS p JOIN users AS u ON p.user_id=u.id WHERE p.published=? AND topic_id=?";
$stmt = executeQuery($sql, ['published' => 1, 'topic_id' => $topic_id]);
$records = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
return $records;
}
function searchPosts($term)
{
$match="%" . $term . '%';
global $conn;
$sql = "SELECT
p.*, u.username
FROM posts AS p
JOIN users AS u
ON p.user_id=u.id
WHERE p.published=?
AND p.title LIKE ? OR p.body LIKE ?";
$stmt = executeQuery($sql, ['published' => 1, 'title' => $match, 'body' => $match]);
$records = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
return $records;
}
đó là bài đăng.php
<?php
include(ROOT_PATH . "/app/database/db.php");
include(ROOT_PATH . "/app/helpers/middleware.php");
include(ROOT_PATH . "/app/helpers/validatePost.php");
$table="posts";
$topics = selectAll('topics');
$posts = selectAll($table);
$errors = array();
$id = "";
$title = "";
$body = "";
$topic_id = "";
$published = "";
if (isset($_GET['id'])) {
$post = selectOne($table, ['id' => $_GET['id']]);
$id = $post['id'];
$title = $post['title'];
$body = $post['body'];
$topic_id = $post['topic_id'];
$published = $post['published'];
}
if (isset($_GET['delete_id'])) {
adminOnly();
$count = delete($table, $_GET['delete_id']);
$_SESSION['message'] = "Post deleted successfully";
$_SESSION['type'] = "success";
header("location: " . BASE_URL . "/admin/posts/index.php");
exit();
}
if (isset($_GET['published']) && isset($_GET['p_id'])) {
adminOnly();
$published = $_GET['published'];
$p_id = $_GET['p_id'];
$count = update($table, $p_id, ['published' => $published]);
$_SESSION['message'] = "Post published state changed!";
$_SESSION['type'] = "success";
header("location: " . BASE_URL . "/admin/posts/index.php");
exit();
}
if (isset($_POST['add-post'])) {
//adminOnly();
$errors = validatePost($_POST);
if (!empty($_FILES['image']['name'])) {
$image_name = time() . '_' . $_FILES['image']['name'];
$destination = ROOT_PATH . "/assets/images/" . $image_name;
$result = move_uploaded_file($_FILES['image']['tmp_name'], $destination);
if ($result) {
$_POST['image'] = $image_name;
} else {
array_push($errors, "Failed to upload image");
}
} else {
array_push($errors, "Post image required");
}
if (count($errors) == 0) {
unset($_POST['add-post']);
//Xrisomopoiontas to isset($_POST['add-post']) pernoume mono tis times title/image/body opote xriazete na baloume kai id kai published
$_POST['user_id'] = $_SESSION['id'];
$_POST['published'] = isset($_POST['published']) ? 1 : 0;
$_POST['body'] = htmlentities($_POST['body']); // Convert all applicable characters to HTML entities
$post_id = create($table, $_POST);
$_SESSION['message'] = "Post created successfully";
$_SESSION['type'] = "success";
header("location: " . BASE_URL . "/admin/posts/index.php");
exit();
} else {
$title = $_POST['title'];
$body = $_POST['body'];
$topic_id = $_POST['topic_id'];
$published = isset($_POST['published']) ? 1 : 0;
}
}
if (isset($_POST['update-post'])) {
adminOnly();
$errors = validatePost($_POST);
if (!empty($_FILES['image']['name'])) {
$image_name = time() . '_' . $_FILES['image']['name'];
$destination = ROOT_PATH . "/assets/images/" . $image_name;
$result = move_uploaded_file($_FILES['image']['tmp_name'], $destination);
if ($result) {
$_POST['image'] = $image_name;
} else {
array_push($errors, "Failed to upload image");
}
} else {
array_push($errors, "Post image required");
}
if (count($errors) == 0) {
$id = $_POST['id'];
unset($_POST['update-post'], $_POST['id']);
$_POST['user_id'] = $_SESSION['id'];
$_POST['published'] = isset($_POST['published']) ? 1 : 0;
$_POST['body'] = htmlentities($_POST['body']);
$post_id = update($table, $id, $_POST);
$_SESSION['message'] = "Post updated successfully";
$_SESSION['type'] = "success";
header("location: " . BASE_URL . "/admin/posts/index.php");
} else {
$title = $_POST['title'];
$body = $_POST['body'];
$topic_id = $_POST['topic_id'];
$published = isset($_POST['published']) ? 1 : 0;
}
}
và đó là tạo.php
<?php include("../../path.php"); ?>
<?php include(ROOT_PATH . "/app/controllers/posts.php");
adminOnly();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<!-- Font Awesome -->
<link rel="stylesheet"
href="https://use.fontawesome.com/releases/v5.7.2/css/all.css"
integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr"
crossorigin="anonymous">
<!-- Google Fonts Candal.ttf kai Lora.ttf -->
<link href="https://fonts.googleapis.com/css?family=Candal|Lora"
rel="stylesheet">
<!-- CSS -->
<link rel="stylesheet" href="http://forum.freecodecamp.org/../assets/css/style.css">
<link rel="stylesheet" href="http://forum.freecodecamp.org/../assets/css/admin.css">
<title>Admin Section - Add Post</title>
</head>
<body>
<?php include(ROOT_PATH . "/app/includes/adminHeader.php"); ?>
<!-- Admin Page Wrapper -->
<div class="admin-wrapper">
<?php include(ROOT_PATH . "/app/includes/adminSidebar.php"); ?>
<!-- Admin Content -->
<div class="admin-content">
<div class="button-group">
<a href="create.php" class="btn btn-big">Add Post</a>
<a href="index.php" class="btn btn-big">Manage Posts</a>
</div>
<div class="content">
<h2 class="page-title">Add Post</h2>
<?php include(ROOT_PATH . '/app/helpers/formErrors.php'); ?>
<form action="create.php" method="post" enctype="multipart/form-data">
<div>
<label>Title</label>
<input type="text" name="title" value="<?php echo $title ?>" class="text-input">
</div>
<div>
<label>Body</label>
<textarea name="body" id="body"><?php echo $body ?></textarea>
</div>
<div>
<label>Image</label>
<input type="file" name="image" class="text-input">
</div>
<div>
<label>Topic</label>
<select name="topic_id" class="text-input">
<option value=""></option>
<?php foreach ($topics as $key => $topic): ?>
<?php if (!empty($topic_id) && $topic_id == $topic['id'] ): // an o users exi epileksi sto parelthon kai to id einai to idio topic me auto sto loop tote emfanizi to selected topics ?>
<option selected value="<?php echo $topic['id'] ?>"><?php echo $topic['name'] ?></option>
<?php else: ?>
<option value="<?php echo $topic['id'] ?>"><?php echo $topic['name'] ?></option>
<?php endif; ?>
<?php endforeach; ?>
</select>
</div>
<div>
<?php if (empty($published)): ?>
<label>
<input type="checkbox" name="published">
Publish
</label>
<?php else: ?>
<label>
<input type="checkbox" name="published" checked>
Publish
</label>
<?php endif; ?>
</div>
<div>
<button type="submit" name="add-post" class="btn btn-big">Add Post</button>
</div>
</form>
</div>
</div>
<!-- Admin Content -->
</div>
<!-- Page Wrapper -->
<!-- JQuery -->
<script
src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Ckeditor -->
<script
src="https://cdn.ckeditor.com/ckeditor5/12.2.0/classic/ckeditor.js"></script>
<!-- Java Script -->
<script src="../../assets/js/scripts.js"></script>
</body>
</html>
cũng có connect.php trong trường hợp các bạn cần nó
<?php
define("DB_SERVER", "localhost");
define("DB_USER", "root");
define("DB_PASSWORD", "");
define("DB_DATABASE", "movieworld");
$conn = mysqli_connect(DB_SERVER , DB_USER, DB_PASSWORD, DB_DATABASE);
if ($conn->connect_error) {
die('Database connection error: ' . $conn->connect_error);
}