1

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?

4
  • 4
    $query = "DELETE FROM users_list WHERE id = ".$this_user_id; did you try concatenating? Commented Jun 9, 2015 at 1:16
  • 2
    Shouldn't you be using a prepared statement with MySQLi? Commented Jun 9, 2015 at 1:17
  • What type of field is id? Have you tried wrapping the value in single quotes or concatenation? Commented Jun 9, 2015 at 1:17
  • Concatenating didn't work either sadly. Tim - I'm still just working on the basics of MYSQL/PHP so I haven't worked up to prepared statements yet, though I hope to soon. And id is an integer. It's just an auto incrementing id field in a MYSQL table. Commented Jun 9, 2015 at 1:23

1 Answer 1

2

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.