Israel

Israel

  • 705
  • 1.3k
  • 216.4k

Update doest work?

Aug 30 2020 1:36 PM
Hi,
I do test these codes and its works perfectly (saave and delete option). But after clicking on the update button stop and sent me to this line nº25 on the server.php:
 
$name = mysql_real_escape_string($_POST['name']);  //here the bugg
 
Fatal error: Uncaught Error: Call to undefined function mysql_real_escape_string() in C:\xampp\htdocs\dashboard\TestCRUD\server.php:25 Stack trace: #0 {main} thrown in C:\xampp\htdocs\dashboard\TestCRUD\server.php on line 25 
 
 
index.php
 
  1. <?php include('server.php');   
  2.   
  3. // fetch the record to be updated  
  4. if (isset($_GET['edit'])) {  
  5.     $id = $_GET['edit'];  
  6.     $edit_state = true;  
  7.   
  8.     $rec = mysqli_query($db"SELECT * FROM info WHERE id=$id");  
  9.     $record = mysqli_fetch_array($rec);  
  10.     $name = $record['name'];  
  11.     $address = $record['address'];  
  12.     $id = $record['id'];  
  13. }  
  14. ?>  
  15.   
  16. <html>  
  17. <head>  
  18.     <title>Add to create, update, delete Database records</title>  
  19.     <link rel="stylesheet" type="text/css" href="style.css">  
  20. </head>  
  21. <body>  
  22.   
  23.     <?php if(isset($_SESSION['msg'])): ?>  
  24.         <div class"msg">  
  25.             <?php  
  26.             echo $_SESSION['msg'];  
  27.             unset($_SESSION['msg']);  
  28.             ?>  
  29.         </div>  
  30.     <?php endif ?>  
  31.     <table>  
  32.         <thread>  
  33.             <tr>  
  34.                 <th>Name</th>  
  35.                 <th>Address</th>  
  36.                 <th colspan="2">Action</th>  
  37.             </tr>  
  38.         </thread>      
  39.         </tbody>   
  40.            <?php while ($row = mysqli_fetch_array($results)) { ?>  
  41.             <tr>   
  42.                     <td><?PHP echo $row['name']; ?></td>  
  43.                     <td><?PHP echo $row['address']; ?></td>                      
  44.                     <td>  
  45.                         <a class="edit_btn" href="index.php?edit=<?php echo $row['id']; ?>">Edit</a>  
  46.                     </td>      
  47.                     <td>  
  48.                         <a class="del_btn" href="server.php?del=<?php echo $row['id']; ?>">Delete</a>  
  49.                     </td>  
  50.                 </tr>  
  51.            <?php } ?>  
  52.           </tbody>              
  53.               
  54. </table>  
  55. <form method="post" action="server.php">  
  56. <input type="hidden" name="id" value="<?php echo $id; ?>">  
  57.     <div class="input-group">  
  58.         <label>Name</label>  
  59.         <input type="text" name="name" value="<?php echo $name; ?>">  
  60.     </div>  
  61.     <div class="input-group">  
  62.         <label>Address</label>  
  63.         <input typr="type" name="address" value="<?php echo $address; ?>">  
  64.     </div>  
  65.     <div class="input-group">  
  66.     <?php if ($edit_state == false): ?>  
  67.         <button type="submit" name="save" class="btn">Save</button>  
  68.     <?php else: ?>  
  69.         <button type="submit" name="update" class="btn">Update</button>  
  70.     <?php endif ?>          
  71.     </div>  
  72.     </form>  
  73. </body>  
  74. </html>  
server.php
 
  1. ?php  
  2.     session_start();  
  3. // initialize variables  
  4. $name = "";  
  5. $address = "";  
  6. $id = 0;  
  7. $edit_state = false;  
  8.   
  9. // connect to database  
  10. $db = mysqli_connect('localhost:3307','root','','crud1');  
  11.   
  12. // if save button is clicked  
  13. if (isset($_POST['save'])) {  
  14.     $name = $_POST['name'];  
  15.     $address = $_POST['address'];  
  16.   
  17.     $query = "INSERT INTO info (name, address) VALUES ('$name', '$address')";  
  18.     mysqli_query($db$query);  
  19.     $_SESSION['msg'] = "Address saved";  
  20.     header('location: index.php'); // redirect to index page after inserting  
  21. }  
  22.   
  23. // update records  
  24. if (isset($_POST['update'])) {  
  25.     $name = mysql_real_escape_string($_POST['name']);  
  26.     $address = mysql_real_escape_string($_POST['address']);  
  27.     $id = mysql_real_escape_string($_POST['id']);  
  28.   
  29.     mysqli_query($db"UPDATE info SET name='$name', address='$address' WHERE id=$id");  
  30.     $_SESSION['msg'] = "Address updated";  
  31.       
  32. }  
  33.   
  34. // delete records  
  35. if (isset($_GET['del'])) {  
  36.     $id = $_GET['del'];  
  37.     mysqli_query($db"DELETE FROM info WHERE id=$id");  
  38.     $_SESSION['msg'] = "Address deleted";  
  39.     header('location: index.php');  
  40. }  
  41.   
  42.   
  43. // retrieve records  
  44. $results = mysqli_query($db"SELECT * FROM info");  
  45. ?>  
style.css
 
 
  1. body {  
  2.     font-size: 19px;  
  3. }  
  4. table{  
  5.     width: 50px;  
  6.     margin: 30px auto;  
  7.     border-collapse: collapse;  
  8.     text-align: left;  
  9. }  
  10. tr{  
  11.     border-bottom: 1px solid #cbcbcb;      
  12. }  
  13.   
  14. th, td{  
  15.     border: none;  
  16.     height: 30px;  
  17.     padding: 2px;  
  18. }  
  19. tr:hover{  
  20.     background: #f5f5f5;  
  21. }  
  22. form{  
  23.     width: 45%;  
  24.     margin: 50px auto;  
  25.     text-align: left;  
  26.     padding: 20px;  
  27.     border: 1px solid #bbbbbb;  
  28.     border-radius: 5px;  
  29. }  
  30. .input-group{  
  31.     margin: 10px 0px 10px 0px;      
  32. }  
  33. .input-group label{  
  34.     display: block;  
  35.     text-align: left;  
  36.     margin: 3px;  
  37. }  
  38. .input-group input{  
  39.     height: 30px;  
  40.     width: 93%;  
  41.     padding: 5px 10px;  
  42.     font-size: 16px;  
  43.     border-radius: 5px;  
  44.     border: 1px solid gray;         
  45. }  
  46. .btn {  
  47.     padding: 10px;  
  48.     font-size: 15px;  
  49.     color: white;  
  50.     background: #5F9EA0;  
  51.     border: none;  
  52.     border-radius: 5px;  
  53. }.msg{  
  54.     margin: 30px auto;  
  55.     padding: 10px;  
  56.     border-radius: 5px;  
  57.     color: #3c763d;  
  58.     background: #dff0d8;  
  59.     width: 50%;  
  60.     text-align: center;  
  61. }  
  62.   
  63. .msg{  
  64.     margin: 30px auto;  
  65.     padding: 10px;  
  66.     border-radius: 50px;  
  67.     color: #3c763d;  
  68.     background: #dff0d8;  
  69.     border: 1px solid #3c763d;  
  70.     width: 50px;  
  71.     text-align: center;  
  72. }  
  73.   
  74. .edit{  
  75.     text-decoration: none;  
  76.     padding: 2px 5px;  
  77.     background: #2E8B57;  
  78.     color: white;  
  79.     border-radius: 3px;      
  80. }  
  81.   
  82. .del_btn{  
  83. text-decoration: none;  
  84. padding: 2px 5px;  
  85. color: white;  
  86. border-radius: 3px;  
  87. background: #800080;  
  88. }  

Answers (1)