Webbserver - Love Blomberg

Show sourcecode

The following files exists in this folder. Click to view.

public_html/exercises/databas/ovn_db1/

createtable.php
deletepost.php
index.html
insertpost.php
selectposts.php
showtable.php
updatepost.php

deletepost.php

77 lines ASCII Windows (CRLF)
<!-- deletepost.php -->
<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <title>Delete</title>
</head>

<body>
    <form action="index.html" method="get"><button type="submit">Tillbaka till meny</button></form>
    <?php
    
include('../dbconnection.php');
    if (!
$dbconn) {
        die(
"Connection failed: Can't connect to database.");
    }
    
$message null;
    
$id null;

    if (isset(
$_POST['id']) && !empty($_POST['id'])) {
        
$id $_POST['id'];

        try {
            
# prepare
            
$sql "DELETE FROM kompisar WHERE id=?";
            
$stmt $dbconn->prepare($sql);
            
# the data we want to insert
            
$data = array($id);
            
# execute width array-parameter
            
$stmt->execute($data);

            
$message .= "<br />Record deleted successfully.<br />";
        } catch (
PDOException $e) {
            
$message .= $sql "<br>" $e->getMessage();
        }
    } else {
        
$message .= "<br />";
    }

    echo 
$message;

    
// Ouput table with all posts
    /*** The SQL SELECT statement ***/
    
$sql "SELECT * FROM kompisar";
    
$stmt $dbconn->prepare($sql);
    
// fetch width column names, create a table
    
$data = array();
    
$stmt->execute($data);
    
$output "<table><caption>Kompisar i table:</caption>";
    while (
$res $stmt->fetch(PDO::FETCH_ASSOC)) {
        
$idx htmlentities($res['id']);
        
$first htmlentities($res['firstname']);
        
$last htmlentities($res['lastname']);
        
$mobilex htmlentities($res['mobilenumber']);
        
$email htmlentities($res['email']);

        
$output .= "<tr>" .
            
"<td>$idx</td>" .
            
"<td>$first</td>" .
            
"<td>$last</td>" .
            
"<td>$mobilex</td>" .
            
"<td>$email</td>" .
            
"<td><form method='post' action=''>" .
            
"<input type='hidden' name='id' value='$idx'>" .
            
"<button type='submit'>Ta bort</button></form></td>" .
            
"</tr>";
    }
    
$output .= "</table>";
    echo 
"$output";

    
$dbconn null;
    
?>

</body>

</html>