Advertisement
husiphp

Untitled

Sep 18th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.71 KB | None | 0 0
  1. <?php
  2.  
  3. $mysqli = new mysqli("localhost", "root", "", "demo");
  4.  
  5. // Check connection
  6. if($mysqli === false){
  7.     die("ERROR: Could not connect. " . $mysqli->connect_error);
  8. }
  9.  
  10. $first_name = $mysqli->real_escape_string($_REQUEST['first_name']);
  11. $last_name = $mysqli->real_escape_string($_REQUEST['last_name']);
  12.  
  13.  
  14. $sql = "INSERT INTO Persons (first_name, last_name) VALUES ('$first_name', '$last_name')";
  15. if($mysqli->query($sql) === true){
  16.     echo "Saved! <br/> <a href=index.php> Back </a> ";
  17. } else{
  18.     echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
  19. }
  20.  
  21. $sql = "SELECT * FROM Persons";
  22. if($result = $mysqli->query($sql)){
  23.     if($result->num_rows > 0){
  24.         echo "<table>";
  25.         echo "<tr>";
  26.         echo "<th>Imie</th>";
  27.         echo "<th>Nazwisko</th>";
  28.         echo "</tr>";
  29.         while($row = $result->fetch_array()){
  30.             echo "<tr>";
  31.             echo "<td>" . $row['id'] . "</td>";
  32.             echo "<td>" . $row['first_name'] . "</td>";
  33.             echo "<td>" . $row['last_name'] . "</td>";
  34.             echo "<td><input type='submit' id='".$row['id']."' value='delete' name='delete'></td>";
  35.             echo "</tr>";
  36.         }
  37.         echo "</table>";
  38.         // Free result set
  39.         $result->free();
  40.     } else{
  41.         echo "No records matching your query were found.";
  42.     }
  43. } else{
  44.     echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
  45. }
  46.  
  47.  
  48. // Check connection
  49. if($mysqli === false){
  50.     die("ERROR: Could not connect. " . $mysqli->connect_error);
  51. }
  52. if(isset($_GET['delete'])){
  53.     $deleted = $_GET['id'];
  54.     $new ="DELETE FROM Persons WHERE id = $deleted;";
  55.     $dbh->query($new);
  56. }
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63. // Close connection
  64. $mysqli->close();
  65. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement