Israel

Israel

  • 996
  • 1.3k
  • 155.2k

It's saving very well without bug but datas doesnt appear into DB

Aug 27 2020 12:23 PM
Hi,
I am learning PHP. But I am trying to save into my mysql Database.
 
When I click on the save button its saving without bugg's message but my DB still blank. I do verify all the connection's functions (there is not problem). But my DB doesnt receive any datas. Can I have a solution please?
 
index.php
  1. <?php include('server.php'); ?>  
  2. <html>  
  3. <head>  
  4. <title>Add to create, update, delete Database records</title>  
  5. <link rel="stylesheet" type="text/css" href="style.css">  
  6. </head>  
  7. <body>  
  8. <?php if(isset($_SESSION['msg'])): ?>  
  9. <div class"msg">  
  10. <?php  
  11. echo $_SESSION['msg'];  
  12. unset($_SESSION['msg']);  
  13. ?>  
  14. </div>  
  15. <?php endif ?>  
  16. <table>  
  17. <thread>  
  18. <tr>  
  19. <th>Name</th>  
  20. <th>Address</th>  
  21. <th colspan="2">Action</th>  
  22. </tr>  
  23. </thread>  
  24. </tbody>  
  25. <?php while ($row = mysqli_fetch_array($results)) ?>  
  26. <tr>  
  27. <td><?php echo $row['name']; ?></td>  
  28. <td><?php echo $row['address']; ?></td>  
  29. <td>  
  30. <a href="#">Edit</a>  
  31. </td>  
  32. <td>  
  33. <a href="#">Delete</a>  
  34. </td>  
  35. </tr>  
  36. <?php ?>  
  37. </tbody>  
  38. </table>  
  39. <form method="post" action="server.php">  
  40. <div class="input-group">  
  41. <label>Name</label>  
  42. <input type="text" name="name">  
  43. </div>  
  44. <div class="input-group">  
  45. <label>Address</label>  
  46. <input typr="type" name="address">  
  47. </div>  
  48. <div class="input-group">  
  49. <button type="submit" name="save" class="btn">Save</button>  
  50. </div>  
  51. </form>  
  52. </body>  
  53. </html>  
server.php
  1. <?php  
  2. // initialize variables  
  3. $name = "";  
  4. $address = "";  
  5. $id = 0;  
  6. // connect to database  
  7. $db = mysqli_connect('localhost:3307','root','','crud1');  
  8. // if save button is clicked  
  9. if (isset($_POST['save'])) {  
  10. $name = $_POST['name'];  
  11. $address = $_POST['address'];  
  12.   
  13. $query = "INSERT INTO info (name, address) VALUES ('$name', '$address')";  
  14. mysqli_query($db$query);  
  15. $_SESSION['msg'] = "Address saved";  
  16. header('location: index.php'); // redirect to index page after inserting  
  17. }  
  18. // retrieve records  
  19. $results = mysqli_query($db"SELECT * FROM info");  
  20. ?>  
style.css
  1. body {  
  2. font-size19px;  
  3. }  
  4. table{  
  5. width50px;  
  6. margin30px auto;  
  7. border-collapsecollapse;  
  8. text-alignleft;  
  9. }  
  10. tr{  
  11. border-bottom1px solid #cbcbcb;  
  12. }  
  13.   
  14. th, td{  
  15. bordernone;  
  16. height30px;  
  17. padding2px;  
  18. }  
  19. tr:hover{  
  20. background#f5f5f5;  
  21. }  
  22. form{  
  23. width45%;  
  24. margin50px auto;  
  25. text-alignleft;  
  26. padding20px;  
  27. border1px solid #bbbbbb;  
  28. border-radius: 5px;  
  29. }  
  30. .input-group{  
  31. margin10px 0px 10px 0px;  
  32. }  
  33. .input-group label{  
  34. displayblock;  
  35. text-alignleft;  
  36. margin3px;  
  37. }  
  38. .input-group input{  
  39. height30px;  
  40. width93%;  
  41. padding5px 10px;  
  42. font-size16px;  
  43. border-radius: 5px;  
  44. border1px solid gray;  
  45. }  
  46. .btn {  
  47. padding10px;  
  48. font-size15px;  
  49. colorwhite;  
  50. background#5F9EA0;  
  51. bordernone;  
  52. border-radius: 5px;  
  53. }.msg{  
  54. margin30px auto;  
  55. padding10px;  
  56. border-radius: 5px;  
  57. color#3c763d;  
  58. background#dff0d8;  
  59. width50%;  
  60. text-aligncenter;  
  61. }  

Answers (4)