Blogger Information
Blog 29
fans 1
comment 0
visits 23256
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP实现MySQl增删改查基本语法!
阿心
Original
1179 people have browsed it

PHP查询MYSQL语句

  1. <?php
  2. namespace pdo_edu;
  3. use PDO;
  4. require 'config/connect.php';
  5. //查询语句
  6. $sql= 'select * from user_list';
  7. $stmt = $pdo->prepare($sql);
  8. $stmt->execute();
  9. $user_lists = $stmt->fetchAll(PDO::FETCH_ASSOC);
  10. //预处理防止SQL注入
  11. unset($PDO);
  12. ?>
  13. <html>
  14. <meta charset="utf-8">
  15. <body>
  16. <div class="contain">
  17. <h2>员工信息查询系统</h2>
  18. <table border='1' cellspacing='0' cellpadding='10'>
  19. <tr bgcolor=#ccc><th>编号</th><th>账户</th><th>性别</th><th>姓名</th><th>存款</th><th>籍贯</tr>
  20. <?php
  21. foreach($user_lists as $user_list){ ?>
  22. <!-- // printf('<pre>%s</pre>',print_r($staff,true)); -->
  23. <tr>
  24. <td><?php echo $user_list['id'] ?></td>
  25. <td><?php echo $user_list['user_name'] ?></td>
  26. <td><?php echo $user_list['sex'] ?></td>
  27. <td><?php echo $user_list['pay_name'] ?></td>
  28. <td><?php echo $user_list['money'] ?></td>
  29. <td><?php echo $user_list['loginaddress'] ?></td>
  30. </tr>
  31. <?php } ?>
  32. </div>
  33. </body>
  34. </html>

PHP实现MYSQL新增数据

  1. <?php
  2. namespace pdo_edu;
  3. use PDO;
  4. require 'config/connect.php';
  5. $user_name = $_POST['user_name'];
  6. $sex=$_POST['sex'];
  7. $pay_name=$_POST['pay_name'];
  8. $money=$_POST['money'];
  9. $loginaddress=$_POST['loginaddress'];
  10. if(!empty($user_name)){
  11. //查询语句
  12. $sql= "insert `user_list` set `user_name`=? , `sex`=? , `pay_name`=? , `money`=? , `loginaddress`=? ";
  13. // var_dump($sql);
  14. //预处理防止SQL注入
  15. $stmt = $pdo->prepare($sql);
  16. $indata=[$user_name,$sex,$pay_name,$money,$loginaddress];
  17. $stmt->execute($indata);
  18. // PDOStatement::rowCount() 返回上一个由对应的 PDOStatement 对象执行DELETE、 INSERT、或 UPDATE 语句受影响的行数
  19. if($stmt->rowCount() === 1 ){
  20. // PDO::lastInsertId — 返回最后插入行的ID或序列值
  21. // echo '新增成功'.$pdo->lastInsertId();
  22. echo "<script>alert('success');</script>";
  23. }else{
  24. echo "<script>alert('field');</script>";
  25. }
  26. unset($PDO);
  27. }
  28. ?>
  29. <html>
  30. <meta charset="utf-8">
  31. <body>
  32. <div class="contain">
  33. <h2>员工信息查询系统</h2>
  34. <form action='./insert.php' method='post' id='iset' name='iset'>
  35. <table border='1' cellspacing='0' cellpadding='10'>
  36. <tr bgcolor=#ccc>
  37. <th>账户</th>
  38. <th>性别</th>
  39. <th>姓名</th>
  40. <th>存款</th>
  41. <th>籍贯</th>
  42. </tr>
  43. <tr>
  44. <td>
  45. <input type='text' name='user_name' >
  46. </td>
  47. <td>
  48. <input type='text' name='sex' >
  49. </td>
  50. <td>
  51. <input type='text' name='pay_name' >
  52. </td>
  53. <td>
  54. <input type='text' name='money' >
  55. </td>
  56. <td>
  57. <input type='text' name='loginaddress' >
  58. </td>
  59. </tr>
  60. <tr>
  61. <td rowspan='6'><input type='submit' value='提交'></td>
  62. </tr>
  63. </table>
  64. </form>
  65. </div>
  66. </body>
  67. </html>

PHP操作MYSQL更新字段行

  1. <?php
  2. namespace pdo_edu;
  3. use PDO;
  4. require 'config/connect.php';
  5. $sql = 'update `user_list` set `pay_name`=? where `id`=? ';
  6. $stmt = $pdo->prepare($sql);
  7. $stmt->execute(['王霸',2791]);
  8. if($stmt->rowCount()===1){
  9. echo "更新成功";
  10. }

PHP操作MYSQL删除字段行

  1. <?php
  2. namespace pdo_edu;
  3. use PDO;
  4. require 'config/connect.php';
  5. $sql= 'delete from `user_list` where `id`=:id ';
  6. $stmt= $pdo->prepare($sql);
  7. $id=filter_input(INPUT_GET,'id',FILTER_VALIDATE_INT);
  8. $stmt->execute(['id'=>$_GET['id']]);
  9. if($stmt->rowCount()===1)
  10. {
  11. echo '删除成功';
  12. }
  13. unset($pdo);

总结:PHP对MYSQL表中添加数据和查询数据思路相对清晰一点。删除和修改还没有明确的认识!

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:步骤,流程一定要清楚
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post