Jump to content

$_GET[] not working properly


webdogjcn

Recommended Posts

okay so in one page I have a script that looks something like this:
<a href='http://mysite.com/members/upload/files/filepage.htm?id=$fileid'>$filename</a>

And so when the user clicks the link it goes to:
[a href=\"http://mysite.com/members/upload/files/filepage.htm?id=3\" target=\"_blank\"]http://mysite.com/members/upload/files/filepage.htm?id=3[/a]

And filpage.htm contains a line that looks like this to get the value of 'id'
$id = $_GET['id'];
$sql = mysql_query("SELECT * FROM user_files WHERE file_id='$id'") or die ('Unknown file ID');

I am using PHPMyAdmin so I know that there is a place where file_id inside of user_files is equal to 3. Unfortunately I keep getting the die command 'Unknown file ID'. So, I am thinking that $_GET['id']; is not actually returning the value stored in the URL which is 3
Link to comment
Share on other sites

woops...sorry. I have a line in filepage.htm like this:
<?php include('http://mysite.com/filepage.php'); ?>

and then the $_GET code is in the filepage.php file

Crayon_violet: I will try what you suggested and repost the error.



This time I got no error at all. Nothing showed up whatsoever, and I know that there is information in that row on my database.

Here is the filepage.php coding
[code]<?php
$name="";
$lock="";
$database="";

$id = @$_GET['id'];

mysql_connect("",$name,$lock);
@mysql_select_db($database) or die( "Unable to select database");
$sql = mysql_query("SELECT * FROM user_files WHERE file_id='$id'") or die or die (mysql_error());
$info = mysql_fetch_array( $sql );
$content = $info['file_desc'];
$thumburl = $info['file_pic'];


if ($content='')
   {echo "The user has not supplied a description for this file"; }
else
   {echo "$content";}
?>[/code]
Link to comment
Share on other sites

you aren't getting any error messages cuz you don't have anything reporting them:

mysql_connect("",$name,$lock);

you probably need to put something like "localhost" in that first argument, and also throw in the mysql_error in there too:

mysql_connect("localhost",$name,$lock) or die(mysql_error();

... unless you took that out for posting purposes...

and this:

$sql = mysql_query("SELECT * FROM user_files WHERE file_id='$id'") or die or die (mysql_error());

should really be this:

$sql = mysql_query("SELECT * FROM user_files WHERE file_id='$id'") or die(mysql_error());

(you have 2 "or die" in there)

also try echoing $id after you set it:

$id = $_GET['id'];
echo $id;

see if it's printing out the id. maybe the id is not being passed, so nothing is being selected.
Link to comment
Share on other sites

Alright I played around with the code some and here's what I have (filepage.php):
[code]<?php
$name="";
$lock="";
$database="";

$id = @$_GET['id'];

echo "$id";

mysql_connect("",$name,$lock) or die(mysql_error());
@mysql_select_db($database) or die( "Unable to select database");
$sql = "SELECT * FROM user_files WHERE file_id='$id'";
$query = mysql_query($sql);
$info = mysql_fetch_array($query);
$content = $info['file_desc'];
$thumburl = $info['file_pic'];


if ($content='0')
   {echo "The user has not supplied a description for this file"; }
else
   {echo "$content";}
?>[/code]

It is displaying 0 which I believe is the echoed value of $id. So apparently it is not being passed. How can I fix this problem?
Link to comment
Share on other sites

Take note of the help you've had from Crayon Violent and add some error and debugging displays. And remove the @ symbols. Try this - with your db information used - and uploaded to your server, and tell us what actually happens:

[code]<?php
$name=""; // edit to suit
$lock=""; // edit to suit
$database=""; // edit to suit
$id = $_GET['id'];

echo "The value of id is ". $id. "<br/>";

mysql_connect("localhost",$name,$lock) or die(mysql_error());
mysql_select_db($database) or die( "Unable to select database");
$sql = "SELECT * FROM user_files WHERE file_id='$id'";
$query = mysql_query($sql) or die("Error: ". mysql_error(). " with query ". $sql);
$info = mysql_fetch_array($query);
$content = $info['file_desc'];
$thumburl = $info['file_pic'];


if ($content='')
   {echo "The user has not supplied a description for this file"; }
else
   {echo $content;}
?>[/code]
Link to comment
Share on other sites

No sql error? Right?

I assume you're not calling that script in isolation (which would give no value for id), but getting it to run by clicking a link like:
[code]<a href='filepage.php?id=2'>image 2</a>[/code]
Link to comment
Share on other sites

Yea it comes from a link. I have a list of files uploaded, where each file's info is stored in a db and on a page filelist.htm I display all the uploaded files. Well, I want a description page for each file so I did this:
echo"<a href='http://mysite.com/members/upload/files/filepage.htm?id=$fileid'>$filename</a>";
and when the user clicks on the link it will send them to [a href=\"http://mysite.com/members/upload/files/filepage.htm?id=3\" target=\"_blank\"]http://mysite.com/members/upload/files/filepage.htm?id=3[/a] (in this example).
Link to comment
Share on other sites

what are you seeing in the address bar?

http:://mysite.com/members/upload/files/filepage.htm?id=2

or

[a href=\"http://mysite.com/members/upload/files/filepage.htm?id=\" target=\"_blank\"]http://mysite.com/members/upload/files/filepage.htm?id=[/a]

try typing in the address by passing the var manually and see if your script works then

I see this from your first post

<a href='http://mysite.com/members/upload/files/filepage.htm?id=$fileid'>$filename</a>

which is probably your problem

try

<a href='http://mysite.com/members/upload/files/filepage.htm?id=' . $fileid . '>$filename</a>

or

<a href="http://mysite.com/members/upload/files/filepage.htm?id=$fileid">$filename</a>

note: double quotes and not single quotes above

I would like to see how your passing the id to the url, typing it in manually would narrow down the problem



Link to comment
Share on other sites

I dont see how that would be the problem, the description page URL looks like this:
[a href=\"http://mysite.com/members/upload/files/filepage.htm?id=3\" target=\"_blank\"]http://mysite.com/members/upl...lepage.htm?id=3[/a] not
[a href=\"http://mysite.com/members/upload/files/filepage.htm?id=\" target=\"_blank\"]http://mysite.com/members/upl...ilepage.htm?id=[/a]
Link to comment
Share on other sites

Hello,

I think the problem is with your include in filepage.htm

<?php include('http://mysite.com/filepage.php'); ?>

u r using URL to include....that can create problems passing the $_GET variable.Try following solutions:

1) Use relative path to include

<?php include('filepage.php'); ?>

OR

2) Pass the id to included file like this


<?php include('http://mysite.com/filepage.php?id='.$_GET['id']); ?>

hth
Link to comment
Share on other sites

alright samshell's idea has passed the value of ID onto the script so it works now, thx. The only problem is that it is still not returning the value of $content from the database.

Here is the column info for file_desc:
type: varchar
maxlength: 100
null: no

Here is the specific info for the entry with file_id equal to 3:
file_id: 3
file_name: ag5.png
file_size: 34394
file_type: Images
file_path: ...
file_creator: webdogjcn
file_desc: This is the default logo for the Alternative Gaming Website. Copyright 2006. All rights reserved.

It is returning this error:
The value of id is 3
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 ''user_files' WHERE file_id='3'' at line 1 with query SELECT * FROM user_files WHERE file_id='3'

$content=mysql_query("SELECT 'file_desc' FROM 'user_files' WHERE file_id='$id'") or die("Error: ". mysql_error(). " with query ". $sql);

The $sql is where I ran the query earlier just w/o the SELECT 'file_desc' aprt and just SELECT * (I did that for different reasons as I was trying to find a way to get this working).

n/m I took out the single quotes ('.') around the file_desc and user files. However I now just get "The value of id is 3" and it never displays the text stored in file_desc
Link to comment
Share on other sites

[code]$content=mysql_query("SELECT 'file_desc' FROM 'user_files' WHERE file_id='$id'") or die("Error: ". mysql_error(). " with query ". $sql);[/code]
Humor me. Use the code I posted a few posts back instead of your version of it.
Link to comment
Share on other sites

Alright, thanks for all the help, but I managed to figure it out by myself. I have a new problem, shouldn't be as big of a deal.....
[code]if ($getrating < 6 || $getrating > 0)
{
$numrates ++; //Adds one to the total number of times this file has been rated
$ccc = $ratesum; //Used to update the db below
$ratesum += $getrating; //adds the user's own rating to the total
$ratesum /= $numrates; //find the average

$sqlquery = "UPDATE user_files SET file_rates='$numrates',file_ratesum='$ccc',file_rating='$ratesum' WHERE file_id='$id'";
mysql_query($sqlquery) or die("Error: ". mysql_error(). " with query ". $sqlquery);
}[/code]
$getrating come from $_GET[]. This script is used to rate a file.

Okay so I want to find the average of all the ratings that have been made on the file so, everytime I rate the file it takes the # (1-5) (found in $getrating) and adds it to the total then takes that number and divides it by the times teh file has been rated.

PROBLEM: The only part that gets updated in the db is the file_rates nothing else.
Link to comment
Share on other sites

so just out of curiosity, how did you fix the previous problem?

as far as your current problem:

[code]
$ccc = $ratesum; //Used to update the db below
$ratesum += $getrating; //adds the user's own rating to the total
$ratesum /= $numrates; //find the average
[/code]
you are setting $ccc to equal the value of $ratesum, and then altering $ratesum right after? first off, where is the code that sets $ratesum in the first place (and therefore has a value to even pass to $ccc) and shouldn't $ccc = $ratesum; come after you do those 2 operations?
Link to comment
Share on other sites

Also,

isnt this condition ambigious....

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
if ($getrating < 6 || $getrating > 0)
[/quote]

if you are checking $getrating > 0...and use an OR sign....it will not check for $getrating < 6

i think the condition should be....

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
if ($getrating < 6 && $getrating > 0)
[/quote]

if you want to check if rating is between 0 and 6...

hth
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.