اضافة وتعديل وحذف وعرض البيانات بلغة php وقواعد بيانات mysql باستخدام PDO
اضافة وتعديل وحذف وعرض البيانات بلغة php وقواعد بيانات mysql باستخدام PDO
انشى قاعدة بيانات على السرفرولتكن school ثم انشى جدول جديد وليكن student
نضيف الحقول الى الجدول
انشى ملف للبروجت في ملف السرفر www
ثم ابدا بأنشاء ملفات البروجت
connect.php
<?php$dns = "mysql:host=localhost;dbname=school";$user = "root";$pass ="";$option = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');try{$con = new PDO($dns , $user , $pass ,$option);$con->setAttribute(PDO::ATTR_ERRMODE , PDO::ERRMODE_EXCEPTION);echo "";}catch(PDOExecption $e){echo "Not Connection ".$e->getMessage();}?>
Index.php
<?phpinclude 'connect.php';$do = isset($_GET['do']) ?$_GET['do'] : 'control';if($do == "control"){$stmt = $con->prepare("SELECT * FROM student");$stmt->execute();$rows = $stmt->fetchAll();?><a href="Index.php?do=Add">Add New Student</a><table><tr><td>Number of Student| </td><td>Name of Student |</td><td>Age |</td></tr><?phpforeach($rows as $row){echo "<tr>";echo "<td".$row['id']."</td>";echo "<td>".$row['name']."</td>";echo "<td>".$row['age']."</td>";echo "<td><a href='Index.php?do=Edit&std=".$row['id']."'>Edit</a><a href='Index.php?do=Delete&std=".$row['id']."'>Delete</a></td>";echo "</tr>";}?></table><?php}elseif($do == "Add"){?><form action="?do=Insert" method="POST">Student Name : <input type="text" name ="name"/><br><br>Student Age :<input type="text" name="age"/><br><br><input type="submit" value="Save Student" ></from><?php}elseif($do == "Insert"){if($_SERVER["REQUEST_METHOD"] == "POST"){$name = $_POST['name'];$age = $_POST['age'];$stmt = $con->prepare("INSERT INTO student (name , age ) VALUES (:zname , :zage)");$stmt->execute(array('zname' => $name,'zage' => $age));header("location:Index.php");}}elseif($do == "Edit"){$std = isset($_GET['std']) && is_numeric($_GET['std']) ? intval($_GET['std']) : 0;$stmt1 = $con->prepare("SELECT * FROM student WHERE id = ?");$stmt1->execute(array($std));$rows = $stmt1->fetch();$count = $stmt1->rowCount();if($stmt1->rowCount() > 0){?><form action="?do=Update" method="POST"><input type="hidden" name="std" value ="<?php echo $std ?>">Student Name : <input type="text" name ="name" value="<?php echo $rows['name']?>"/><br><br>Student Age :<input type="text" name="age" value="<?php echo $rows['age']?>"/><br><br><input type="submit" value="Save Student" ></from><?php}}elseif($do == "Update"){if($_SERVER["REQUEST_METHOD"] == "POST"){$id = $_POST['std'];$name = $_POST['name'];$age = $_POST['age'];$stmt2 = $con->prepare("UPDATE student SET name = ? , age =? WHERE id = ?");$stmt2->execute(array($name,$age,$id));header("location:Index.php");}}elseif($do == 'Delete'){$std = isset($_GET['std']) && is_numeric($_GET['std']) ? intval($_GET['std']) : 0;$stmt3 = $con->prepare("DELETE FROM student WHERE id = :zid");$stmt3->bindParam(":zid" ,$std);$stmt3->execute();header("location:Index.php");}?>
شاهد الفيديو هنا
تنزيل الكود هنا
ليست هناك تعليقات: