1

当您选中复选框并按下“删除”按钮以从成员表中删除该成员并从应用程序表中删除他的应用程序时,我想与网站成员和在此表中创建一个表。当我单击删除按钮时,使用我的代码打印“查询失败”

这是我的代码:

<?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>';
    }
?>

这是我的 php 部分:

    <?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");
            }
        ?>

编辑:

<?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");
                }
            ?>
4

3 回答 3

1

您试图在一个查询中执行两个删除语句。这是一个禁忌。

您需要将语句拆分为两个执行:

$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);
于 2012-09-13T13:46:43.730 回答
1
$qry = "DELETE FROM members WHERE login='$login' AND passwd='".md5($_POST['password'])."'" 
&& "DELETE FROM applications WHERE userfname = '$userfname'";

这是你的问题——你试图一次调用两个 SQL 语句,而 mysql_query 不会让你这样做。如果您执行两个单独的查询,它应该可以工作。

然而

您应该考虑迁移到mysqli_*或 PDO -mysql_*已被弃用。您也可以直接使用 mysqli在一次调用中执行多个查询;并且它们都使用绑定参数,这可以帮助您编写更安全的代码。

于 2012-09-13T13:45:50.100 回答
0

您可以随时尝试使用mysqli_multi_query()

于 2012-09-13T13:58:29.287 回答