1

I'm trying to add a delete button on a table row, once the user clicks it, I want it to delete the record on the mysql database and therefore, not show on the table. My problem is when I click the delete button, I get a connection error! message, here's my php code:

<?php

        $id = $_GET['id'];


        $dbc = mysqli_connect('localhost','user','pass','db') or die('Connection error!');



        $query = "DELETE FROM table WHERE id = '$id'";
        mysqli_query($dbc, $query) or die('Database error!');
        \header('location:datapull.php');

    ?>

thanks for any help in advance...

  • 2
    Get the real error `or die(mysqli_error($dbc))` instead of `or die('Connection error!')` same thing for your query. Plus, is `table` your actual table name? I had to ask. Plus, get rid of the \ in `\header` – Funk Forty Niner Apr 17 '15 at 02:05
  • failure to connect, been there bro –  Apr 17 '15 at 02:08
  • 1
    Also at a minimum cast $id to an int. It's best to use prepared statements. http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?lq=1 – chris85 Apr 17 '15 at 02:09
  • 1
    Just so you know your script is vulnerable to SQL injection, anyone could pass `?id=' or '1'='1` to your script and delete every single row in that table. Use [mysqli_real_escape_string](http://php.net/manual/en/mysqli.real-escape-string.php) to sanitize your input. – Prime Apr 17 '15 at 02:10
  • ok I switched it, now I get a blank page. My table name is not table, I just put it there for reference. i also got rid of the backslash. But all I get is a blank page – Ramses llorente Apr 17 '15 at 02:10
  • 1
    Blank page means syntax errors. Add error reporting to the top of your file(s) right after your opening PHP tag for example ` – Funk Forty Niner Apr 17 '15 at 02:12
  • Plus, what @AlphaDelta said to use `mysqli_real_escape_string()`. You may be trying to enter something that MySQL doesn't agree with. `or die(mysqli_error($dbc))` to `mysqli_query()` will tell you that. – Funk Forty Niner Apr 17 '15 at 02:14
  • @Fred-ii- thats my whole php code, are you refering to where the begin button is located? – Ramses llorente Apr 17 '15 at 02:17
  • Yes. If that is part of your PHP and in the same file. By the way, are you running this from your own computer or hosted site? If localhost; are you accessing via `http://localhost/file.php` or `file:///file.php`? – Funk Forty Niner Apr 17 '15 at 02:18
  • @Fred-ii- I got these errors: Warning: mysqli_connect(): (28000/1045): Access denied for user 'admin'@'localhost' (using password: YES) in /home2/doctorc0/public_html/consultas/delete.php on line 9 Warning: mysqli_error() expects parameter 1 to be mysqli, boolean given in /home2/doctorc0/public_html/consultas/delete.php on line 9 – Ramses llorente Apr 17 '15 at 02:18
  • @Fred-ii- hosted site – Ramses llorente Apr 17 '15 at 02:19
  • 1
    username or password or db is incorrect –  Apr 17 '15 at 02:19
  • `mysqli_error()` requires DB connection passed to it `mysqli_error($dbc)` – Funk Forty Niner Apr 17 '15 at 02:19
  • what should I do? im sorry, I dont know much... @Fred-ii- – Ramses llorente Apr 17 '15 at 02:21
  • `$dbc = mysqli_connect('localhost','user','pass','db') or die("Error " . mysqli_error($dbc));` and `mysqli_query($dbc, $query) or die(mysqli_error($dbc));` – Funk Forty Niner Apr 17 '15 at 02:23
  • untill the connection is fixed you cant parse it on to the other functions –  Apr 17 '15 at 02:23
  • @Fred-ii- It's telling me I have denied access: Warning: mysqli_connect(): (28000/1045): Access denied for user 'user'@'localhost' (using password: YES) in /home2/doctorc0/public_html/consultas/delete.php on line 9 Warning: mysqli_error() expects parameter 1 to be mysqli, boolean given in /home2/doctorc0/public_html/consultas/delete.php on line 9 Error – Ramses llorente Apr 17 '15 at 02:26
  • It's a permissions issue then. Check all credentials and that you chose the right database. Make sure you have `delete` rights also. You may not have that right to do it. – Funk Forty Niner Apr 17 '15 at 02:27
  • @Fred-ii- Im so sorry, there was a letter missing, its going back to the datapull.php but its not deleting the row, I still see the info on it... – Ramses llorente Apr 17 '15 at 02:30
  • I'd like to help further, but I really have to go to bed now; and my back's out. If nobody provides a solution, I'll come back to the question tomorrow. Good luck. – Funk Forty Niner Apr 17 '15 at 02:31

2 Answers2

0

Solution:

Step1. Over check: localhost,user,pass and db

Step2: Check privilege of user on your database.

If it does not work, I am over-sure that there is problem in your server. At that case contact with customer support of your server.

Asif Iqbal
  • 1,132
  • 1
  • 10
  • 23
-1

Double check your database username, password and database name.

Try this code and reply me with what you are getting.

$id = $_GET['id'];    
$dbc = mysqli_connect('localhost','user','pass','db') or die('Connection error!');
//Variables inside single quotes wont work. Change to this
$query = "DELETE FROM table WHERE id = '".$id."'";
mysqli_query($dbc, $query) or die('Database error!');
// Check how many rows is affected. If it is returns 0 it is not working. Else it will return how many rows is deleted.
printf("Records deleted: %d\n", mysqli_affected_rows($dbc)); 
exit;
Mahendran Sakkarai
  • 8,381
  • 6
  • 44
  • 66