vendredi 11 mars 2016

Remove column from SQLite

I´m looking for a example for deleting columns from my database. I can currently add a new column from my code, but I cant figure out how to add an delete option. I´m thinking about having the delete button in the same place I have the "Tillbaka" button.

<?php   
    $db = new PDO("sqlite:nyheter.sqlite"); 

    if(!empty($_GET["id"])){
        $idGet = htmlspecialchars($_GET["id"]);
    }
    else{
        $idGet = null;
    }


    if(!empty($_POST['title']) && !empty($_POST['text']) && !empty($_POST['author'])){
        $titlePost = htmlspecialchars($_POST['title']);
        $textPost = htmlspecialchars($_POST['text']);
        $authorPost = htmlspecialchars($_POST['author']);

        $stmt = $db->prepare("INSERT INTO news(title, text, author) VALUES('{$titlePost}','{$textPost}','{$authorPost}')"); 
        $stmt->execute();   // här kör vi frågan som vi förberett
        echo"Nyhet är inmatat";
    }
    ?>

    <doctype html>
    <html lang="sv">
        <head>
        <meta charset="utf-8">
        <title> Vecka 10 inmatning</title>
        </head>

        <body>
        <h1>Vecka 10 - inmatning i nyhetsdatabasen</h1>

    <?php
    if(!$idGet){        
        $stmt = $db->prepare("SELECT title, id FROM news"); 
        $stmt->execute();   

    while($news = $stmt->fetch())
    {
        echo "<h2><a href='Inmatning.php?id={$news['id']}'>{$news['title']}</a></h2>";
    }
    }   
    else{
    $stmt = $db->prepare("SELECT * FROM news WHERE id = '{$idGet}'");
        $stmt->execute();
        $news = $stmt->fetch();

        echo<<<Nyhet
        <h2>{$news['title']}</h2>
        <h3>{$news['date']}</h3>
        <h4>{$news['text']}</h4>
        <h5>{$news['author']}</h5>
        <a href="Inmatning.php">Tillbaka</a>

Nyhet;

    }
    ?>

            <form action="Inmatning.php" method="post">
            <p><label>title:</label></p><input type="text" name="title" placeholder="skriv titel här..."></p>
            <p><label>Text:</label></p><input type="text" name="text" placeholder="skriv text här..."></p>
            <p><label>Författare:</label></p><input type="text" name="author" placeholder="skriv författare här..."></p>
            <p><input type="submit" value="skicka"></p>

        </body>
    </html>

Aucun commentaire:

Enregistrer un commentaire