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
- <?php include('server.php');
-
-
- if (isset($_GET['edit'])) {
- $id = $_GET['edit'];
- $edit_state = true;
-
- $rec = mysqli_query($db, "SELECT * FROM info WHERE id=$id");
- $record = mysqli_fetch_array($rec);
- $name = $record['name'];
- $address = $record['address'];
- $id = $record['id'];
- }
- ?>
-
- <html>
- <head>
- <title>Add to create, update, delete Database records</title>
- <link rel="stylesheet" type="text/css" href="style.css">
- </head>
- <body>
-
- <?php if(isset($_SESSION['msg'])): ?>
- <div class= "msg">
- <?php
- echo $_SESSION['msg'];
- unset($_SESSION['msg']);
- ?>
- </div>
- <?php endif ?>
- <table>
- <thread>
- <tr>
- <th>Name</th>
- <th>Address</th>
- <th colspan="2">Action</th>
- </tr>
- </thread>
- </tbody>
- <?php while ($row = mysqli_fetch_array($results)) { ?>
- <tr>
- <td><?PHP echo $row['name']; ?></td>
- <td><?PHP echo $row['address']; ?></td>
- <td>
- <a class="edit_btn" href="index.php?edit=<?php echo $row['id']; ?>">Edit</a>
- </td>
- <td>
- <a class="del_btn" href="server.php?del=<?php echo $row['id']; ?>">Delete</a>
- </td>
- </tr>
- <?php } ?>
- </tbody>
-
- </table>
- <form method="post" action="server.php">
- <input type="hidden" name="id" value="<?php echo $id; ?>">
- <div class="input-group">
- <label>Name</label>
- <input type="text" name="name" value="<?php echo $name; ?>">
- </div>
- <div class="input-group">
- <label>Address</label>
- <input typr="type" name="address" value="<?php echo $address; ?>">
- </div>
- <div class="input-group">
- <?php if ($edit_state == false): ?>
- <button type="submit" name="save" class="btn">Save</button>
- <?php else: ?>
- <button type="submit" name="update" class="btn">Update</button>
- <?php endif ?>
- </div>
- </form>
- </body>
- </html>
server.php
- ?php
- session_start();
-
- $name = "";
- $address = "";
- $id = 0;
- $edit_state = false;
-
-
- $db = mysqli_connect('localhost:3307','root','','crud1');
-
-
- if (isset($_POST['save'])) {
- $name = $_POST['name'];
- $address = $_POST['address'];
-
- $query = "INSERT INTO info (name, address) VALUES ('$name', '$address')";
- mysqli_query($db, $query);
- $_SESSION['msg'] = "Address saved";
- header('location: index.php');
- }
-
-
- if (isset($_POST['update'])) {
- $name = mysql_real_escape_string($_POST['name']);
- $address = mysql_real_escape_string($_POST['address']);
- $id = mysql_real_escape_string($_POST['id']);
-
- mysqli_query($db, "UPDATE info SET name='$name', address='$address' WHERE id=$id");
- $_SESSION['msg'] = "Address updated";
-
- }
-
-
- if (isset($_GET['del'])) {
- $id = $_GET['del'];
- mysqli_query($db, "DELETE FROM info WHERE id=$id");
- $_SESSION['msg'] = "Address deleted";
- header('location: index.php');
- }
-
-
-
- $results = mysqli_query($db, "SELECT * FROM info");
- ?>
style.css
- body {
- font-size: 19px;
- }
- table{
- width: 50px;
- margin: 30px auto;
- border-collapse: collapse;
- text-align: left;
- }
- tr{
- border-bottom: 1px solid #cbcbcb;
- }
-
- th, td{
- border: none;
- height: 30px;
- padding: 2px;
- }
- tr:hover{
- background: #f5f5f5;
- }
- form{
- width: 45%;
- margin: 50px auto;
- text-align: left;
- padding: 20px;
- border: 1px solid #bbbbbb;
- border-radius: 5px;
- }
- .input-group{
- margin: 10px 0px 10px 0px;
- }
- .input-group label{
- display: block;
- text-align: left;
- margin: 3px;
- }
- .input-group input{
- height: 30px;
- width: 93%;
- padding: 5px 10px;
- font-size: 16px;
- border-radius: 5px;
- border: 1px solid gray;
- }
- .btn {
- padding: 10px;
- font-size: 15px;
- color: white;
- background: #5F9EA0;
- border: none;
- border-radius: 5px;
- }.msg{
- margin: 30px auto;
- padding: 10px;
- border-radius: 5px;
- color: #3c763d;
- background: #dff0d8;
- width: 50%;
- text-align: center;
- }
-
- .msg{
- margin: 30px auto;
- padding: 10px;
- border-radius: 50px;
- color: #3c763d;
- background: #dff0d8;
- border: 1px solid #3c763d;
- width: 50px;
- text-align: center;
- }
-
- .edit{
- text-decoration: none;
- padding: 2px 5px;
- background: #2E8B57;
- color: white;
- border-radius: 3px;
- }
-
- .del_btn{
- text-decoration: none;
- padding: 2px 5px;
- color: white;
- border-radius: 3px;
- background: #800080;
- }