Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I've been looking at this for some time now, all I'm doing is learning php with mysql, with no previous programming experience before these so please be kind. I need to delete an entire row from a table. The table is inside list.php and there's a delete button that redirects to delete.php. But all it does it redirects me to a blank page with an url something like "delete.php?id=4" depending on row ID, which seems to be correct.

This is connect.php:

<?php 
$server = "localhost";
$db_user = "root";
$db_pass = "";
$db_name = "school";

$db_conn = mysqli_connect($server, $db_user, $db_pass, $db_name);

$connect_error = 'Sorry, we are experiencing connection problems';

mysql_connect('localhost', 'root', '') or die($connect_error);
mysql_select_db('school') or die($connect_error);

?>

And this is delete.php :

<?php 
if(isset($_GET['ID'])){
    $courseID = $_GET['ID'];
    $sql_delete = "DELETE FROM Courses WHERE ID = $courseID";       
    print ($sql_delete);        
        $result = mysqli_query($db_conn,$sql_delete);
    if($result) {
        echo "Congratulations. You have deleted this course succesfully.";
        header('location:list.php');
    } else {
        echo "Error";
    error_reporting(E_ALL ^ E_DEPRECATED);
    }

}
?>

Notice "print ($sql_delete);", which doesn't print anything either. All I can think of is there is something wrong with the way I am asking for the ID by using $_GET, but can't get my head around it. When I run

"DELETE FROM Courses WHERE ID = 4;"

inside xampp's mysql module, it works. Uhm... oh well.

OK, let me add: This is actually my delete button inside list.php, which gives me an URL based on table row number, based on Course id.

<?php echo "<td><a href=\"delete.php?id=$row[ID]\">Delete</a>";?>
share|improve this question
1  
$_GET['ID'] must be $_GET['id'] – Paul Spiegel Dec 13 '15 at 11:37
    
Hey Paul, thanks for answering. It doesn't redirect me to a blank page anymore which means that header now works, but it doesn't delete the row either. :-/ – J. Doe Dec 13 '15 at 11:50
    
Are you testing with an ID that exists? Meaning: if you already deleted the row with ID of 4, are you testing with ID 4? – timgavin Dec 13 '15 at 11:55
    
You also need to avoid sql injection. $courseID = mysqli_real_escape_string($_GET['id']); – timgavin Dec 13 '15 at 11:57
    
timgavin, you missed the details of the question. I only inserted a number manually inside SQL command line to check whether there was something wrong with the query. I need to make it work automatically with $_GET, by making the code read the ID automatically from the URL. As I already mentioned, URL redirection works properly. – J. Doe Dec 13 '15 at 12:01

connect.php

    $server = "localhost";
    $db_user = "root";
    $db_pass = "";
    $db_name = "school";
    $db_conn = mysqli_connect($server, $db_user, $db_pass, $db_name);

delete.php

if(isset($_GET['ID'])){
  $courseID = $_GET['ID'];
  $sql_delete = "DELETE FROM Courses WHERE ID = $courseID";        
  $result = mysqli_query($db_conn,$sql_delete);
  if(mysqli_affected_rows($db_conn)>0) {
      header('location:list.php?result=success');
  } else {
     header('location:list.php?result=fail');
  }
 }

list.php (Where is delete button)

<?php
if(isset($_GET['result'))
{
if($_GET['result']=='success')
{

    echo "Congratulations. You have deleted this course succesfully.";
}
else{
     echo "error";
  }
}
?>
(Delete button)
<a href="delete.php?ID=5" >Delete</a>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.