 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
12-21-2007, 02:31 PM
|
#1 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
(HELP!!!!!!!!) Creating a 1 - 5 Star rating script
For now I just made it say Rating Successful and Unsuccessful, and query.
PHP Code:
<style type="text/css"> input.ratingsnstuff { background:url('rate/default.png') no-repeat; border:none; width:16px; height:16px; margin:1px; cursor: pointer; } input:hover.ratingsnstuff { background:url('rate/default_hover.png') no-repeat; border:none; width:16px; height:16px; margin:1px; cursor: pointer; } </style> <form class="ratingsnstuff" action="rating.php" method="post"> <input class="ratingsnstuff" name="star1" type="button" value=""> <input class="ratingsnstuff" name="star2" type="button" value=""> <input class="ratingsnstuff" name="star3" type="button" value=""> <input class="ratingsnstuff" name="star4" type="button" value=""> <input class="ratingsnstuff" name="star5" type="button" value=""> </form> <?php
$dbhost = "localhost"; $dbuser = "root"; $dbpass = ""; $database = "rates";
mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error()); mysql_select_db($database) or die(mysql_error());
function ratingInsert($ratings) { $rateid = intval($_REQUEST['rateid']); $rating = array($_POST['star1'], $_POST['star2'],$_POST['star3'], $_POST['star4'], $_POST['star5']); foreach($rating as $ratings) { $query = mysql_query("Insert into db_ratings WHERE rateid=".$rateid." and rate=".$ratings."");
if($query!=null) { echo "Rated successfully!"; } elseif(!$query) { echo 'Rated Unsuccessfully!'; } } }
$ratingInsert = ratingInsert($ratings); return $ratingInsert;
?>
This outputted:
Code:
Rated Unsuccessfully!Rated Unsuccessfully!Rated Unsuccessfully!Rated Unsuccessfully!Rated Unsuccessfully!
Please help!
UPDATE!!!!
Code:
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 'WHERE rateid=0 and rate=' at line 1
No idea what this means :[
|
|
|
|
12-21-2007, 02:57 PM
|
#2 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Is there a better way to group the ratings? And make this work! :[
I've updated more and more of the sql and experimented more but I cannot find the right solution. -.-
|
|
|
|
12-21-2007, 03:01 PM
|
#3 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
You arent inserting any values on your foreach loop, thats the SQL error. You have no separation between the PHP and HTML, it will execute when you load the page. You will want to put the code outside of a function in a different file. Lastly, why the return statement outside of a function?
|
|
|
|
12-21-2007, 03:02 PM
|
#4 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Quote:
Originally Posted by Village Idiot
You arent inserting any values on your foreach loop. You have no separation between the PHP and HTML, it will execute when you load the page. Lastly, why the return statement outside of a function?
|
I need to be better guided in PHP is what I need but umm, in foreach I tried that and well it still seems to output the same thing, I dunno. :/ Care to give me any examples or anything? -_-
|
|
|
|
12-21-2007, 03:07 PM
|
#5 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
If thats the case, you need to learn mysql a bit better. Go here for that.
For the SQL, here is the proper syntax
Code:
INSERT INTO `table` VALUES ('each','row','here','in','order')
edit: Just woke up, there is no WHERE in INSERT's syntax
|
|
|
|
|
The Following User Says Thank You to Village Idiot For This Useful Post:
|
|
12-21-2007, 03:18 PM
|
#6 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
THANKS! But could you help me fix this anyway? Please :D?
|
|
|
|
12-21-2007, 03:29 PM
|
#7 (permalink)
|
|
The Prestige
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
|
read the sql error, its quite informative (unlike some errors from languages i've used):
Code:
'WHERE rateid=0 and rate='
and as villageidiot said b4, there isnt a 'where' in 'insert'
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
|
|
|
|
12-21-2007, 03:29 PM
|
#8 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
Something like this should work
form.html
HTML Code:
<style type="text/css">
input.ratingsnstuff {
background:url('rate/default.png') no-repeat;
border:none;
width:16px;
height:16px;
margin:1px;
cursor: pointer;
}
input:hover.ratingsnstuff {
background:url('rate/default_hover.png') no-repeat;
border:none;
width:16px;
height:16px;
margin:1px;
cursor: pointer;
}
</style>
<form class="ratingsnstuff" action="action.php" method="post">
<input class="ratingsnstuff" name="star" type="button" value="1">
<input class="ratingsnstuff" name="star" type="button" value="2">
<input class="ratingsnstuff" name="star" type="button" value="3">
<input class="ratingsnstuff" name="star" type="button" value="4">
<input class="ratingsnstuff" name="star" type="button" value="5">
</form>
Note the values I put in there, without that, how does the database know whats been rated? Also, give then the same name, only one can be clicked after all.
action.php
PHP Code:
<?php
$dbhost = "localhost"; $dbuser = "root"; $dbpass = ""; $database = "rates";
mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error()); mysql_select_db($database) or die(mysql_error());
$rateid = intval($_REQUEST['rateid']); $rating = $_POST["star"]; //lets say the ratings table has two fields, ID (primary key, auto_incriment) and rating. $query = mysql_query("Insert into db_ratings VALUES (NULL,'$rating')") OR die(mysql_error());
echo "rating sucessful"; ?>
This doesnt need to be in a function, and the rating is all one name so no need for the foreach loop. This may not be valid HTML however.
Insetad of two if statements, just tell the query do die the mysql error if unsuccessful and echo if it passes (Die ends the page, should be taken out after debugging)
Putting a return statement outside of a function will generate a syntax error.
|
|
|
|
12-21-2007, 03:31 PM
|
#9 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Quote:
Originally Posted by Village Idiot
Something like this should work
form.html
HTML Code:
<style type="text/css">
input.ratingsnstuff {
background:url('rate/default.png') no-repeat;
border:none;
width:16px;
height:16px;
margin:1px;
cursor: pointer;
}
input:hover.ratingsnstuff {
background:url('rate/default_hover.png') no-repeat;
border:none;
width:16px;
height:16px;
margin:1px;
cursor: pointer;
}
</style>
<form class="ratingsnstuff" action="action.php" method="post">
<input class="ratingsnstuff" name="star" type="button" value="1">
<input class="ratingsnstuff" name="star" type="button" value="2">
<input class="ratingsnstuff" name="star" type="button" value="3">
<input class="ratingsnstuff" name="star" type="button" value="4">
<input class="ratingsnstuff" name="star" type="button" value="5">
</form>
Note the values I put in there, without that, how does the database know whats been rated? Also, give then the same name, only one can be clicked after all.
action.php
PHP Code:
<?php
$dbhost = "localhost"; $dbuser = "root"; $dbpass = ""; $database = "rates";
mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error()); mysql_select_db($database) or die(mysql_error());
$rateid = intval($_REQUEST['rateid']); $rating = $_POST["star"]; //lets say the ratings table has two fields, ID (primary key, auto_incriment) and rating. $query = mysql_query("Insert into db_ratings VALUES (NULL,'$rating')") OR die(mysql_error());
echo "rating sucessful"; ?>
This doesnt need to be in a function, and the rating is all one name so no need for the foreach loop. This may not be valid HTML however.
Insetad of two if statements, just tell the query do die the mysql error if unsuccessful and echo if it passes (Die ends the page, should be taken out after debugging)
Putting a return statement outside of a function will generate a syntax error.
|
Wow, you took my script and simplified it! Great; thanks man!
UPDATE: It still says Rated Successful even though I have not submitted one of the stars yet.
|
|
|
|
12-21-2007, 03:35 PM
|
#10 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
It takes you to action.php without you clicking anything?
|
|
|
|
|
The Following User Says Thank You to Village Idiot For This Useful Post:
|
|
12-21-2007, 03:41 PM
|
#11 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Quote:
Originally Posted by Village Idiot
It takes you to action.php without you clicking anything?
|
Well no, but it doesn't redirect to action.php when trying to submit the input.
|
|
|
|
12-21-2007, 03:49 PM
|
#12 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
Try changing yout html to
HTML Code:
<form class="ratingsnstuff" name="rating" action="action.php" method="post">
<input class="ratingsnstuff" name="star" type="button" value="1" OnClick="document.rating.submit();">
<input class="ratingsnstuff" name="star" type="button" value="2" OnClick="document.rating.submit();">
<input class="ratingsnstuff" name="star" type="button" value="3" OnClick="document.rating.submit();">
<input class="ratingsnstuff" name="star" type="button" value="4" OnClick="document.rating.submit();">
<input class="ratingsnstuff" name="star" type="button" value="5" OnClick="document.rating.submit();">
|
|
|
|
|
The Following User Says Thank You to Village Idiot For This Useful Post:
|
|
12-21-2007, 03:52 PM
|
#13 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Quote:
Originally Posted by Village Idiot
Try changing yout html to
HTML Code:
<form class="ratingsnstuff" name="rating" action="action.php" method="post">
<input class="ratingsnstuff" name="star" type="button" value="1" OnClick="document.rating.submit();">
<input class="ratingsnstuff" name="star" type="button" value="2" OnClick="document.rating.submit();">
<input class="ratingsnstuff" name="star" type="button" value="3" OnClick="document.rating.submit();">
<input class="ratingsnstuff" name="star" type="button" value="4" OnClick="document.rating.submit();">
<input class="ratingsnstuff" name="star" type="button" value="5" OnClick="document.rating.submit();">
|
Thanks! That worked! But it's a bit slow but it was successful! :D
|
|
|
|
12-24-2007, 04:55 PM
|
#14 (permalink)
|
|
The Frequenter
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
|
If you want, you can modify the script using real javascript, instead of the document.name.action format. Should get rid of 'slow' performance which you are talking about.
__________________
"Life is a bitch, take that bitch on a ride"
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|