Jump to content

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?
Link to comment
https://forums.phpfreaks.com/topic/20371-_getid-script/
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
Link to comment
https://forums.phpfreaks.com/topic/20371-_getid-script/#findComment-89713
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]
Link to comment
https://forums.phpfreaks.com/topic/20371-_getid-script/#findComment-89714
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
Link to comment
https://forums.phpfreaks.com/topic/20371-_getid-script/#findComment-89718
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
Link to comment
https://forums.phpfreaks.com/topic/20371-_getid-script/#findComment-89732
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.