I have to build a php website that outputs a list of movies which I am capable of doing. I am having trouble to update another table with PDO connexion
using phpmyadmin. I am so new to php that I am learning as I'm coding.
I am using a select
query to show a list of movies' title and rating. | works fine
I'm offering a user to add a new rating to a movie of his choice through inputs. | output is true all the time or false all the time
The only hint regarding my attempt to update my table is that my if block
tells me with the code below that $result is false
.
main.php
<form method="post" action="controller.php">
<input type="number" name="data_rating" placeholder="Between 0 and 100">
<input type="text" name="data_movie_id" placeholder="Movie's ID">
<input type="text" name="data_comment" placeholder="Comment">
<input type="submit" name="data_new_rating" value="Add rating">
</form>
controller.php
// First if is $_POST['connexion_to_database'] with user's ID and password
else if(isset($_POST['data_new_rating']))
{
add_rating();
}
model.php
function add_rating()
{
$rating = $_POST['data_rating'];
$comment= $_POST['data_comment'];
$movieID = $_POST['data_movie_id'];
try
{
$conn = new PDO("my connexion string");
$output = $conn->prepare("INSERT INTO rating(rating, comment, date, clientID, movieID)
VALUES (:rating, :comment, :date, :clientID, :movieID)";
$result = $output->execute(array(
':rating' => $rating,
':comment' => $comment,
':date' => date("Y-m-d"),
':clientID' => $_SESSION["id"],
':movieID' => $movieID
));
if($result){
echo "Movie rated successfully!";
}else{
echo "Movie has not been rated...";
}
$conn = null;
// header("Location:main.php");
// I have commented this ^ because I need to see which echo is showing for debugging purpose.
} catch { } ...
}