I want to implement the functionality of likes and dislikes in my project. But when I start it, it shows me an error:
Uncaught mysqli_sql_exception: Field ‘likes’ doesn’t have a default value in E:OSPaneldomainslocalhostTrynditterindex.php:22 Stack trace: #0 E:OSPaneldomainslocalhostTrynditterindex.php(22): mysqli_query(Object(mysqli), ‘INSERT INTO pos…’) #1 {main} thrown in E:OSPaneldomainslocalhostTrynditterindex.php on line 22
I used: HTML,PHP,MySQL,JS,JQuery.
I took the line: mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
Then the error disappears, but the rest of the functionality stops working.
index.php
<?php require_once "header.php"; ?>
<html>
<body>
<?php
//if (isset($_SESSION["user_login"])) echo 'user_login_Isset';
$edit_id=$_GET['edit'];
$text_for_edit = $_GET['text'];
$Posts_Text = $_POST['post_text'];
$user_login = $_SESSION["user_login"];
$likes =$_Get['likes'];
if(isset($_GET['logout'])) //exit session
{
unset($_SESSION["user_login"]);
header("location:index.php");
}
if(isset($_POST['post_text']) && (!isset($_GET['edit'])) ) //ноий tweet
{
$sql = "INSERT INTO posts (posts_text, posts_date, user_login) VALUES('$Posts_Text',now(), '$user_login')";
$result = mysqli_query($con,$sql);
if($sql){
header("location:index.php");
}
}
if(isset($_POST['post_text']) && (isset($_GET['edit'])) ) //редагування
{
$sql = "UPDATE posts SET posts_text='$Posts_Text' WHERE posts_id=$edit_id";
$result = mysqli_query($con,$sql);
if($sql){
header("location:index.php");
}
}
if(isset($_GET['del'])) //Видалення
{
$Del_ID = $_GET['del'];
$sql="delete from posts where posts_id='$Del_ID'";
$post_query = mysqli_query($con,$sql);
if($sql){
header("location:index.php");
}
}
?>
<div class="grid-container">
<?php require_once "left-sidebar.php";?>
<div class="main">
<p class = "page_title"><?php if (isset($_GET['edit'])) echo 'Editing mode'; else echo 'Trynditter'; ?></p>
<div class="tweet__box tweet__add" <?php if (!isset($_SESSION["user_login"])) echo 'hidden';?> >
<div class="tweet_left">
</div>
<div class="tweet__body">
<form method="post" enctype="multipart/form-data">
<textarea name="post_text" id="" cols="100%" rows="3" placeholder='What is happening?'><?php echo $text_for_edit; ?></textarea>
<?php while ($row = mysqli_fetch_array($posts_text)) { ?>
<div class="post">
<?php echo $row['text']; ?>
<div style="padding: 2px; margin-top: 5px;">
<?php
// determine if user has already liked this post
$query = mysqli_query($con, "SELECT * FROM likes WHERE user_id=1 AND post_id=".$row['id']."");
if (mysqli_num_rows($query) == 1 ): ?>
<!-- user already likes post -->
<span class="unlike fa fa-thumbs-up" data-id="<?php echo $row['id']; ?>"></span>
<span class="like hide fa fa-thumbs-o-up" data-id="<?php echo $row['id']; ?>"></span>
<?php else: ?>
<!-- user has not yet liked post -->
<span class="like fa fa-thumbs-o-up" data-id="<?php echo $row['id']; ?>"></span>
<span class="unlike hide fa fa-thumbs-up" data-id="<?php echo $row['id']; ?>"></span>
<?php endif ?>
<span class="likes_count"><?php echo $row['likes']; ?> likes</span>
</div>
</div>
<?php } ?>
<button class="button__tweet" type="submit" name="btn_add_post">Tryndit</button>
</div>
</form>
</div>
</div>
<?php require_once "tweet.php" ?>
</div>
</div>
<?php require_once "right-sidebar.php";?>
<script src="jquery.min.js"></script>
<script>
$(document).ready(function(){
// when the user clicks on like
$('.like').on('click', function(){
var postid = $(this).data('id');
$post = $(this);
$.ajax({
url: 'index.php',
type: 'post',
data: {
'liked': 1,
'post_id': postid
},
success: function(response){
$post.parent().find('span.likes_count').text(response + " likes");
$post.addClass('hide');
$post.siblings().removeClass('hide');
}
});
});
// when the user clicks on unlike
$('.unlike').on('click', function(){
var post_id = $(this).data('id');
$post = $(this);
$.ajax({
url: 'index.php',
type: 'post',
data: {
'unliked': 1,
'post_id': post_id
},
success: function(response){
$post.parent().find('span.likes_count').text(response + " likes");
$post.addClass('hide');
$post.siblings().removeClass('hide');
}
});
});
});
</script>
</body>
</html>
db.php
<?php
error_reporting(E_ERROR | E_PARSE);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$con = mysqli_connect("localhost","root","","twitter");
$select_db=mysqli_select_db($con,'twitter');
if(!con){
die("Not Connected");
}
?>
header.php
<?php
ob_start();
session_start();
require_once('db.php');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Trynditter</title>
<link rel="shortcut icon" href="imagesTrynditter.jpg" type="image/jpg">
<link href="cssstyle.css" rel="stylesheet" type="text/css">
<link href="csstweet-box.css" rel="stylesheet" type="text/css">
<link href="cssright-sidebar.css" rel="stylesheet" type="text/css">
<link href="cssleft-sidebar.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />
</head>
<body>
</body>
</html>
MySQL
Структура таблиці `likes`
--
CREATE TABLE `likes` (
`id` int NOT NULL,
`user_id` int NOT NULL,
`post_id` int NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
-- --------------------------------------------------------
--
-- Структура таблиці `posts`
--
CREATE TABLE `posts` (
`posts_id` int NOT NULL,
`posts_text` varchar(5000) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci NOT NULL,
`posts_date` date NOT NULL,
`user_login` varchar(20) NOT NULL,
`likes` int NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
--
-- Дамп даних таблиці `posts`
--
INSERT INTO `posts` (`posts_id`, `posts_text`, `posts_date`, `user_login`, `likes`) VALUES
(148, 'bfbbbbbt', '2022-10-05', 'petro', 0);
-- --------------------------------------------------------
--
-- Структура таблиці `users`
--
CREATE TABLE `users` (
`id` int NOT NULL,
`login` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci NOT NULL,
`password` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci NOT NULL,
`name` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
--
-- Дамп даних таблиці `users`
--
INSERT INTO `users` (`id`, `login`, `password`, `name`) VALUES
(33, 'Roma', '123', ''),
(34, 'Petro', '333', ''),
(35, 'roma', '123', '');
--
-- Індекси збережених таблиць
--
--
-- Індекси таблиці `likes`
--
ALTER TABLE `likes`
ADD PRIMARY KEY (`id`);
--
-- Індекси таблиці `posts`
--
ALTER TABLE `posts`
ADD PRIMARY KEY (`posts_id`);
--
-- Індекси таблиці `users`
--
ALTER TABLE `users`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT для збережених таблиць
--
--
-- AUTO_INCREMENT для таблиці `likes`
--
ALTER TABLE `likes`
MODIFY `id` int NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT для таблиці `posts`
--
ALTER TABLE `posts`
MODIFY `posts_id` int NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=152;
--
-- AUTO_INCREMENT для таблиці `users`
--
ALTER TABLE `users`
MODIFY `id` int NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=36;
COMMIT;