Hello! I am a 17 year old beginner Php student and I am currently creating a page for which I would like to create a blog management admin interface. I went very well, I can already add the categories to the database, through the admin interface or I can list them on the website. However, there would be a problem with creating a Blog post. Because I use the same create () function as for adding a category, but unfortunately the Blog Post does not add to the database. I don't get any php error code just when I try to list the post id after the create function. Instead of a value of 1, I get a 0. I enclose the code of the blog post and its control, as well as the data of my database!
Blog Posting form admin html and php code (ujblogposzt.php)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<?php require_once('templates/head.php') ?> <!-- includes database and user controll -->
<?php require_once('templates/sidebar.php') ?>
<?php include('app/controllers/posztok.php');?> <!-- Add new blog post controll file -->
<!DOCTYPE html>
<div class="content-wrapper">
<section class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="col-sm-6">
<h1>Új Blog Poszt</h1>
</div>
<div class="col-sm-6">
<ol class="breadcrumb float-sm-right">
<li class="breadcrumb-item"><a href="#">Home</a></li>
<li class="breadcrumb-item active">General Form</li>
</ol>
</div>
</div>
</div><!-- /.container-fluid -->
</section>
<!-- Main content -->
<section class="content">
<div class="container-fluid">
<div class="row">
<!-- left column -->
<div class="col-md-12">
<!-- general form elements -->
<div class="card card-primary">
<div class="card-header">
<h3 class="card-title">Új cikk</h3>
</div>
<!-- /.card-header -->
<!-- form start -->
<form action="ujblogposzt.php" method="post">
<div class="card-body">
<div class="form-group">
<label>Cím</label>
<input type="text" class="form-control" name="title" placeholder="Blog poszt címe..">
</div>
<div class="form-group">
<label>Leírás</label>
<input type="text" class="form-control" name="description" placeholder="Blog poszt leírása">
</div>
<div class="form-group">
<label>SEO Cím</label>
<input type="text" class="form-control" name="Seotitle" placeholder="SEO Címe...">
</div>
<div class="form-group">
<label>SEO Leírás</label>
<input type="text" class="form-control" name="Seodesc" placeholder="SEO Leírása...">
</div>
<div class="form-group">
<label>SEO Kulcsszavak</label>
<input type="text" class="form-control" name="Seokeywords" placeholder="SEO kulcsszavak...">
</div>
<div class="form-group">
<label>Kategória</label>
<select type="text" class="form-control" name="kategoria_id">
<option value=""></option>
<?php foreach ($kategoriak as $key => $kategoria): ?>
<option value="<?php echo $kategoria['id'] ?>"><?php echo $kategoria['name'] ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group">
<label>Kép</label>
<div class="input-group">
<div class="custom-file">
<input type="file" class="custom-file-input" name="image" id="exampleInputFile">
<label class="custom-file-label" for="exampleInputFile">keresés</label>
</div>
</div>
</div>
<div class="form-group">
<label>Tartalom</label>
<textarea id="mytextarea" name="text">Új Blog Poszt kezdése..</textarea>
</div>
</div>
<!-- /.card-body -->
<div class="card-footer">
<button type="submit" name="add-post" class="btn btn-primary">Poszt küldése</button>
</div>
</form>
</div>
<!-- /.card -->
</div>
<!-- /.card-body -->
</div>
<!-- /.card -->
<!--/.col (right) -->
<!-- /.row -->
</div><!-- /.container-fluid -->
</section>
<!-- /.content -->
</div>
<!-- bs-custom-file-input -->
<script src="plugins/bs-custom-file-input/bs-custom-file-input.min.js"></script>
<!-- Page specific script -->
<script>
$(function () {
bsCustomFileInput.init();
});
</script>
<!DOCTYPE html>
<script src='https://cdn.tiny.cloud/1/an8m1yl0yhyz2z87q08zevxnhs8q1zqcf71rzv9wxftm4596/tinymce/5/tinymce.min.js' referrerpolicy="origin"></script>
<script>
tinymce.init({
selector: '#mytextarea'
});
</script>
<?php require_once('templates/footer.php') ?>
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Blogpost controll code (posztok.php)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<?php
include("blogigazolas.php"); ?>
<?php
$table = 'blogposts';
$kategoriak = selectAll('blog_kategoriak');
$posts = selectAll('blogposts');
$errors = array();
if (isset($_POST['add-post'])) {
unset($_POST['add-post'], $_POST['kategoria_id']);
$_POST['user_id'] = 1;
$_POST['published'] = 1;
$postid = create($table, $_POST);
dd($postid);
}
?>
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
I see everything okay in these codes, because I add the category to the database in the same way when adding a category my function called dd is at the end of the line of code, it's just for checking, it shows results.
my create() function seen like this:
function create($table, $data)
{
global $conn;
$sql = "INSERT INTO $table SET ";
$i = 0;
foreach ($data as $key => $value) {
if ($i === 0) {
$sql = $sql . " $key=?";
} else {
$sql = $sql . ", $key=?";
}
$i++;
}
$stmt = executeQuery($sql, $data);
$id = $stmt->insert_id;
return $id;
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The strange thing is that if I add 1-2 items to the database statically without php code, it is detected and displayed in the blog editing menu, but I can't add a post to the database via the admin interface itself, that's why I wrote here ! :) From testing, I also print the end result of the create function at the end of the control code, which should return an id, but instead of id 1 I get a value of 0 and the post doesn't added to the database after pressing the post button['add-post'] and unfortunately I can't figure out why it is ?! I also attach the pictures of the problem!
The pictures of my problem can be viewed here:
https://drive.google.com/drive/folders/1WTVNv4LlbJiosWvBZoIRzjiUDkvhMgYk?usp=sharing
Thanks to anyone in advance !!!