首页 > 开发 > PHP > 正文

php mysql 删除记录, url传值删除数据

2024-05-04 21:58:28
字体:大 中 小
来源:转载
供稿:网友

本款删除数据代码是一款根据用户url地址栏传过来的参数,再去mysql数据库中执行删除操作,PHP实例代码如下:

  1. $host = 'localhost'; 
  2. $user_name = 'root'; 
  3. $password = 'admin'; 
  4.  
  5. $conn = mysql_connect($host,$user_name,$password); 
  6. if(!$conn) 
  7. { 
  8.     die('数据库连接失败:'.mysql_error()); 
  9. } 
  10. mysql_select_db('test'); 
  11.  
  12. $sql = 'select id,name,city,created_time from users'; 
  13.  
  14. $result = mysql_query($sql) or die("<br/>error: <b>".mysql_error()."</b><br/>产生问题的sql:".$sql); 
  15. ?> 
  16. <html> 
  17. <head> 
  18. <title>13-12.php</title> 
  19. <script language="网页特效"> 
  20.  
  21. </script> 
  22. </head> 
  23. <center> 
  24.  
  25. <body> 
  26. <table width="75%" border="0" cellpadding="0" cellspacing="1" bgcolor="#7b7b84"> 
  27.     <tr bgcolor="#8bbcc7">  
  28.         <td height="33"><div align="center"><strong>用户id</strong></div></td> 
  29.         <td><div align="center"><strong>用户名称</strong></div></td> 
  30.         <td><div align="center"><strong>来自城市</strong></div></td> 
  31.         <td><div align="center"><strong>注册时间</strong></div></td> 
  32.         <td><div align="center"><strong>操作</strong></div></td> 
  33.     </tr> 
  34.  
  35. <?php 
  36. if($num = mysql_num_rows($result)) 
  37. { 
  38.     while($row = mysql_fetch_array($result,mysql_assoc)) 
  39.     { 
  40. ?> 
  41.     <tr bgcolor="#ffffff">  
  42.         <td height="22" align="right"><?php echo $row['id']; ?>&nbsp;</td> 
  43.         <td height="22">&nbsp;<?php echo $row['name']; ?>&nbsp;</td> 
  44.         <td height="22">&nbsp;<?php echo $row['city']; ?>&nbsp;</td> 
  45.         <td height="22">&nbsp;<?php echo $row['created_time']; ?>&nbsp;</td> 
  46.         <td height="22">&nbsp;<a onclick="javascript:if(confirm('确定要删除用户信息吗?')) return true; else return false;" href="13-13.php?id=<?php echo $row['id']; ?>">删除</a>&nbsp;</td> 
  47.     </tr> 
  48. <?php 
  49.     } 
  50. } 
  51. mysql_close($conn); 
  52. ?> 
  53.  
  54. </table> 
  55. </body> 
  56. </center> 
  57. </html> 
  58.  
  59. <?php 
  60. if(!isset($_get['id'])) 
  61. { 
  62.     echo '参数错误!'; 
  63.     exit; 
  64. } 
  65.  
  66. $id = $_get['id']; 
  67. if(emptyempty($id)) 
  68. { 
  69.     echo '用户id不能为空!'; 
  70.     exit; 
  71. } 
  72.  
  73. $host = 'localhost'; 
  74. $user_name = 'root'; 
  75. $password = 'admin'; 
  76.  
  77. $conn = mysql_connect($host,$user_name,$password); 
  78. if(!$conn) 
  79. { 
  80.     die('数据库连接失败:'.mysql_error()); 
  81. } 
  82. mysql_select_db('test'); 
  83.  
  84. //先判断是否存在该id的用户 
  85. $sql = "select * from users where id=$id"; 
  86. $result = mysql_query($sql) or die("<br/>error: <b>".mysql_error()."</b><br/>sql:".$sql); 
  87. if(!mysql_num_rows($result)) 
  88. { 
  89.     echo '用户id错误!'; 
  90.     exit; 
  91. } 
  92.  
  93. //删除用户数据//开源代码Vevb.com 
  94. $sql = "delete from users where id=$id"; 
  95. mysql_query($sql) or die("<br/>error: <b>".mysql_error()."</b><br/>sql:".$sql); 
  96. mysql_close($conn); 
  97.  
  98. echo '数据删除成功,返回<a href="13-12.php">13-12.php</a>查看数据'; 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表