С помощью этого кода я пытаюсь создать запись на сайте. Когда я нажимаю на "создать запись" на сайте, то страница просто обновляется и ничего не добавляется. Возможна ли ошибка в SQL? Если да, то в чем может быть ошибка? Суть такая: я ввожу заголовок, выбираю изображение, выбираю категорию (topic), вставляю содержимое, выбираю active/passive и нажимаю на "Создать запись". Дело в том, что изображение загружается и я это вижу в соответствующей папке. Но остальное не добавляются в базу данных.
P.S: Я знаю, что лучше всего использовать PDO, но мне нужно использовать именно этот вариант...
function createPost($request_values)
{
global $conn, $errors, $title, $featured_image, $topic_id, $body, $published;
$title = esc($request_values['title']);
$body = htmlentities(esc($request_values['body']));
if (isset($request_values['topic_id'])) {
$topic_id = esc($request_values['topic_id']);
}
if (isset($request_values['publish'])) {
$published = esc($request_values['publish']);
}
// create slug
$post_slug = makeSlug($title);
// validate form
if (empty($title)) { array_push($errors, "Post <b>title</b> is required"); }
if (empty($body)) { array_push($errors, "Post <b>body</b> is required"); }
if (empty($topic_id)) { array_push($errors, "Post <b>category</b> is required"); }
// Get image name
$featured_image = $_FILES['featured_image']['name'];
if (empty($featured_image)) { array_push($errors, "<b>Featured image</b> is required"); }
// image file directory
$target = "../../static/images/" . basename($featured_image);
if (!move_uploaded_file($_FILES['featured_image']['tmp_name'], $target)) {
array_push($errors, "Failed to upload image. Please check file settings for your server");
}
// Ensure that no post is saved twice.
$post_check_query = "SELECT * FROM posts WHERE slug='$post_slug' LIMIT 1";
$result = mysqli_query($conn, $post_check_query);
if (mysqli_num_rows($result) > 0) { // if post exists
array_push($errors, "A post already exists with that title.");
}
// create post if there are no errors in the form
if (count($errors) == 0) {
$query = "INSERT INTO posts (user_id, title, slug, image, body, published, created_at, updated_at) VALUES(1, '$title', '$post_slug', '$featured_image', '$body', $published, now(), now())";
if(mysqli_query($conn, $query)){ // if post created successfully
$inserted_post_id = mysqli_insert_id($conn);
// create relationship between post and topic
$sql = "INSERT INTO post_topic (post_id, topic_id) VALUES($inserted_post_id, $topic_id)";
mysqli_query($conn, $sql);
$_SESSION['message'] = "Post created successfully";
header('location: posts.php');
exit(0);
}
}
}
Продвижение своими сайтами как стратегия роста и независимости