显示表并提供删除选项

时间:2015-04-10 12:53:19

标签: php mysql

我希望大家看到我的桌子的条目,并且有可能删除一行,

正如您所看到的,我希望用户可以单击“删除”并将其从数据库中删除:)

这是我到目前为止所得到的:

function editRooster() {
    if(isset($_POST['submit'])) {   
        if(isset($_POST['delete_id'])) {
        $delete_id = mysql_real_escape_string($_POST['delete_id']);
        mysql_query("DELETE FROM event WHERE `id`=".$delete_id);
        header('Location: dashboard.php');
        }
    }
    else {

    echo '<h1><a href="dashboard.php">Het terras</a> &rsaquo; <a href="dashboard.php?app=mysql">Roosters</a> &rsaquo; <a href="dashboard.php?app=mysql&action=editrooster">Wijzigen</a></h1>';
        $connection = mysqlConnect();

 // Find out how many items are in the table
    $total = $connection->query('SELECT COUNT(*) FROM intranet_users')->fetchColumn();

    // How many items to list per page
    $limit = 20;

    // How many pages will there be
    $pages = ceil($total / $limit);

    // What page are we currently on?
    $page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
        'options' => array(
            'default'   => 1,
            'min_range' => 1,
        ),
    )));

    // Calculate the offset for the query
    $offset = ($page - 1)  * $limit;

    // Some information to display to the user
    $start = $offset + 1;
    $end = min(($offset + $limit), $total);

    // The "back" link
    $prevlink = ($page > 1) ? '<a href="?app=users&page=1" title="Eerste pagina">&laquo;</a> <a href="?app=users&page=' . ($page - 1) . '" title="Vorige pagina">&lsaquo;</a>' : '<span class="disabled">&laquo;</span> <span class="disabled">&lsaquo;</span>';

    // The "forward" link
    $nextlink = ($page < $pages) ? '<a href="?app=users&page=' . ($page + 1) . '" title="Volgende pagina">&rsaquo;</a> <a href="?app=users&page=' . $pages . '" title="Laaste pagina">&raquo;</a>' : '<span class="disabled">&rsaquo;</span> <span class="disabled">&raquo;</span>';

    // Prepare the paged query
    $stmt = $connection->prepare('SELECT * FROM event ORDER BY id LIMIT :limit OFFSET :offset');

    // Bind the query params
    $stmt->bindParam(':limit', $limit, PDO:: PARAM_INT);
    $stmt->bindParam(':offset', $offset, PDO:: PARAM_INT);
    $stmt->execute();

    // Do we have any results?
    if ($stmt->rowCount() > 0) {
        // Define how we want to fetch the results
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $iterator = new IteratorIterator($stmt);

        // Display the results
        echo '<table><thead><td>ID:</td><td>Voornaam:</td><td>Datum</td><td>Vanaf</td><td>Tot</td><td>Omschrijving</td><td>Wijzig</td><td>Verwijder</td></thead>';
        foreach ($iterator as $row) {
            echo '<tr><td>';
            echo $row['id'];
            echo '<td>';
            echo $row['firstname'];
            echo '</td><td>';
            echo (new \DateTime($row['date']))->format('d-m-Y');
            echo '</td><td>';
            echo $row['begintijd'];
            echo '</td><td>';

        echo $row['eindtijd'];
        echo '</td><td>';
        echo $row['omschrijving'];
        echo '</td><td>';
        echo '<input type="submit" value="Edit">';
        echo '</td><td>';
        echo '<form method="post">';
        echo '<input type="hidden" name="delete_id" value="'.$row['id'].'" />';
        echo '<input type="submit" value="Delete">';
        echo '</form>'; 

    }
    echo '</table>';
}
} 
}

很抱歉这个功能太长了,但我不知道出了什么问题,所以我必须发布整个功能。请原谅我的长代码:)

2 个答案:

答案 0 :(得分:0)

变化

echo '<input type="submit" value="Delete">';

echo '<input type="submit" name = "submit" value="Delete">';

答案 1 :(得分:0)

您的$connection是PDO对象。

尝试以这种方式调用您的删除查询:

$query = "DELETE FROM event WHERE id=?";
$stmt = $connection->prepare($query);
$stmt->bindParam(1, $delete_id);
$stmt->execute();

您也不需要使用$delete_id来逃避mysql_real_escape_string()的价值,因为它是一份准备好的陈述。

(在PHP聊天室中进行调试讨论后写的答案)。