0

I am working on an assignment in PHP and MySQL, where I am needed to create a function to delete records from a table via ID with a push of a button with JavaScript. Even though I followed by teacher's videos, it still is not able to delete the record away.

My teacher and I suspected that it has got to do with the bind_param part, but still it is not solved.

Here are the files:

db-connect.php

<?php
    $servername = "localhost";
    $username = "root";
    $password = ""; // this should be empty
    $dbName = "newDB"; // add your database name here
    
    $conn = new mysqli($servername, $username, $password, $dbName);
    
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    
        /**
         * $conn->connect_error - contains an error message from the database server (if any)
         */
    }
    ?>

index.php

<?php
    require "db-connect.php";
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>PHP Code-Along 2</title>

        <link rel="stylesheet" href="style.css">
    </head>

    <body>
        <h1>List of Records</h1>

        <div>
            <table>
                <tr>
                    <th>ID</th>
                    <th>Name</th>
                    <th>Age</th>
                    <th>Email</th>
                    <th>Actions</th>
                </tr>
                <?php
                $sql = "SELECT * FROM `Employee`;";

                $sql_run = $conn->query($sql);

                if($sql_run) {  // if it is not false, then proceed
                    if($sql_run->num_rows > 0) {    // num_rows will check if there are row(s) of results
                        while($row = $sql_run->fetch_assoc()) {
                            ?>
                <tr>
                    <td><?= $row['id']; ?></td>
                    <td><?= $row['name']; ?></td>
                    <td><?= $row['age']; ?></td>
                    <td><?= $row['email']; ?></td>
                    <td>
                        <button onclick="document.location.href = 'form.php?id=<?= $row['id']; ?>'">Edit</button>
                        <button onclick="deleteConfirm(<?= $row['id']; ?>);">Delete</button>
                    </td>
                </tr>
                <?php
                        }
                    } else {
                        // echo "No table rows found.";
                        ?>
                <tr>
                    <td colspan="5">No records found.</td>
                </tr>
                <?php
                    }
                } else {
                ?>
                <tr>
                    <td colspan="5">Error retrieving table rows: <?= $conn->error; ?></td>
                </tr>
                <?php
                }
            ?>
            </table>
        </div>

        <script src="main.js"></script>
    </body>

</html>

main.js

function deleteConfirm(id) {

    const response = confirm(`Are you sure you want to delete record #${id}?`);

    if(response) {
        document.location.href = "db-deleterecord.php?=id" + id;
    }
}

db-deleterecord.php

<?php
 if(isset($_GET['id'])) {   //  check if "?id=..." exists, i.e. if a GET value id is obtained.
    require "db-connect.php";

    $sql = "DELETE FROM `Employee` WHERE `id` = ?;";

    $stmt = $conn->prepare($sql);

    if($stmt) {
        $stmt->bind_param("i", $_GET['id']);

        if($stmt->execute()) {
            echo "Deleted record with ID: " .$_GET['id'];
        } else echo "Unable to delete record #" . $_GET['id'] . ": " .$stmt->error;
    } else echo "Unable to prepare statement: " . $conn->error;
 }
 header("refresh:5; url=index.php");
 ?>
  • Do you see any of the feedback messages during the 5 seconds you're on `db-deleterecord.php`? – KIKO Software Nov 27 '22 at 10:12
  • I'm afraid not. – A.K. Enoch Nov 27 '22 at 10:20
  • In that case try to [see if there's a PHP error](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display). – KIKO Software Nov 27 '22 at 10:23
  • So far it's ok, I think it's no error, when I put the code. – A.K. Enoch Nov 27 '22 at 10:27
  • `bind_param()` requires variables, but you give it `$_GET['id']`. Why not store that value in a variable: `$employeeId = $_GET['id'];` and then do `$stmt->bind_param("i", $employeeId);`? However this will not solve your problem, because you don't see any feedback messages. Something else must be wrong as well. – KIKO Software Nov 27 '22 at 10:33
  • Ah! I think the problem is that the URL reads "id5", for example, instead of "id=5" – A.K. Enoch Nov 27 '22 at 11:10

1 Answers1

2

There's an error in your string concatenation for the url -

document.location.href = "db-deleterecord.php?=id" + id;

should be -

document.location.href = "db-deleterecord.php?id=" + id;
user1191247
  • 10,808
  • 2
  • 22
  • 32
  • That's well spotted! – KIKO Software Nov 27 '22 at 10:56
  • Yeah, but still it reads something like "http://localhost/wpga/practice/db-deleterecord.php?=id5", which should be "id=5" if I am not mistaken – A.K. Enoch Nov 27 '22 at 11:20
  • You may need to re-read my answer. Perhaps you have a caching issue? You may need to force a hard refresh. And you should turn up the error reporting level and make sure display errors is on, as you should have received numerous error messages. – user1191247 Nov 27 '22 at 11:23
  • It is solved, but it gave this error: Fatal error: Uncaught mysqli_sql_exception: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1 – A.K. Enoch Nov 27 '22 at 11:26
  • You may want to echo $sql to see what is going wrong. You may have an unexpected unicode char which is not visible. It runs fine for me. – user1191247 Nov 27 '22 at 11:48