Le https://inshare.fr/topic/1719-syst%C3%A8me-like-commentaire/?do=findComment&comment=15231 de message dans Système Like + Commentaire a été marqué comme réponse
Tiens j'tai fais ça en 20min car j'avais un peu de pitié mdr
index.php
<?php
require 'database.php';
if(!isset($_SESSION['account'])) {
header('Location: login');
}
?>
<!doctype html>
<html>
<head>
<title>Démo HD</title>
<meta charset="UTF-8">
<meta name="viewport" content="initial-scale=1.0">
</head>
<body>
<?php if($_GET['id']) { ?>
<?php
$req = $db->prepare('SELECT * FROM articles WHERE id = ?');
$req->execute(array($_GET['id']));
$fetch = $req->fetch();
$like = $db->prepare('SELECT COUNT(*) AS nb FROM likes WHERE id_article = ?');
$like->execute(array($_GET['id']));
$fetch2 = $like->fetch();
?>
<center><p><?= $fetch['title']; ?></p>
<hr>
<p><?= $fetch['contenu']; ?></p>
<hr>
Il y a <?= $fetch2['nb']; ?> j'aime<?php if($fetch2['nb'] > 1) { echo 's'; } ?>
<hr>
<?php
$lik = $db->prepare('SELECT * FROM likes WHERE id_article = ? AND user_member = ?');
$lik->execute(array($_GET['id'], $_SESSION['account']['username']));
$rowCount = $lik->rowCount();
if(isset($_POST['like'])) {
$req = $db->prepare('SELECT * FROM likes WHERE id_article = ? AND user_member = ?');
$req->execute(array($_GET['id'], $_SESSION['account']['username']));
$rowCount2 = $req->rowCount();
if($rowCount2 == 0) {
$insert = $db->prepare('INSERT INTO likes(id_article, user_member) VALUES(?, ?)');
$insert->execute(array($_GET['id'], $_SESSION['account']['username']));
} else {
$delete = $db->prepare('DELETE FROM likes WHERE id_article = ? AND user_member = ?');
$delete->execute(array($_GET['id'], $_SESSION['account']['username']));
}
}
if($rowCount == 0) {
echo '<form method="post"><button name="like" type="submit">J\'aime</button></form>';
} else {
echo '<form method="post"><button name="like" type="submit">Je n\'aime plus</button></form>';
}
?>
</center>
<?php } else { ?>
<ul>
<?php
$req = $db->query('SELECT * FROM articles');
while($r = $req->fetch()) {
echo '<li>'.$r['title'].' : <a href="?id='.$r['id'].'">Voir</a></li>';
}
?>
</ul>
<?php } ?>
</body>
</html>
login.php
<?php
require 'database.php';
if(isset($_POST['login'])) {
$username = htmlspecialchars($_POST['username']);
$password = htmlspecialchars($_POST['password']);
if(!empty($username) && !empty($password)) {
$req = $db->prepare('SELECT * FROM users WHERE username = ?');
$req->execute(array($username));
$fetch = $req->fetch();
$rowCount = $req->rowCount();
if($rowCount != 0) {
if($password == $fetch['password']) {
$_SESSION['account'] = array(
'username' => $username,
);
header('Location: /');
} else {
$erreur = 'Le mot de passe est incorrect';
}
} else {
$erreur = 'Le compte n\'existe pas';
}
} else {
$erreur = 'Veuillez remplir tous les champs';
}
}
?>
<?php if(isset($erreur)) { echo $erreur; } ?>
<form method="post">
<input name="username" placeholder="Nom d'utilisateur" type="text">
<input name="password" placeholder="Mot de passe" type="password">
<button name="login" type="submit">Connexion</button>
</form>
database.php
<?php
// Connexion à la base de données
$user = 'root';
$pass = 'aqwzsx';
$db = new PDO('mysql:host=localhost;dbname=demohd', $user, $pass);
session_start();
?>
table sql
/*
Navicat Premium Data Transfer
Source Server : MacServer
Source Server Type : MySQL
Source Server Version : 50718
Source Host : localhost
Source Database : demohd
Target Server Type : MySQL
Target Server Version : 50718
File Encoding : utf-8
Date: 06/02/2017 22:57:33 PM
*/
SET NAMES utf8;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for `articles`
-- ----------------------------
DROP TABLE IF EXISTS `articles`;
CREATE TABLE `articles` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(50) DEFAULT NULL,
`contenu` text,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- ----------------------------
-- Table structure for `likes`
-- ----------------------------
DROP TABLE IF EXISTS `likes`;
CREATE TABLE `likes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_article` int(11) DEFAULT NULL,
`user_member` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- ----------------------------
-- Table structure for `users`
-- ----------------------------
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
SET FOREIGN_KEY_CHECKS = 1;