0

I am trying to create a very basic star rating system for a simple website. I found this tutorial : Creating 5 Star Rating System With PHP , MySQL ,Jquery And Ajax

however, when I run the same code, I keep getting the following error an no votes get inserted into the database:

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-databaseName.ratings WHERE id='1'' at line 1

This is the code for the page to insert the ratings:

<?php
include("settings.php");
connect();
$ids=array(1,2,3);
?>
<html>
<head>
<script src="jquery.js" type="text/javascript"></script>
<link rel="stylesheet" href="rating.css" />
<script type="text/javascript" src="rating.js"></script>
</head>
<body>
<?php
for($i=0;$i<count($ids);$i++)
    {
        $rating_tableName     = 'ratings';
     $id=$ids[$i];
 $q="SELECT * FROM ratings WHERE id='$id'";
$r=mysql_query($q);
echo $q;
if(!$r) echo mysql_error();
$rat = 0;
$v = 1;    // In case there are no records.
while($row=mysql_fetch_array($r))
{
    $v = $row['total_votes'];
    $tv = $row['total_value'];
    $rat = $tv/$v;
}



    $j=$i+1;
    $id=$ids[$i];
echo'<div class="product">
           Rate Item '.$j.'
            <div id="rating_'.$id.'" class="ratings">';
                for($k=1;$k<6;$k++){
                    if($rat+0.5>$k)$class="star_".$k."  ratings_stars ratings_vote";
                    else $class="star_".$k." ratings_stars ratings_blank";
                    echo '<div class="'.$class.'"></div>';
                    }
                echo' <div class="total_votes"><p class="voted"> Rating: <strong>'.@number_format($rat).'</strong>/5 ('.$v. '  vote(s) cast) 
            </div>
        </div></div>';}
?>
</body></html>

I did try everything that i thought might solve the issue but nothing worked;

I tried to add back slashes and single quotes for the $id and it still throws that error.

I also echoed $q as mentioned on that page and i get the exact same thing that was mentioned on that page!

the only thing that I am a bit suspicious about is the fact that it is not showing the full database name in the error message.

the database name is like this: CL12-databaseName but in the error above its showing the database as -databaseName and i'm not sure if it suppose to do that or is that something that is causing the error ?

any help would be appreciated.

Thanks

Community
  • 1
  • 1
user3806613
  • 510
  • 2
  • 11
  • 25
  • => `-databaseName.ratings` that contains a hyphen. SQL's figuring you want to do math (as in minus). Wrap it in backticks or use an underscore. – Funk Forty Niner Aug 18 '14 at 14:07
  • @Fred-ii- wrap what in backticks? the databasename? – user3806613 Aug 18 '14 at 14:09
  • Database and/or table, whatever contains the hyphen. A hyphen to SQL means `minus`. TBH, I'm having a bit of trouble following your code, but I know an SQL error when I see one ;) Use ticks `\`` around `CL12-databaseName` or change your structure to use underscores instead. I.e.: `CL12_databaseName` and you should be good to go. – Funk Forty Niner Aug 18 '14 at 14:10
  • okay Thanks. its not my code! its just a mess. but it will give me a head start to create what I'm after. – user3806613 Aug 18 '14 at 14:13
  • That didn't work! when I put the backticks around database name i get access denied error. – user3806613 Aug 18 '14 at 14:23
  • Well, errors in a way means some form of success. Don't take errors as being totally bad. Most likely a DB connection issue at this point. – Funk Forty Niner Aug 18 '14 at 14:24
  • @Fred-ii- LoVe your theory :D – user3806613 Aug 18 '14 at 14:34
  • I've seen quite a few questions with an access denied error, which usually stems from using the wrong DB and/or a mix of incorrect username/passwords. – Funk Forty Niner Aug 18 '14 at 14:39

0 Answers0