Forum Moderators: coopster
I have a page that displays files, from an upload.php page, so the user can view them, and click a link to download them... i.e. below:
<?
if(isset($_GET['id']))
{
//MySQL Database Connect
include '../includes/dblogin.php';
$id = $_GET['id'];
$query = "SELECT name, type, size, content FROM upload WHERE id = '$id'";
$result = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $content) = mysql_fetch_array($result);
header("Content-Disposition: attachment; filename=$name");
header("Content-length: $size");
header("Content-type: $type");
echo $content;
exit;
}
?>
//body goes here
<?
//MySQL Database Connect
include '../includes/dblogin.php';
$query = "SELECT id, name FROM upload";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
echo "Database is empty <br>";
}
else
{
while(list($id, $name) = mysql_fetch_array($result))
{
?>
<ul class="agenda">
<li><a href="index.php?id=<?=$id;?>"><?=$name;?></a> <a href="../includes/delete.php?id=<?=$id;?>"><img src="../images/delete_icon.jpg" alt="Delete Icon"></a></li>
</ul>
<?
}
}
?>
i have another table that i want to display on this page in teh same manner... i tried to implement the query for both tables, which the second table is called upload_minutes, but receive the error that the query keeps failing...
how am i going to be able to query both tables, and display them in two different areas of the same page?
$id = $_GET['id'];
$query = "SELECT name, type, size, content FROM upload WHERE id = '$id'";
if ([url=http://uk.php.net/manual/en/function.is-numeric.php]is_numeric[/url]($_GET['id'])) {
$id = mysqli_real_escape_string($_GET['id']); // overkill, but much better than not enough ;)
}
else {
die ('Go back and do it again...properly this time.');
}
A second much less important point is that <?=$id;?> is the short form. This may or may not work on different servers, so you would be better to just use these echo as that is guaranteed to work. As otherwise this is not a very portable script.
Finally to get around to your actual question.
What error are you getting? You say it fails, but how.
Put a bit more information into your error handling:
$result = mysql_query($query); // or die('Error, query failed');
// this is quoted from the [url=http://uk.php.net/manual/en/function.mysql-query.php]mysql_query[/url] page of the manual
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n"; // some useful information about the query
$message .= 'Whole query: ' . $query; // so you can check that the query is actually what you expected
die($message);
}
You may want to call mysql_free_result [uk.php.net] on the $result to free it. As you are using $query and $result twice. These should overwrite each other, so this should be fine, however I would personally free the result before overwriting (just in case).
[edited by: PHP_Chimp at 9:55 am (utc) on June 10, 2008]