Skip to content
Loading
Update doest work?
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. }  

1 Reply

Know the answer? Post it — somebody with the same question will find it here.