Advertisement
Guest User

delete

a guest
Jun 9th, 2014
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.66 KB | None | 0 0
  1. delete.php
  2.  
  3. <?php
  4.     $host ="localhost";
  5.     $user = "root";
  6.     $pass = "";
  7.     $db_name = "test_son";
  8.     $table = "test";
  9.    
  10.     //Connect to MySQL and select database
  11.     $connection = mysqli_connect($host, $user, $pass, $db_name);
  12.    
  13.     //Check if connection errors
  14.    
  15.     if (mysqli_connect_errno()){
  16.        
  17.         die ("An error while connecting to MySQL" . mysqli_connect_errno());
  18.        
  19.     }
  20.    
  21.     //Select record from mysql
  22.     $select_query = "select * from $table";
  23.     $result = mysqli_query($connection, $select_query);
  24.  
  25. ?>
  26.  
  27. <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
  28. <tr>
  29. <td colspan="5" bgcolor="#FFFFFF"><strong>Delete data in mysql</strong> </td>
  30. </tr>
  31.  
  32. <tr>
  33. <td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
  34. <td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
  35. <td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
  36. <td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
  37. <td align="center" bgcolor="#FFFFFF">&nbsp;</td>
  38. </tr>
  39.  
  40. <?php
  41. while ($rows = mysqli_fetch_array($result)){
  42. ?>
  43. <tr>
  44. <td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
  45. <td bgcolor="#FFFFFF"><? echo $rows['name']; ?></td>
  46. <td bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td>
  47. <td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>
  48. <td bgcolor="#FFFFFF"><a href="delete_ac.php?id=<? echo $rows['id']; ?>">delete</a></td>
  49. </tr>
  50.  
  51. <?php
  52. // close while loop
  53. }
  54. ?>
  55.  
  56. </table>
  57.  
  58. <?php
  59.     //Close connection
  60.     mysqli_close($connection);
  61.  
  62. ?>
  63.  
  64.  
  65.  
  66.  
  67.  
  68. -------------------------
  69. delete_ac.php
  70. -------------------------
  71.  
  72. <?php
  73.     $host ="localhost";
  74.     $user ="root";
  75.     $pass = "";
  76.     $db_name = "test_son";
  77.     $table = "test";
  78.    
  79.     //Connect to server and select database
  80.    
  81.     $connection = mysqli_connect($host, $user, $pass, $db_name);
  82.    
  83.     //Check if connection errors?
  84.    
  85.     if (mysqli_connect_errno()){
  86.        
  87.         die("Connection error has occurred" . mysqli_errno());
  88.        
  89.     }
  90.    
  91.     //get value of id that sent from address bar
  92.     $id=$_GET['id'];
  93.    
  94.     //Delete data in mysql from row that has this id
  95.    
  96.     $sql="delete from $table where id='$id'";
  97.     $result = mysqli_query($connection, $sql);
  98.    
  99.     //If successfully deleted
  100.    
  101.    if ($result){
  102.        
  103.        echo "deleted successfully";
  104.        echo "<BR>";
  105.        echo "<a href='delete.php'> Back to Main Page</a>";
  106.        
  107.    }
  108.         else {
  109.            
  110.             echo "ERROR OCCURRED " . mysqli_errno($connection);
  111.            
  112.         }
  113. ?>
  114.  
  115. <?php
  116. //close php connection
  117. mysqli_close($connection);
  118.  
  119. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement