موقع شكايات ب php و mysql الجزء الأول
نظرة سريعة بالفيديو
1- إنشاء قاعدة البيانات
--
-- Structure de la table `contacts`
--
CREATE TABLE `contacts` (
`id` int(11) NOT NULL,
`fullname` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`body` text NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `plaintes` (
`id` int(11) NOT NULL,
`fullname` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`category` varchar(255) NOT NULL,
`city` varchar(255) NOT NULL,
`title` varchar(255) NOT NULL,
`body` text NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`code` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Index pour la table `contacts`
--
ALTER TABLE `contacts`
ADD PRIMARY KEY (`id`);
--
-- Index pour la table `plaintes`
--
ALTER TABLE `plaintes`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT pour les tables déchargées
--
--
-- AUTO_INCREMENT pour la table `contacts`
--
ALTER TABLE `contacts`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=0;
--
-- AUTO_INCREMENT pour la table `plaintes`
--
ALTER TABLE `plaintes`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=0;
2- الملف config.php
<?php
define ('DB_HOST','localhost');
define ('DB_USER','root');
define ('DB_PW','');
define ('DB_DATABASE','chikaya');
3- الملف Database.php
<?php
require_once('config.php');
/**
*
*/
class Database
{
public $connection;
public function __construct(){
$this->connect();
}
public function connect(){
$this->connection = new mysqli(DB_HOST,DB_USER,DB_PW,DB_DATABASE);
if($this->connection->connect_errno){
die('connexion echouée '.mysqli_error($this->connection ));
}
}
public function query($query){
$result = mysqli_query($this->connection,$query);
return $result;
}
public function escape_string($string){
$escaped_string = mysqli_real_escape_string($this->connection,$string);
return $escaped_string;
}
public function inserted_id(){
$id = mysqli_insert_id($this->connection);
return $id;
}
}
$database = new Database();
4- الملف Query.php
<?php
require ('Database.php');
/**
*
*/
class Query
{
public function insert($values = array()){
global $database;
$sql = "INSERT INTO plaintes (fullname,email,category,city,title,body,code) VALUES ('".implode("','",$values)."')";
if($database->query($sql)){
return $database->inserted_id();
}else{
return mysqli_error($database->connection);
}
}
public function addMessage($values = array()){
global $database;
$sql = "INSERT INTO contacts (fullname,email,body) VALUES ('".implode("','",$values)."')";
if($database->query($sql)){
return $database->inserted_id();
}else{
return mysqli_error($database->connection);
}
}
public function find($code){
global $database;
$sql = "SELECT count(*) FROM plaintes WHERE code='$code'";
$result = $database->query($sql);
$total = mysqli_fetch_assoc($result);
return !empty($total) ? array_shift($total) : false;
}
public function makeCode(){
global $database;
do{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$code = '';
for ($i = 0; $i < 10; $i++) {
$code .= $characters[rand(0, $charactersLength - 1)];
}
$sql = "SELECT count(*) FROM plaintes WHERE code=".$code;
$code_url = $database->query($sql);
}while($code_url >= 1);
return $code;
}
}