使用复选框和按钮从MySQL表中删除行

时间:2012-09-13 13:41:40

标签: php mysql html

我想与网站的成员建立一个表,当你勾选复选框时,在这个表中按“删除”按钮从成员表中删除该成员,并从应用程序表中删除他的应用程序。单击删除按钮时,我的代码会打印出“查询失败”

这是我的代码:

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

3 个答案:

答案 0 :(得分: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_*已被弃用。您也可以do multiple queries in one call直接使用mysqli;并且它们都使用绑定参数,这有助于您编写更安全的代码。

答案 1 :(得分: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);

答案 2 :(得分:0)

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