arhg why wont my php script work??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Ralle
Forum Commoner
Posts: 38
Joined: Mon Oct 17, 2005 5:05 am

arhg why wont my php script work??

Post 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';
		}
?>
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post 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...
Dm7
Forum Commoner
Posts: 67
Joined: Sat Oct 08, 2005 9:16 pm
Location: USA

Post 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.
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post 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
RobertPaul
Forum Contributor
Posts: 122
Joined: Sun Sep 18, 2005 8:54 pm
Location: OCNY

Post 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 ...
ryanlwh
Forum Commoner
Posts: 84
Joined: Wed Sep 14, 2005 1:29 pm

Post by ryanlwh »

forgot to close this if statement?

Code: Select all

if ($id != NULL && $error == false)
{
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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:

Code: Select all

$rate = intval($_POST['rate']);
Or:

Code: Select all

$rate = $_POST['rate'];
settype($rate, 'int');
Ralle
Forum Commoner
Posts: 38
Joined: Mon Oct 17, 2005 5:05 am

Post 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';
		}
User avatar
dallasx
Forum Contributor
Posts: 106
Joined: Thu Oct 20, 2005 4:55 pm
Location: California

Yeah

Post 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.
Ralle
Forum Commoner
Posts: 38
Joined: Mon Oct 17, 2005 5:05 am

Post 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???
User avatar
dallasx
Forum Contributor
Posts: 106
Joined: Thu Oct 20, 2005 4:55 pm
Location: California

Post 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.
Last edited by dallasx on Sat Oct 22, 2005 12:36 pm, edited 2 times in total.
User avatar
dallasx
Forum Contributor
Posts: 106
Joined: Thu Oct 20, 2005 4:55 pm
Location: California

Another way to think of it

Post 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.
Ralle
Forum Commoner
Posts: 38
Joined: Mon Oct 17, 2005 5:05 am

Post 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.
User avatar
dallasx
Forum Contributor
Posts: 106
Joined: Thu Oct 20, 2005 4:55 pm
Location: California

Post 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";
User avatar
dallasx
Forum Contributor
Posts: 106
Joined: Thu Oct 20, 2005 4:55 pm
Location: California

Whoops... My mistake

Post 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
Post Reply