MYSQL delete query works in console, but not in PHP script

200 views Asked by At

I have a simple script here that is meant to delete a user out of the database. The "$query" works fine straight in the console, but through this script it will just not work no matter what I try:

<?php
  require_once("../includes/session.php");
  require_once("../includes/db_connection.php");
  require_once("../includes/functions.php");

  if (isset($_POST["submit_delete"])) {
    $this_user_id = $_GET["id"];

    $query = "DELETE FROM users_list WHERE id = {$this_user_id}";
    $result = mysqli_query($connection, $query);
    confirm_query($result); // function that runs connect_error/errno

    if ($result) {
      $_SESSION["message"] = "User deleted successfully.";
      redirect_to("index.php");
    } else {
      $_SESSION["message"] = "Query failed. User was not deleted";
      redirect_to("edit_user.php?id=" . $this_user_id);
    }

  } else {
    $_SESSION["message"] = "Something went wrong. Your user wasn't deleted.";
    redirect_to("index.php");
  }

  mysqli_free_result($result);

  if (isset($connection)) {
    mysqli_close($connection);
  }
?>

All I get is a mysqli_connect_error/errno code of "0";

I've even changed the variable to an integer (e.g: 21) and it still doesn't work.

Any ideas?

1

There are 1 answers

0
James On

With a lot of help from Tim Biegeleisen we figured out what the problem was.

Hooked into the users_list table was a foreign key from another table which was preventing the record/row from being deleted. I edited my original delete function so the record from the other table was deleted first, and then the record from users_list was deleted straight after. It all works now!