Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Inserting into database not working

Hi guys. I really need help with this, I've been at it for hours and i am honestly about to give up.

I cant get this code to insert information into my database for the life of me! Does anyone know what i'm doing wrong? I'm getting no errors, but nothing's happening either!

<?php 
require_once 'login.php';
require_once 'security.php';

$bibliotek = array();

if(!empty($_POST)) {
    if(isset($_POST['title'], $_POST['director'], $_POST['year'])) {

        $title = trim($_POST['title']);
        $director = trim($_POST['director']);
        $year = trim($_POST['year']);
        $category_id = trim($_POST['category_id']);

        if(!empty($title) && !empty($director) && !empty($year)) {
            $insert = $db->prepare("INSERT INTO `movies`.`filmer` (`id`, `title`, `director`, `year`, `category_id`) 
            VALUES (NULL, '?', '?', '?', '?')");
            $insert->bind_param('ssi', $title, $director, $year, $category_id);

            if($insert->execute()) {
                header('Location: index.php');
                die();
            }

        }

    }
}

if($results = $db->query("SELECT * FROM filmer")) {
    if($results->num_rows) {
        while($row = $results->fetch_object()) {
            $bibliotek[] = $row;
        }
        $results->free();
    }
}
?>

<!doctype html>
<html lang="sv">
<head>
    <meta charset="UTF-8">
    <title>Bibliotek</title>
</head>
<body>

    <h3>Filmer</h3>

    <?php 
        if(!count($bibliotek)) {
            echo 'Inga filmer i databasen';
        } else {
    ?>
        <table border="1">
            <thead>
                <tr>
                    <th>Titel</th>
                    <th>Regissör</th>
                    <th>År</th>
                    <th>Kategori</th>
                    <th>Uppdatera</th>
                    <th>Radera</th>
                </tr>
            </thead>
            <tbody>
            <?php
            foreach($bibliotek as $b) {
            ?>
                <tr>
                    <td><?php echo esc($b->title); ?></td>
                    <td><?php echo esc($b->director); ?></td>
                    <td><?php echo esc($b->year); ?></td>
                    <td><?php echo esc($b->category_id); ?></td>
                    <td>Uppdatera</td>
                    <td>Radera</td>
                </tr>
            <?php 
            }
            ?>
            </tbody>
        </table>
    <?php 
    }
    ?>

    <form action="index.php" method="post">

            <p><input type="radio" id="action" name="category_id">
            <label for="action">Action</label></p>

            <p><input type="radio" id="deckare" name="category_id">
            <label for="deckare">Deckare</label></p>

            <p><input type="radio" id="drama" name="category_id">
            <label for="drama">Drama</label></p>

            <p><input type="radio" id="fantasy" name="category_id">
            <label for="fantasy">Fantasy</label></p>



            <p><label for="title">Titel: </label>
            <input type="text" id="title" name="title"></p>

            <p><label for="director">Regissör: </label>
            <input type="text" id="director" name="director"></p>

            <p><label for="year">År: </label>
            <input type="text" id="year" name="year"></p>

            <input type="button" value="Lagra">

    </form>

</body>
</html>
<?php // inloggnings uppgifter (login.php)
    $db = new mysqli('localhost', 'root', '', 'movies');

    if($db->connect_errno) {
        die("Kunde inte ansluta till databasen.");
    }
?>
<?php // Förhindra XSS attacker (security.php) 
    function esc ($string) {
        return htmlentities(trim($string), ENT_QUOTES, 'UTF-8');
    }
?>

If you can help me it would mean the world to me. I've been struggling with this task since Tuesday and i haven't even been able to insert data yet!

1 Answer

There are some things in your code I have not learned yet, but you are missing a ; on the $insert line.