hi all im in the middle of following a tutorial online for a rating system and im encountering a problem when i click a number of 1/5 to rate a topic the rating is not going into the database can someone check the code to see what going off

sql tables

CREATE TABLE IF NOT EXISTS `articles` (
  `id` int(11) NOT NULL,
  `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Dumping data for table `articles`
--

INSERT INTO `articles` (`id`, `title`) VALUES
(1, 'test article one'),
(2, 'test article two'),
(3, 'test article three');

and the php code to add to tables

<?php

    require_once 'app/init.php';

much appreciated ty jan x
if(isset($_GET['article'], $_GET['rating'])) {

        $article = (int)$_GET['article'];
        $rating = (int)$_GET['rating'];

        if(in_array($rating, [1, 2, 3, 4, 5])) {

            $exists = $db->query("SELECT id FROM articles WHERE id = {$article}")->num_rows ? true : false;

            if($exists) {
                $db->query("INSERT INTO articles_ratings (article, rating) VALUES ({$article}, {$rating})");
            }
        }

        header('location: article.php?id=' . $article);
    }
?>




CREATE TABLE IF NOT EXISTS `articles_ratings` (
  `id` int(11) NOT NULL,
  `article` int(11) NOT NULL,
  `rating` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Hi,

it seems fine to me. Make sure the database connection is available by adding an error check:

if ($db->connect_errno) {
    die('Connect Error: ' . $db->connect_errno);
}

Check also if you get through each IF condition by placing something like die('stop at ' . __LINE__); and so on after each condition, so you can see where the script stop its execution, for example:

if(isset($_GET['article'], $_GET['rating'])) {

    $article = (int)$_GET['article'];
    $rating = (int)$_GET['rating'];

    if(in_array($rating, [1, 2, 3, 4, 5])) {

        $exists = $db->query("SELECT id FROM articles WHERE id = {$article}")->num_rows ? true : false;

        if($exists) {
            $db->query("INSERT INTO articles_ratings (article, rating) VALUES ({$article}, {$rating})");
            die('stop at ' . __LINE__);
        }

        die('stop at ' . __LINE__);
    }

    die('stop at ' . __LINE__);
    header('location: article.php?id=' . $article);
}

die('stop at ' . __LINE__);

You could also add the third parameter to the in_array() function, it's boolean TRUE and it is used to enable a type check, so:

if(in_array($article, range(1, 5), TRUE) {

Bye!

The code if($exists) only it insert the data. Shouldn't it be if(!$exists)?

No, she's checking if the article exists in the articles table, if TRUE then she adds the rating to the articles_rating table.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.