Fonctionnement ...LOGIN

Fonctionnement de la base de données du didacticiel de gestion des étudiants en développement PHP

Créez un fichier action.php

Ce fichier utilise des instructions switch et case pour traiter des données telles que la suppression, la modification et l'ajout sur le. même page, la duplication de code est réduite

5.jpg

Le code est le suivant

<?php
header("content-type:text/html;charset=utf8");
$conn=mysqli_connect("localhost","root","root","study");
mysqli_set_charset($conn,"utf8");
if($conn){
    switch ($_GET['action']){
        case 'add'://add
            $name = $_POST['name'];
            $sex = $_POST['sex'];
            $age = $_POST['age'];
            $class = $_POST['class'];
            $sql = "insert into stu (`name`, sex, age, class) values ('$name', '$sex','$age','$class')";
            $rw = mysqli_query($conn,$sql);
            if ($rw > 0){
                echo "<script>alert('添加成功');</script>";
            }else{
                echo "<script>alert('添加失败');</script>";
            }
            header('Location: index.php');
            break;
        case 'del'://get
            $id = $_GET['id'];
            $sql = "delete from stu where id='$id'";
            $rw = mysqli_query($conn,$sql);
            if ($rw > 0){
                echo "<script>alert('删除成功');</script>";
            }else{
                echo "<script>alert('删除失败');</script>";
            }
            header('Location: index.php');
            break;
        case 'edit'://post
            $id = $_POST['id'];
            $name = $_POST['name'];
            $age = $_POST['age'];
            $class = $_POST['class'];
            $sex = $_POST['sex'];
//    echo $id, $age, $age, $name;
            $sql = "update stu set name='$name', age='$age',sex='$sex',class='$class' where id='$id';";
//    $sql = "update myapp.stu set name='jike',sex='女', age=24,classid=44 where id=17";
//            print $sql;
            $rw = mysqli_query($conn,$sql);
//            var_dump($rw);
//            die();
            if ($rw > 0){
                echo "<script>alert('更新成功');</script>";
            }else{
                echo "<script>alert('更新失败');</script>";
            }
            header('Location: index.php');
            break;
        default:
            header('Location: index.php');
            break;
    }
}else{
    die('数据库连接失败' .mysqli_connect_error());
}
?>






<?php header("content-type:text/html;charset=utf8"); $conn=mysqli_connect("localhost","root","root","study"); mysqli_set_charset($conn,"utf8"); if($conn){ switch ($_GET['action']){ case 'add'://add $name = $_POST['name']; $sex = $_POST['sex']; $age = $_POST['age']; $class = $_POST['class']; $sql = "insert into stu (`name`, sex, age, class) values ('$name', '$sex','$age','$class')"; $rw = mysqli_query($conn,$sql); if ($rw > 0){ echo "<script>alert('添加成功');</script>"; }else{ echo "<script>alert('添加失败');</script>"; } header('Location: index.php'); break; case 'del'://get $id = $_GET['id']; $sql = "delete from stu where id='$id'"; $rw = mysqli_query($conn,$sql); if ($rw > 0){ echo "<script>alert('删除成功');</script>"; }else{ echo "<script>alert('删除失败');</script>"; } header('Location: index.php'); break; case 'edit'://post $id = $_POST['id']; $name = $_POST['name']; $age = $_POST['age']; $class = $_POST['class']; $sex = $_POST['sex']; // echo $id, $age, $age, $name; $sql = "update stu set name='$name', age='$age',sex='$sex',class='$class' where id='$id';"; // $sql = "update myapp.stu set name='jike',sex='女', age=24,classid=44 where id=17"; // print $sql; $rw = mysqli_query($conn,$sql); // var_dump($rw); // die(); if ($rw > 0){ echo "<script>alert('更新成功');</script>"; }else{ echo "<script>alert('更新失败');</script>"; } header('Location: index.php'); break; default: header('Location: index.php'); break; } }else{ die('数据库连接失败' .mysqli_connect_error()); } ?>
soumettreRéinitialiser le code
chapitredidacticiel