Jurik Posted September 11, 2006 Share Posted September 11, 2006 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 errorNotice: Undefined index: ID in c:\documents and settings\administrator\my documents\web pages\st john fisher movie front end\index.php on line 160Query failedNow 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? Quote Link to comment https://forums.phpfreaks.com/topic/20371-_getid-script/ Share on other sites More sharing options...
shocker-z Posted September 11, 2006 Share Posted September 11, 2006 <?php//code to query the database for the tableid=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 mateRegardsLiam Quote Link to comment https://forums.phpfreaks.com/topic/20371-_getid-script/#findComment-89713 Share on other sites More sharing options...
Wintergreen Posted September 11, 2006 Share Posted September 11, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/20371-_getid-script/#findComment-89714 Share on other sites More sharing options...
HuggieBear Posted September 11, 2006 Share Posted September 11, 2006 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]RegardsRich Quote Link to comment https://forums.phpfreaks.com/topic/20371-_getid-script/#findComment-89718 Share on other sites More sharing options...
Jenk Posted September 11, 2006 Share Posted September 11, 2006 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] :) Quote Link to comment https://forums.phpfreaks.com/topic/20371-_getid-script/#findComment-89726 Share on other sites More sharing options...
Jurik Posted September 11, 2006 Author Share Posted September 11, 2006 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 informationecho "<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 159Query 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 Quote Link to comment https://forums.phpfreaks.com/topic/20371-_getid-script/#findComment-89732 Share on other sites More sharing options...
Wintergreen Posted September 11, 2006 Share Posted September 11, 2006 [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] Quote Link to comment https://forums.phpfreaks.com/topic/20371-_getid-script/#findComment-89733 Share on other sites More sharing options...
Jurik Posted September 11, 2006 Author Share Posted September 11, 2006 Got it no thank you very much people Quote Link to comment https://forums.phpfreaks.com/topic/20371-_getid-script/#findComment-89742 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.