允许用户删除个人资料信息

时间:2013-04-14 21:08:33

标签: php mysql delete-row

我正在尝试创建一个表单,在其个人资料中显示用户的当前服务信息,然后允许他们为他们提供的一个或多个服务选中一个复选框,然后点击删除按钮删除服务(s )与复选框有关。每个服务都有一个唯一的ID,因此我假设我需要使用它,以便查询知道要删除的内容。

以下是相关代码和我正在使用的表单:

<?php

if (isset($_POST['OddJobName']) && isset($_POST['Description']) && isset($_POST['DaysAvailable']) && empty($errors) === true){//if (empty($_POST) === false && empty($errors) === true) { //if (isset(empty($_POST['OddJobName'])) && isset(empty($_POST['Description'])) && isset(empty($_POST['DaysAvailable'])) === false && empty($errors) === true)
            $daysavailable='';
            foreach ($_POST['DaysAvailable'] as $value)
            {

            $daysavailable .=$value." ";
            }

            $Delete_Oddjob = array (
                'MemberID'      => $MemberID,
                'OddJobID'      => $_POST['OddJobID'],
                'OddJobName'    => $_POST['OddJobName'],
                'Description'   => $_POST['Description'],
                'DaysAvailable' => $daysavailable, 

                );
                Delete_Oddjob ($Delete_Oddjob);
                if(success){
                 header('Location: member.php?username='.$username);
                 exit ();
                }
            } else if (empty($errors) === false){
                //otherwise output errors
                echo output_errors($errors);
            }

?>

<?php
                $result = mysql_query("SELECT * FROM `oddjob` WHERE `MemberID` = $MemberID");

                while($row = mysql_fetch_assoc($result))
                {

                echo"<table width='100%' border='1' cellspacing='0' cellpadding='5'>
                <td width='50%'>
                <table width='100%' cellspacing='17' cellpadding='0'>
                <form action='' method ='post' enctype='multipart/form-data'>
                    <tr>
                        <td>
                        <input type='hidden' name='MemberID' id='MemberID' value= ". $MemberID ."> 
                        </td>
                    </tr>
                    <tr>
                        <td>
                        <input type='hidden' name='OddJobID' id='OddJobID' value= ". $row['OddJobID'] ."> 
                        </td>
                    </tr>
                    <tr>
                        <td width='35%'>
                        <p>Name of OddJob*:</p>
                        </td>
                        <td>
                        <input type='text' name='OddJobID' style='width:180px' value= ". $row['OddJobID'].">
                        </td>
                    </tr>
                    <tr>
                        <td width='35%'>
                        <p>Name of OddJob*:</p>
                        </td>
                        <td>
                        <input type='text' name='OddJobName' style='width:180px' value= ". $row['OddJobName'].">
                        </td>
                    </tr>
                    <tr>
                        <td>
                        <p>Description*:</p>
                        </td>
                        <td>
                        <div class='expandingArea'>
                        <pre><span></span><br></pre>
                        <textarea name='Description'>". $row['Description'] ."</textarea>
                        </div>
                        </td>
                    </tr>
                    <tr>
                        <td>
                        <p>Days Available(current week)*:</p>
                        <p>(hold Ctrl to select multiple)</p>
                        </td>
                        <td>
                        <select name='DaysAvailable[]' size='5' multiple='multiple' id='DaysAvailable[]'>
                        <option value='Monday'>Monday</option>
                        <option value='Tuesday'>Tuesday</option>
                        <option value='Wednesday'>Wednesday</option>
                        <option value='Thursday'>Thursday</option>
                        <option value='Friday'>Friday</option>
                        <option value='Saturday'>Saturday</option>
                        <option value='Sunday'>Sunday</option>
                        </select>
                        </td>
                </td>
                    <tr>
                        <td>
                        <input type='checkbox' name='Delete' value= ". $row['OddJobID'] .">
                        </td>
                    </tr>
                </table>
                </table>
                </table>
                <input type='submit' name='Delete' value='Delete'>
                </form> ";
                }
?>

功能:

function Delete_Oddjob ($Delete_Oddjob){
            //global $Add_Oddjob;
             array_walk($Delete_Oddjob, 'array_sanitize');
             mysql_query("DELETE FROM `oddjob` WHERE `OddJobID` = '".$_POST['OddJobID']."'") or die (mysql_error());
}

我不明白如何使这项工作。当我选中一个复选框并单击删除按钮时,页面似乎刷新,但没有其他任何事情发生。此外,如果我尝试回显查询,则不会显示任何内容。

echo "DELETE FROM `oddjob` WHERE `OddJobID` = '".$_POST['OddJobID']."'";

任何帮助都会很棒。谢谢。

1 个答案:

答案 0 :(得分:1)

你几乎有解决方案。

将删除复选框重命名为name =“delete []”,允许在一个名称下发布多个复选框,然后在PHP方面,您可以通过以下方式访问这些复选框:

if (isset($_POST['delete'])) {
    foreach($_POST['delete'] as $oddjob) {
        if (Delete_Oddjob($oddjob)) {
            // success
        } else {
            // failure
        }
    }
}

每当提交表单并选择删除选项时,这将遍历表单上所有选中的复选框,并使用其ID删除它们。