0

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 { } ...
}
pensum
  • 980
  • 1
  • 11
  • 25
  • 1
    _SMall Point_ PDO connects to a `MySQL` database and no `phpmyadmin` which is a tool written in PHP for mainitaining a MySQL database – RiggsFolly Nov 13 '19 at 16:16
  • $_POST['data_new_rating'] is always going to be null because it has no value – Clint Nov 13 '19 at 16:18
  • So which message do you see from that IF – RiggsFolly Nov 13 '19 at 16:24
  • 1
    When `$result` is false, try `var_dump($output->errorInfo());` and see if that gives you any information. (edit, forgot it was an array, not a string) – aynber Nov 13 '19 at 16:25
  • 1
    You could try changing `placeholder="Add rating"` to `value="Add rating"` as SUBMIT inputs (buttons) dont have a `placeholder` attribute – RiggsFolly Nov 13 '19 at 16:27
  • **If the error is** `INSERT command denied to user '***'@'localhost' for table 'rating''` Looks like you didnt setup the MySQL user account properly. Check who you are connecting as and then check that accounts privilages – RiggsFolly Nov 13 '19 at 16:36
  • I have checked with `show grants for user` and it tells me I am allowed to update on db.rating... @RiggsFolly – pensum Nov 13 '19 at 16:37
  • SImple test would be to connect with `root` and its password if you have set one. If it runs then, you know you made a woopsie when creating this other account – RiggsFolly Nov 13 '19 at 16:44
  • @RiggsFolly I am logging in as a "client" in the database and that triggers a procedure that creates his account privileges. I will try to manually create a "root" client and come back to you. – pensum Nov 13 '19 at 16:47
  • If you dont have access to the database as the `ROOT` (SUPERUSER) user, my above suggestion will not work. – RiggsFolly Nov 13 '19 at 16:48
  • Thats a reather odd way of doing things. Normally you would setup a single user with the required access and privilages and use that in your web site code to connect to a database. – RiggsFolly Nov 13 '19 at 16:51
  • @aynber Can you post your comment about `var_dump` as an answer so that I can close this question? I have added the `insert` privileges so now it works because I have had more information about what was the issue. – pensum Nov 13 '19 at 17:33
  • : "It looks like you're writing your own ORM. Have you considered using one that's already written, tested, and widely supported like [RedBeanPHP](https://redbeanphp.com/), [Doctrine](http://www.doctrine-project.org/), [Propel](http://propelorm.org/) or [Eloquent](https://laravel.com/docs/master/eloquent)?" – tadman Nov 13 '19 at 18:24
  • @tadman It is for an assignment for school. But thanks anyways. I did not know that. – pensum Nov 13 '19 at 18:31
  • Learning's great, but it's important to know what tools exist so you don't have to re-invent the wheel unless you're in a wheel-building class. – tadman Nov 13 '19 at 18:33

1 Answers1

-1

I was having trouble finding out what was the problem with my code so I have tried using the infamous echo for debugging purpose. It wasn't successful so I had to ask for help.

if($result) {
    echo "Movie rated successfully!";
}
else {
    echo "Movie has not been rated...";
}

A helper told me to use this : echo var_dump($output->errorInfo()) in case of $result being false;

I got the message that I was looking for :
                           INSERT command denied to user 'user'@'localhost' for table 'rating'

By going back to my database, I found that I granted the SELECT and UPDATE privileges to the user but not INSERT. That was the issue...

pensum
  • 980
  • 1
  • 11
  • 25