دورة mysql للمبتدئين الدرس الرابع
1- إضافة فورم إضافة المستخدمين
//index.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Php Cours</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
<script src="main.js"></script>
</head>
<body>
<form action="index.php" method="post">
<label for="nom">Nom</label>
<input type="text" name="nom" id="nom"><br>
<label for="prenom">Prénom</label>
<input type="text" name="prenom" id="prenom"><br>
<label for="email">Email </label>
<input type="text" name="email" id="email"><br>
<input type="submit" value="valider">
</form>
</body>
</html>
2- إضافة مستخدم لقاعدة البيانات
//index.php
<?php
$host = 'localhost';
$user = 'root';
$password = '';
$db = 'php_course';
//open connection
$con = new mysqli($host,$user,$password,$db);
//get for data
if(isset($_POST['submit'])){
$nom = htmlspecialchars(trim($_POST['nom']));
$prenom = htmlspecialchars(trim($_POST['prenom']));
$email = htmlspecialchars(trim($_POST['email']));
//insert data
$sql = "INSERT INTO users (nom,prenom,email) VALUES ('$nom','$prenom','$email')";
if($con->query($sql)){
echo 'utilisateur ajouté';
}else{
echo 'Erreur'.$con->error;
}
}
//close connection
$con->close();
?>
3- استرجاع وعرض المستخدمين
//index.php
<table>
<thead>
<th>Nom</th>
<th>Prénom</th>
<th>Email</th>
<th>Action</th>
</thead>
<tbody>
<?php
$host = 'localhost';
$user = 'root';
$password = '';
$db = 'php_course';
//open connection
$con = new mysqli($host,$user,$password,$db);
//get for data
if(isset($_POST['submit'])){
$nom = htmlspecialchars(trim($_POST['nom']));
$prenom = htmlspecialchars(trim($_POST['prenom']));
$email = htmlspecialchars(trim($_POST['email']));
//insert data
$sql = "INSERT INTO users (nom,prenom,email) VALUES ('$nom','$prenom','$email')";
if($con->query($sql)){
echo 'utilisateur ajouté';
}else{
echo 'Erreur'.$con->error;
}
}
//show data
$sql = "SELECT * FROM users";
$result = $con->query($sql);
while($row = $result->fetch_assoc()):
?>
<tr>
<td><?php echo $row['nom'];?></td>
<td><?php echo $row['prenom'];?></td>
<td><?php echo $row['email'];?></td>
<td><a href="index.php?action=delete&id=<?php echo $row['id'];?>">supprimer</a></td>
<td><a href="index.php?action=update&id=<?php echo $row['id'];?>">modifier</a></td>
</tr>
<?php
endwhile;
//close connection
$con->close();
?>
</tbody>
</table>
4- تعديل مستخدم باستعمال id
//index.php
<?php
endwhile;
//update data
if(isset($_GET['id']) && $_GET['id'] !== null && $_GET['action'] === 'update'){
$id = htmlspecialchars(trim($_GET['id']));
$sql = "SELECT * FROM users WHERE id = $id";
$result = $con->query($sql);
$row = $result->fetch_assoc();
}
if(isset($_POST['update'])){
$nom = htmlspecialchars(trim($_POST['nom']));
$prenom = htmlspecialchars(trim($_POST['prenom']));
$email = htmlspecialchars(trim($_POST['email']));
//update data
$id = htmlspecialchars(trim($_POST['id']));
$sql = "UPDATE users SET nom = '$nom',prenom = '$prenom',email = '$email' WHERE id = $id";
if($con->query($sql)){
echo 'utilisateur modifié';
}else{
echo 'Erreur'.$con->error;
}
}
//close connection
$con->close();
?>
</tbody>
</table>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Php Cours</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
<script src="main.js"></script>
</head>
<body>
<form action="index2.php" method="post">
<label for="nom">Nom</label>
<input type="text" name="nom" id="nom" value="<?php echo isset($row['nom']) ? $row['nom'] : '';?>"><br>
<label for="prenom">Prénom</label>
<input type="text" name="prenom" id="prenom" value="<?php echo isset($row['prenom']) ? $row['prenom'] : '';?>"><br>
<label for="email">Email </label>
<input type="hidden" name="action" value="<?php echo isset($_GET['action']) ? $_GET['action'] : '';?>">
<input type="hidden" name="id" value="<?php echo isset($_GET['id']) ? $_GET['id'] : '';?>">
<input type="text" name="email" id="email" value="<?php echo isset($row['email']) ? $row['email'] : '';?>"><br>
<input type="submit" name="submit" value="valider">
<input type="submit" name="update" value="modifier">
</form>
</body>
</html>
5- حذف مستخدم باستعمال id
//index.php
<?php
endwhile;
//update data
if(isset($_GET['id']) && $_GET['id'] !== null && $_GET['action'] === 'update'){
$id = htmlspecialchars(trim($_GET['id']));
$sql = "SELECT * FROM users WHERE id = $id";
$result = $con->query($sql);
$row = $result->fetch_assoc();
}
if(isset($_POST['update'])){
$nom = htmlspecialchars(trim($_POST['nom']));
$prenom = htmlspecialchars(trim($_POST['prenom']));
$email = htmlspecialchars(trim($_POST['email']));
//update data
$id = htmlspecialchars(trim($_POST['id']));
$sql = "UPDATE users SET nom = '$nom',prenom = '$prenom',email = '$email' WHERE id = $id";
if($con->query($sql)){
header('location:index2.php');
echo 'utilisateur modifié';
}else{
echo 'Erreur'.$con->error;
}
}
//delete data
if(isset($_GET['id']) && $_GET['id'] !== null && $_GET['action'] === 'delete'){
$id = htmlspecialchars(trim($_GET['id']));
$sql = "DELETE FROM users WHERE id = $id";
if($con->query($sql)){
header('location:index2.php');
echo 'utilisateur supprimé';
}else{
echo 'Erreur'.$con->error;
}
}
//close connection
$con->close();
?>