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");
                }
            ?>
2
  • Remove the @ infront of the mysql_query. This might surpress error messages - php.net/manual/en/language.operators.errorcontrol.php Commented Sep 13, 2012 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. Commented Sep 13, 2012 at 13:55

3 Answers 3

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.

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

5 Comments

@andrewsi I edited this but however it still doesn't work any ideas ? It just refreshes the current page.
You're only running the second query.
@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).
@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 :)
@w4kv +1 for actually going through with the time and effort to do as I suggested. Good show sir!
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);

4 Comments

This is in some dire need of mysql_real_escape_string.
@Fluffeh It is still not working. It just refreshes the pages and that is all.
@w4kv you missed a ; on the end of $qry = "DELETE FROM members WHERE login='$login' AND... :)
@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.
0

You can always try and use mysqli_multi_query()

2 Comments

I don't know how to use it. Is it just simply swap mysql_query with mysqli_multi_query?
The 2nd answer on this post explains provides an example. stackoverflow.com/questions/4667596/…

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.