Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Nicklas Larsson Hanzén
2,234 PointsInserting 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

Ted Sumner
Courses Plus Student 17,967 PointsThere are some things in your code I have not learned yet, but you are missing a ; on the $insert line.