每日博客
php数据库
<?php session_start(); try {$pdo = new PDO("mysql:host=localhost;dbname=test;", "root", "fx30001225");} catch (PDOException $e) {die("数据库连接失败" . $e->getMessage());} $pdo->query("SET NAMES 'UTF8'"); switch ($_GET['action']) { case 'login': { $name = $_POST['name']; $sex = $_POST['sex']; $str = ""; if ($sex == '女') $str = $name . ' 小姐'; else $str = $name . ' 先生'; $_SESSION['msg'] = $str; $sql = "SELECT *FROM stu WHERE NAME ='{$name}'"; $stmt = $pdo->query($sql); if($stmt->rowCount()> 0 ) { $stu = $stmt->fetch(PDO::FETCH_ASSOC); $_SESSION['id']=$stu['id']; echo "<script>window.location='ex02c.php' </script>"; } else {echo "<script>window.location='ex02b.php'</script>";} break; } case 'add': { $name = $_POST['name']; $sex = $_POST['sex']; $hobby = $_POST['hobby']; $address = $_POST['address']; $age = $_POST['age']; $_SESSION['name']=$name; $sql = "INSERT INTO stu VALUES (NULL ,'{$sex}','{$name}','{$age}','{$address}','{$hobby}')"; $rw = $pdo->exec($sql); if ($rw > 0) { echo "<script> alert('您的资料保存成功'); window.location='ex02c.php'; //跳转 </script>"; } else { echo "<script> alert('增加失败'); window.history.back(); </script>"; } } break; case 'del': { $id = $_GET['id']; $sql = "DELETE FROM stu WHERE id='{$id}'"; $rw = $pdo->exec($sql); if ($rw > 0) { echo "<script> alert('数据删除成功!'); window.location='ex02b.php'; </script>"; } else { echo "<script> alert('数据删除失败!'); window.history.back(); </script>"; } break; } case edit: { $id = $_POST['id']; $name = $_POST['name']; $sex = $_POST['sex']; $hobby = $_POST['hobby']; $address = $_POST['address']; $age = $_POST['age']; $sql = "UPDATE stu SET name='{$name}',sex='{$sex}',age='{$age}',address='{$address}',hobby='{$hobby}' WHERE id='{$id}'"; $rw = $pdo->exec($sql); if ($rw > 0) {echo "<script>alert('修改成功');window.location='ex01b.php'</script>";} else {echo "<script>alert('修改失败');window.history.back()</script>";} break; } }