Wheres my error?

having issues with a file in database not downloading by clicking a download button heres my code for the button

<td>
  <form action = "download.php?uid='.$clients_row2['uid'].'" method="GET">
    <input type="submit" value="Download">
  </form>
</td>

had to remove the <> and </> on here wasnt showing the code

the download.php file has this code in it

if (isset($_GET['uid'])) {
	require_once('includes/database.php');
	if ($_SESSION[$mysqli] == false) {
	  header('Location: index.php');
	}
	$uid = filter_var($_GET['uid'], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);
        $doQuery = $mysqli->query("SELECT * FROM clients WHERE uid=".$uid." LIMIT 1");
	while($x = $doQuery->fetch_array()) {
		header("Content-length: ".strlen($x['AppData']));
		header("Content-type: application/octet-stream");
		header("Content-disposition: download; filename=Data-".$x['uid'].".bin");
		echo $x['AppData'];
	}
}

I get a error in my error log saying

PHP Fatal error:  Call to a member function query() on a non-object in /home/public_html/download.php in $doQuery = $mysqli->query("SELECT * FROM clients WHERE uid=".$uid." LIMIT 1");

but seems to be correct, im not sure if its something to do with the actual button or in the download.php anyone able to help

That error indicates the variable $mysqli is not defined. My guess is the $mysqli variable isn’t in scope.

2 Likes

when u say isn’t in scope, u have a example? As in where do i need to define it at? and how?

I’m a bit surprised the error message has “non-object” instead of “resource” but nonetheless the variable $mysqli needs to be a valid “new mysqli”

http://php.net/manual/en/mysqli.quickstart.connections.php

<?php
$mysqli = new mysqli("localhost", "user", "password", "database");
2 Likes

I’ll try that when I get home and report back if it works

Typically somewhere near the top of your script you would define $mysqli as Mittineague suggested so that it is available to everything below. The variable $mysqli won’t exist unless you define it somehwhere. Furthermore, when using functions the variable won’t be available unless it is within the scope of the function.

i added that and still didnt make a difference

code in download.php is as follows still doesnt work

<?php
$mysqli = new mysqli("localhost", "my username", "pass", "database");

{
echo '
<script language ="javascript">
window.location.href = "dashboard.php"
</script>
    ';
}

if (isset($_GET['uid'])) {
	require_once('includes/database.php');
	if (secureSession($mysqli) == false) {
	  header('Location: dashboard.php');
	}
	$AppData = filter_var($_GET['uid'], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);
	$doQuery = $mysqli->query("SELECT * FROM clients WHERE uid=".$uid." LIMIT 1");
	while($x = $doQuery->fetch_array()) {
		header("Content-length: ".strlen($x['uid']));
		header("Content-type: application/octet-stream");
		header("Content-disposition: download; filename=Data-".$x['uid'].".bin");
		echo $x['AppData'];
	}
}
?>

What do you get if you do a var_dump() on the $_GET array?

still a no go, i think honestly it has something to do with the button, i had it working by adding the download.php code into the index.php but then my add/edit user button didnt work…

what is that statement supposed to be doing with the database pointer?

if (secureSession($mysqli) == false) idk how i got that error honestly, i added and deleted so many different things im completly lost with it, im using a php source that i found and trying to implement it into my site, basically i need a button on my index.php that redirects to download.php and downloads a specified column in the database, this code here is the original code

<?php
if (isset($_GET['uid'])) {
	require_once('includes/database.php');
	if (secureSession($mysqli) == false) {
	  header('Location: index.php');
	}
	$uid = filter_var($_GET['uid'], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);
	$doQuery = $mysqli->query("SELECT * FROM clients WHERE uid=".$uid." LIMIT 1");
	while($x = $doQuery->fetch_array()) {
		header("Content-length: ".strlen($x['AppData']));
		header("Content-type: application/octet-stream");
		header("Content-disposition: download; filename=Data-".$x['uid'].".bin");
		echo $x['AppData'];
	}
}
?>

i know that code works cause when i first tried to implement it into my site i had it on index.php and it would download the specified file. But can have it there cause where i inserted the code it messed up edit/delete users button

As @Spacephoenix asked earlier, what is in $_GET when you get to that code? That is, is the value of $uid what you expect it to be?

And, in the latest version of the code you posted, you don’t seem to define $uid any more:

$AppData = filter_var($_GET['uid'], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);
$doQuery = $mysqli->query("SELECT * FROM clients WHERE uid=".$uid." LIMIT 1");

Now you call it $AppData, but still use $uid in the query.

Not working is far to generic of a response to diagnose.

Therefore, I would recommend by starting with a script that confirms you can connect to MySQL via mysqli and issue a simple select query. If you can’t do that than the problem might be that MySQL isn’t installed properly. If you can than the problem is with the underlaying application level code. Possibly with the query syntax or something.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.