Page 1 of 2
arhg why wont my php script work??
Posted: Thu Oct 20, 2005 7:52 pm
by Ralle
this is my script. tell me why it wont work :S
Code: Select all
<?
include('connect.php');
//RATE
$id = $_POST[id];
$rate = $_POST[rate];
if ($rate == 5 || $rate == 4 || $rate == 3 || $rate == 2 || $rate == 1) {$error = false;}
if ($id != NULL && $error == false)
{
$ip = getenv('REMOTE_ADDR');
if($result = mysql_query("SELECT * FROM rating WHERE rate_ip=$ip AND rate_link_id=$id")
{
echo 'you have already rated on this link';
}
else
{
mysql_query("INSERT INTO rating (rate_ip, rate_link_id, rate_value) VALUES ('$ip', '$id', '$rate')") or die("NONONO");
echo 'inserted';
}
?>
Posted: Thu Oct 20, 2005 8:13 pm
by Charles256
what are you trying to make it do? what errors are you getting? echoed all your variables to make sure it's what you were expecting?Etc.etc...
Posted: Thu Oct 20, 2005 8:21 pm
by Dm7
if database doesn't seem to work right... try echoing your mysql_query... (both of them)... and see what happens. Or use mysql_error(); function... that's all I can tell you so far until you tell us the error.

It looks fine to me.
Posted: Thu Oct 20, 2005 8:25 pm
by Charles256
i'd feel better if he put quotes around the variables in the first query after the WHERE statement..but it's probably not nessecary:-D
Posted: Thu Oct 20, 2005 8:54 pm
by RobertPaul
Just a nitpick, but you could have saved yourself a bit of typing by checking $rate differently:
Code: Select all
if ($rate < 5 && $rate > 1) {$error = FALSE}
It's also a bit easier to read. Also, in the next if statement, I'd suggest:
Code: Select all
if(is_int($id) && !$error) { //do stuff }
You don't want to just to tossing any random vars into your SQL queries when the data's coming from the client-side.
Of course, neither of these fix your problem ... but you haven't stated what your problem is. Set error_reporting(E_ALL) and see what you get.
Looks to me, though, like you're missing a closing parenthesis after the if($result ...
Posted: Fri Oct 21, 2005 12:46 am
by ryanlwh
forgot to close this if statement?
Code: Select all
if ($id != NULL && $error == false)
{
Posted: Fri Oct 21, 2005 1:01 am
by Jenk
You also need to refer to array idices in the same context in which the key is labelled.
I.e.
Code: Select all
$id = $_POST[id];
$rate = $_POST[rate];
Should be:
Code: Select all
$id = $_POST['id'];
$rate = $_POST['rate'];
Because the keys are strings, you must refer to them as strings.
EDIT:
You could also trim down the following:
Code: Select all
if ($rate == 5 || $rate == 4 || $rate == 3 || $rate == 2 || $rate == 1) {$error = false;}
To:
Code: Select all
if (in_array($rate, range(1,5))) { error = false; }
Or:
Code: Select all
if (($rate >= 1) && ($rate <= 5)) { $error = false; }
And finally, in order to have a variable behave like a integer, you must ensure it is an integer, like so:
Or:
Code: Select all
$rate = $_POST['rate'];
settype($rate, 'int');
Posted: Sat Oct 22, 2005 11:05 am
by Ralle
Thanks for all the help. I got it to output data now. But the problem is that it always says:
"you have already rated on this link" also if the row isn't in the database..
Code: Select all
if($result = mysql_query("SELECT * FROM rating WHERE rate_ip='$ip' AND rate_link_id='$id'"))
{
echo 'you have already rated on this link';
}
else
{
mysql_query("INSERT INTO rating (rate_ip, rate_link_id, rate_value) VALUES ('$ip', '$id', '$rate')") or die("NONONO");
echo 'inserted';
}
Yeah
Posted: Sat Oct 22, 2005 11:54 am
by dallasx
The problem is with your IF statement... you're not comparing $result to the query, you're just setting $result equal to the query.
Posted: Sat Oct 22, 2005 11:57 am
by Ralle
but I want it to say true if there exist any row called rate_ip and rate_link_id thing.. how do I do that???
Posted: Sat Oct 22, 2005 12:09 pm
by dallasx
Ralle wrote:but I want it to say true if there exist any row called rate_ip and rate_link_id thing.. how do I do that???
Code: Select all
<?php
include('connect.php');
//RATE
$id = $_POST['id'];
$rate = $_POST['rate'];
if (($rate <= 5) && ($rate >= 1))
{$error = false;}
if ($id != NULL && $error == false)
{
$ip = getenv('REMOTE_ADDR');
$sql = "SELECT * FROM rating WHERE rate_ip=$ip AND rate_link_id=$id";
$result = mysql_query($sql); // Execute the query and store it in $result
$num = mysql_num_rows($result); // Get number of rows in $result. 0 if there's not a match, 1 if there's a match.
if($num != 0) // IF $num is not equal to ZERO the record is there, the record does exist, tell them that it's already there.
{
echo 'you have already rated on this link';
}
else // ELSE, there is no row then the record doesn't exist hence the rating is not there, insert it.
{
mysql_query("INSERT INTO rating (rate_ip, rate_link_id, rate_value) VALUES ('$ip', '$id', '$rate')") or die("NONONO");
echo 'inserted';
}
}
?>
Something to that extent. You have to compare two conditions in the if statement, not set something equal to something.
Another way to think of it
Posted: Sat Oct 22, 2005 12:26 pm
by dallasx
I will explain the logic in the code I posted above.
First, since you want to use an IF statement, you need to compare two conditions. In your case, you wanted to know whether or not a rating has already been submitted by someone on something based on the IP.
The SQL statement is the key here because you had two things that would make it very, very specific to find only one match which is what you want accomplish. You need to first find the record(s) that exist or don't exist. Once you've got the result stored in the variable $result, you then see how many matches (numbers of rows) that has using the mysql_num_rows(). If the number of rows equals one, then one match was found hence the record exists hence they've already rated it. If the number of rows equals 0, then the record does not exist hence they have not rated it.
Posted: Sat Oct 22, 2005 1:00 pm
by Ralle
Code: Select all
// Get number of rows in $result. 0 if invalid, 1 if valid.
$num = mysql_num_rows($result);
echo "the number is:$num";
This echos "the number is:"
means that $num is NULL at any time.
Posted: Sat Oct 22, 2005 1:14 pm
by dallasx
That this:
Code: Select all
$num = mysql_num_rows($result);
echo "the number is: ".$num;
Instead of:
Code: Select all
$num = mysql_num_rows($result);
echo "the number is:$num";
Whoops... My mistake
Posted: Sat Oct 22, 2005 1:18 pm
by dallasx
I screwed up... mysql_num_rows() returns the number of rows if it succeeds, FALSE if it fails.
So just change it to check for not FALSE