-2

The row won't delete in my database. I don't know what's wrong TT but then again, I don't have any knowledge in Database.

Here is the code for my delete query:

require_once('connect.php');

        $sql_delete="DELETE FROM test WHERE pid='$pid'";
        $qry_delete = mysql_query($sql_delete);

and here is the code in PHP. When the user clicks on DELETE, it will automatically delete the selected row. Please help.

while($data = mysql_fetch_array($qry)){
        $html .= '<tr align="center">';
        $html .= '<td>';
        $html .= '<a href="updateprocess.php">Update</a> | <a href="deleteprocess.php">Delete</a>';
        $html .= '</td>';
        $html .= '<td>';
        $html .= $data['pname'];
        $html .= '</td>';
        $html .= '</tr>';
    }

edit: should I change the "pid=$pid" to pname=pname? edit2: pid and pname are fields in my database.

kichen
  • 17
  • 1
  • 4
  • From where did you get $pid ? also if you echo $sql_delete what query you got ? – Hassan Feb 05 '14 at 13:23
  • 5
    Stop using `mysql` , its deprecated. Use `mysqli`. – user2936213 Feb 05 '14 at 13:24
  • 3
    @user2936213 It's nice that you are pointing to not use mysql. But that's not enough. If you’re still working with MySQLi, maybe it’s time for a change: http://net.tutsplus.com/tutorials/php/pdo-vs-mysqli-which-should-you-use/. I highly recommend using PDO. – Christopher Will Feb 05 '14 at 13:27
  • @kichen What error messages do you get when you execute the statement of $sql_delete in your MySQL console? – Christopher Will Feb 05 '14 at 13:29
  • @Hassan i'm sorry. that's not my original code. my original code would be WHERE pname=''"; – kichen Feb 05 '14 at 13:29
  • @ChristopherWill I have a javascript validation. It just says "Product deleted." but it didn't delete it. The original code is WHERE pname=''"; – kichen Feb 05 '14 at 13:31
  • 1
    with this code you need to pass pid in querystring href="deleteprocess.php?pid=".$data['pid'] and get it where you are deleting the record, like $pid = $_GET['pid'] – Hassan Feb 05 '14 at 13:31
  • As @Hassan said, the variable $pid is empty. That's why the query works but you do not see any changes. So pass the $pid to the query. But also check first if it's set correctly. – Christopher Will Feb 05 '14 at 13:36

1 Answers1

1
    while($data = mysql_fetch_array($qry)){
        $html .= '<tr align="center">';
        $html .= '<td>';
        $html .= '<a href="updateprocess.php">Update</a> | <a href="deleteprocess.php?pid=' . $data['pid'] . '">Delete</a>';
        $html .= '</td>';
        $html .= '<td>';
        $html .= $data['pname'];
        $html .= '</td>';
        $html .= '</tr>';
    }

deleteprocess.php will be looks like this:

if(isset($_REQUEST['pid'])) {
    $pid = mysql_escape_string($_REQUEST['pid']);
    $sql_delete="DELETE FROM test WHERE pid='$pid'";
    $qry_delete = mysql_query($sql_delete);
} else {
    die("Something went wrong, try again");
}
JamesG
  • 1,687
  • 2
  • 25
  • 39
  • That's a very good 'what to do', but doesn't tell the OP where they've gone wrong. In short: The system needs to know exactly which 'delete' link was pressed, so you pass the ID along in the querystring and read it in the PHP. Previously $pid was blank so you were deleting `where pid=''`, which matched no rows so nothing was deleted. Even shorter: PHP and MySQL are not mindreaders. – Paul Gregory Feb 05 '14 at 13:37