1

I want to make a table with the members of a website and in this table when you check the checkboxes and you press the "Delete" button to delete this member from the members table and also to delete his applications from the applications table. With my code when I click the delete button it prints me "Query failed"

This is my code:

<?php
    require_once('config.php');
    $errmsg_arr = array();
    $errflag = false;
    $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
    if(!$link) {
        die('Failed to connect to server: ' . mysql_error());
    }
    $db = mysql_select_db(DB_DATABASE);
    if(!$db) {
        die("Unable to select database");
    }
    $data = mysql_query("SELECT * FROM members ") or die(mysql_error()); 
    echo ' <form action="members-exec.php">
            <table width="760" border=1>
                        <tr>';
                            if(isset($_SESSION['SESS_RANK'])) {
                                echo '
                                    <th></th>';
                            }
                            echo '
                            <th>Служител:</th>
                            <th>Отпуск отпреди 2009год.</th>
                            <th>Отпуск от мин. год.</th>
                            <th>Отпуск от тек. год.</th>

                        </tr>';
        while($info = mysql_fetch_array( $data )) 
            { 
                 echo  '
                        <tr>';
                            if(isset($_SESSION['SESS_RANK'])) {
                            echo '
                                <td>
                                    <input type="checkbox" name="'.$info['firstname'] .' '.$info['lastname'] .'" value="'.$info['firstname'] .' '.$info['lastname'] .'" />
                                </td>';
                            }
                            echo '
                            <td>'.$info['firstname'] .' '.$info['lastname'] .'</td>  
                            <td>'.$info['predi'] .'</td>
                            <td>'.$info['minali'] .'</td>
                            <td>'.$info['tekushti'] .'</td>';
                            }

                    echo'   </tr> '; 
    echo '</table>';
    if(isset($_SESSION['SESS_RANK'])) {
        echo '
        <br> <input type="submit" name="remove" value="Delete" /></form>';
    }
?>

This is my php part:

    <?php
            session_start();
            require_once('config.php');
            $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
            if(!$link) {
                die('Failed to connect to server: ' . mysql_error());
            }
            $db = mysql_select_db(DB_DATABASE);
            if(!$db) {
                die("Unable to select database");
            }

            $qry = "DELETE FROM members WHERE login='$login' AND passwd='".md5($_POST['password'])."'" && "DELETE FROM applications WHERE userfname = '$userfname'";
            $result = mysql_query($qry);
            if($result) {
                header("location: members.php");
                exit();
            }else {
                die("Query failed");
            }
        ?>

EDIT:

<?php
                session_start();
                require_once('config.php');
                $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
                if(!$link) {
                    die('Failed to connect to server: ' . mysql_error());
                }
                $db = mysql_select_db(DB_DATABASE);
                if(!$db) {
                    die("Unable to select database");
                }

                $qry = "DELETE FROM members WHERE login='$login' AND passwd='".md5($_POST['password'])."'" ;
$result = mysql_query($qry);
$qry = "DELETE FROM applications WHERE userfname = '$userfname'";
                $result = mysql_query($qry);
                if($result) {
                    header("location: members.php");
                    exit();
                }else {
                    die("Query failed");
                }
            ?>
w4kv
  • 39
  • 3
  • 8
  • Remove the @ infront of the mysql_query. This might surpress error messages - http://php.net/manual/en/language.operators.errorcontrol.php – Conrad Lotz Sep 13 '12 at 13:53
  • @JLC007 That's true, but that isn't his problem, in this case, he is trying to execute two delete queries as a single query and the database is rejecting it. – Fluffeh Sep 13 '12 at 13:55

3 Answers3

1
$qry = "DELETE FROM members WHERE login='$login' AND passwd='".md5($_POST['password'])."'" 
&& "DELETE FROM applications WHERE userfname = '$userfname'";

There's your problem - you're trying to do two SQL statements with one call, and mysql_query won't let you do that. It should work if you do two separate queries.

HOWEVER

You should look at moving to mysqli_* or PDO - mysql_* is being deprecated. You can do multiple queries in one call directly using mysqli, too; and they both make use of bound parameters, which helps you write more secure code.

andrewsi
  • 10,807
  • 132
  • 35
  • 51
  • @andrewsi I edited this but however it still doesn't work any ideas ? It just refreshes the current page. – w4kv Sep 13 '12 at 13:51
  • You're only running the second query. – andrewsi Sep 13 '12 at 13:53
  • @w4kv You need to run the extra mysql_query() between your two separate delete queries. (You are trying to merge two queries into one, which doesn't work). – Fluffeh Sep 13 '12 at 13:54
  • @w4kv Yes, like in your edit (although is is not good practice to modify your question to include the code that will work as it won't help others in the future who look at your question). I would suggest that in this case, you edit your question to show your original code, and then show an *edit* section with the working code - in addition to Accepting one of the answers here so that other folk don't spend time trying to solve something that is already solved :) – Fluffeh Sep 13 '12 at 14:08
  • @w4kv +1 for actually going through with the time and effort to do as I suggested. Good show sir! – Fluffeh Sep 13 '12 at 14:12
1

You are trying to execute two delete statements in one query. This is a no-no.

You will need to split the statements into two executes:

$qry = "DELETE FROM members WHERE login='$login' AND passwd='".md5($_POST['password'])."'";
$result = mysql_query($qry);
$qry="DELETE FROM applications WHERE userfname = '$userfname'";
$result = mysql_query($qry);
Fluffeh
  • 33,228
  • 16
  • 67
  • 80
  • This is in some dire need of `mysql_real_escape_string`. – tadman Sep 13 '12 at 14:11
  • @Fluffeh It is still not working. It just refreshes the pages and that is all. – w4kv Sep 13 '12 at 14:11
  • @w4kv you missed a `;` on the end of `$qry = "DELETE FROM members WHERE login='$login' AND...` :) – Fluffeh Sep 13 '12 at 14:14
  • @Fluffeh I accidently missed to add it to my post when I edited it. In my original file it got the ;. The problem is other i guess. – w4kv Sep 13 '12 at 14:33
0

You can always try and use mysqli_multi_query()

Conrad Lotz
  • 8,200
  • 3
  • 23
  • 27
  • I don't know how to use it. Is it just simply swap mysql_query with mysqli_multi_query? – w4kv Sep 13 '12 at 14:00
  • The 2nd answer on this post explains provides an example. http://stackoverflow.com/questions/4667596/cannot-figure-out-how-to-run-a-mysqli-multi-query-and-use-the-results-from-the-l – Conrad Lotz Sep 13 '12 at 14:17