Jump to content


Photo

$_GET['ID'] script


  • Please log in to reply
7 replies to this topic

#1 Jurik

Jurik
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 11 September 2006 - 09:13 AM

Hi people I have a problem and I hope that someone can help, im getting a bit of a problem with a site I am creating which is linked to a MYSQL database with movies in them. Depending on the ID that is retreaved it will show a number of movies, the problem is that on the main page that does not have an ID I get this following error

Notice: Undefined index: ID in c:\documents and settings\administrator\my documents\web pages\st john fisher movie front end\index.php on line 160
Query failed

Now I am using the peice of code below on this line

<?php
//code to query the database for the table
$query = "SELECT * FROM Videos WHERE VideoID = {$_GET['ID']}";
$result = mysql_query($query) or die ("Query failed");
$numofrows = mysql_num_rows($result);
?>

I know I have to add an IF statement but ive tired and am starting to relise im naff with IF statements, can anyone help?

#2 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 11 September 2006 - 10:14 AM

<?php
//code to query the database for the table
id=mysql_real_escape_string($_GET['ID']);
$query = "SELECT * FROM Videos WHERE VideoID = '$id'";
$result = mysql_query($query) or die ("Query failed");
$numofrows = mysql_num_rows($result);
?>


give that a blast mate

Regards
Liam
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 Wintergreen

Wintergreen
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 11 September 2006 - 10:21 AM

The query fails because there's no ID to get, which is what your site relies on to show a video.  I tried searching through php.net for the function but don't remember what it's called.  There is one that gets the ID of the last entry into the DB.  So, since you can't know what the user will search for, at least you can show the latest video or something.  So then you'd just do a quick check:

if(isset($_GET['ID'])) {
   $query = "SELECT * FROM Videos WHERE VideoID = '$_GET['ID']'";
} else {
   $query =   and this is where you'd need the function  
}


#4 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 11 September 2006 - 10:39 AM

This would show the last one:

if(isset($_GET['ID'])) {
   $query = "SELECT * FROM Videos WHERE VideoID = $_GET['ID']";
} else {
   $query = "SELECT * FROM Videos WHERE VideoID = (SELECT max(VideoID) from Videos)";
}

Or if you'd prefer to show a specific video, then specify it's id in the code.

$id = (isset($_GET['ID']) ? $_GET['ID'] : 1; // If you don't understand this code, check out the 'Ternary Operator' link below

//code to query the database for the table
$query = "SELECT * FROM Videos WHERE VideoID = $id";
$result = mysql_query($query) or die ("Query failed");
$numofrows = mysql_num_rows($result);

Info on the Ternary Operator can be found here

Regards
Rich
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#5 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 11 September 2006 - 11:20 AM

Just a note for HuggieBear - you don't need to sub-select when searching for max():

SELECT `column` FROM `table` WHERE `column` = MAX(`column`)


:)

#6 Jurik

Jurik
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 11 September 2006 - 11:46 AM

Sorry I no I should of stated this when I first put up the post but I need the script to not run any query if ID isn't set and if the ID is set then the script is run. JHeres the entire peice of the script I am running

<?php
//code to query the database for the table
$query = "SELECT * FROM Videos WHERE VideoID = {$_GET['ID']}";
$result = mysql_query($query) or die ("Query failed: " . mysql_error ());
$numofrows = mysql_num_rows($result);
?>

<?php
//code to create the table with database information
echo "<center><TABLE BORDER=\"0\">\n";
for($i = 0; $i < $numofrows; $i++) {
    $row = mysql_fetch_array($result);
    echo "<TD>".$row['Name']."</TD><TD>".$row['Videofile']." - <a href=\"videos/{$row['Videofile']}\">Watch</a></TD>\n";
echo "</TR>\n";
}
echo "</TABLE></center>\n";
?>

Now Internet Explorer is complaining that "Notice: Undefined index: ID in c:\documents and settings\administrator\my documents\web pages\st john fisher movie front end\index.php on line 159
Query failed: 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 '' at line 1" hope this helps

#7 Wintergreen

Wintergreen
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 11 September 2006 - 11:50 AM

if(isset($_GET['ID'])) {
   $query = "SELECT * FROM Videos WHERE VideoID = $_GET['ID']";
   $result = mysql_query($query) or die("Query Failed");
   $numofrows = mysql_num_rows($result);

   Then do all your video display code here
}


#8 Jurik

Jurik
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 11 September 2006 - 12:20 PM

Got it no thank you very much people




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users