Jump to content

Archived

This topic is now archived and is closed to further replies.

Jurik

$_GET['ID'] script

Recommended Posts

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?

Share this post


Link to post
Share on other sites
<?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

Share this post


Link to post
Share on other sites
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:

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

Share this post


Link to post
Share on other sites
This would show the last one:

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

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

[code=php:0]
$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);
[/code]

Info on the Ternary Operator can be found [url=http://uk.php.net/manual/en/language.operators.comparison.php]here[/url]

Regards
Rich

Share this post


Link to post
Share on other sites
Just a note for HuggieBear - you don't need to sub-select when searching for max():

[code]SELECT `column` FROM `table` WHERE `column` = MAX(`column`)[/code]

:)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[code]
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
}[/code]

Share this post


Link to post
Share on other sites

×

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.