문제

Here is the table below that I'm trying to delete rows out of:

<form method="POST" >
        <table class="sortable">
            <thead>
                <tr>
                    <th id="makehead">Make </th>
                    <th id="modelhead">Model </th>
                    <th id="idhead">Delete </th>
                </tr>
            </thead>
            <tbody>
                <?php
                $i = 0;

                foreach ($carArray as $k => $carInfo) {
                    $i++;
                    echo '<tr>';
                    if ($i % 2) {
                        echo '<td class="make">' . $carInfo['make'] . '</td>
                              <td class="model">' . $carInfo['model'] . '</td>
                              <td class="id"><input type="checkbox" name="id" value="' . $carInfo['id'] . '">' . $carInfo['id'] . '</td>';
                    } else {
                        echo '<td class="makelight">' . $carInfo['make'] . '</td>
                              <td class="modellight">' . $carInfo['model'] . '</td>
                              <td class="idlight"><input type="checkbox" name="id" value="' . $carInfo['id'] . '">' . $carInfo['id'] . '</td>';
                    }
                }
                ?>
                </tr>

        </table>

    </tbody>
    <td>
        <input Onclick="return ConfirmDelete();" name="delete" type="submit" id="delete" value="Delete"></input>
    </td>
</table></form>

As you can see i'm using checkboxes to tick each row then the delete button will have a confirm message then should delete but it doesn't here is my if statement:

if ($_REQUEST['delete']) {
        $dbid = $_REQUEST['id'];
        $db->setdbid($dbid);

So when this wasn't working I had a look on here and on other questions people said I need a setter function so i did this: EDIT: this is my class file.

public function setdbid($dbid){
    $this->dbid=$dbid;
}

for this main function to delete things:

 public function delete($dbid) {
    try {
        $sql = "DELETE FROM cars WHERE id = '$dbid'";
        $this->db->exec($sql);

        echo "Car has been deleted.";
    } catch (PDOException $e) {
        echo $e->getMessage();
    }
}

So that's all the relevant code I think, please help me if you can.

도움이 되었습니까?

해결책

You just have to replace some piece of code in PHP :

if ($_REQUEST['delete']) {
    $dbid = $_REQUEST['id'];
    $db->delete($dbid); //Assuming delete is well a $db method, else replace it by the correct delete call
}

다른 팁

As you are using checkboxes with the same name, you have to change it so it's an array (and this way you'll be able to delete multiple rows at once) :

<td class="id"><input type="checkbox" name="ids[]" value="' . $carInfo['id'] . '">' . $carInfo['id'] . '</td>';

Then in your php code, treat this data as such :

if ($_REQUEST['delete']) {
     foreach($_REQUEST['ids'] as $id){
         $xxx->delete(intval($id)); //convert to integer to avoid sql injection.
     }
}

Note that you don't need to set $db->setdbid since you pass that id as a parameter of your delete method.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top