I can't figure out why my code isn't working. I'm trying to insert a dynamic variable to a table in my database with regular input in a form.
Like this, here is my query code:
if (isset($_POST['submit'])) {
// Check if movie allready is added.
$stmt = $dbh->prepare("SELECT * FROM watchlist WHERE movie_title='{$_POST['$title']}'");
$stmt->bindParam(':movie_title', $_POST[$movie->title]);
$stmt->execute();
$rows = $stmt->fetchALL();
$n = count($rows);
if($n > 0){
echo 'Du är redan peppad på filmen!';
}
$sql = $dbh->prepare("INSERT INTO watchlist(movie_title, movie_des, movie_link)
VALUES ('{$_POST['title']}', '{$_POST['description']}', '{$_POST['link']}'");
$sql->bindParam(':title', $_POST['title']);
$sql->bindParam(':description', $_POST['description']);
$sql->bindParam(':link', $_POST['link']);
$sql->execute();
header("Location: ../index.php");
exit;
}
And here is my form:
$title = $movie->title;
$description = $movie->description;
$link = $movie->link;
echo '<div class="view">';
echo '<h3>' . $title . '</h3>';
echo $description . '<br/>';
echo '<a href="'. $link . '" target="_blank">Läs mer...</a><br/><br/>';
echo '<a href="index.php">Tillbaka</a>';
echo '<div class="form_dis"></div>';
echo '<div class="form_content">';
echo '<form action="queries/insert.php" method="post">',
'<input type="hidden" name="title" value="$title">',
'<input type="hidden" name="description" value="$description">',
'<input type="hidden" name="link" value="$link">',
'<input type="submit" name="submit" value="Peppa!">',
'</form></div>';
I get send back to my index page but nothing has been added to the database and I just can't figure out why. Is there anything wrong with my code that I'm missing?