0

I'm trying to create a form that displays current service information for a user on their profile and then allows them to select a check box for one or more services they provide and then hit a delete button to remove the service(s) related to the check box. Each service has a unique ID and so I'm assuming I need to use this so that the query knows what to delete.

Here is relevant code and the form I am using:

<?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:

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());
}

I don't understand how to make this work. At the moment when I select a check box and click the delete button the page seems to refresh but nothing else happens. Also If i try to echo out the query nothing is displayed.

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

Any help would be great. Thank you.

4

1 回答 1

1

你在那里几乎有解决方案。

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

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

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

于 2013-04-15T10:21:45.297 回答