Forum Topic: Problem with Sql Query

(126 views • 6 replies)

This topic is 1 page long.

<< < > >>
None

Thomas

Reply To Post Reply & Quote

Posted at: 8/14/09 02:33 AM

Thomas LIGHT LEVEL 13

Sign-Up: 02/14/05

Posts: 2,830

I've been working on a new uploader for my website, and it works using sql so that a viewing page can be dynamically generated. However, I am having unexplained sql errors.

In my script, I have a line that inserts a row for the information on the upload, using a temporary hash so that the row can be revisited later in the code. The first problem I have is that the query won't work. I checked the error using mysql_error(), and it says I haven't selected a database (which is wrong).

The error also occurs later in the script, where the sql row is revisited.

Here is a link to the page:
http://knoxius.com/sites/test/upload/upl oad.php

Here is the code until the INSERT INTO query:

//MySql Info
mysql_connect('localhost','secret','secret');

//this is the hard part: the db is here, but it doesnt believe me
mysql_select_db('knoxius8_upload');

//Error Function - Remove Temporary Row
//This is for if an error occurs in the script: the temporary row is deleted and mysql is closed
function error($hash) {
	$find_entry = mysql_query("DELETE FROM files WHERE filename='$hash'");
	mysql_close();
}

//File Info
//all of the file info
$file = $_FILES['file']['name'];
$file_size = $_FILES['file']['size'];
$file_location = $_FILES['file']['tmp_name'];
$file_ext = explode('.',$file);
$file_ext = array_reverse($file_ext);
$file_ext = $file_ext[0];

//Temporary Row
$date = time();
$user_ip = $_SERVER['REMOTE_ADDR'];
$temp_hash = md5($file);
$insert = mysql_query("INSERT INTO files VALUES(NULL,'$temp_hash','$date','$user_ip')");

//put this here to test the query
if(!$insert) {
	echo 'problem <br />';
	echo mysql_error();
	exit;
}

I really do not know what the problem is...hopefully someone can help? Thanks.


None

WoogieNoogie

Reply To Post Reply & Quote

Posted at: 8/14/09 02:40 AM

WoogieNoogie LIGHT LEVEL 14

Sign-Up: 06/26/05

Posts: 3,284

All I can guess is that your function is causing your connection to close. Try removing that and see if it works.


None

VigilanteNighthawk

Reply To Post Reply & Quote

Posted at: 8/14/09 02:43 AM

VigilanteNighthawk LIGHT LEVEL 03

Sign-Up: 02/13/03

Posts: 1,686

I doubt it's the functions, though it's worth looking at. I would check that the database is actually being selected when you run mysql_select_db.

Also, your file extension checking function doesn't handle multiple extensions, such as tar.gz.


None

WoogieNoogie

Reply To Post Reply & Quote

Posted at: 8/14/09 02:46 AM

WoogieNoogie LIGHT LEVEL 14

Sign-Up: 06/26/05

Posts: 3,284

At 8/14/09 02:43 AM, VigilanteNighthawk wrote: I doubt it's the functions, though it's worth looking at. I would check that the database is actually being selected when you run mysql_select_db.

Yeah, that seems to be the most likely thing. Make sure that the database name is what you think it is. Hosts will sometimes at stuff to the beginning.


None

Thomas

Reply To Post Reply & Quote

Posted at: 8/14/09 02:54 AM

Thomas LIGHT LEVEL 13

Sign-Up: 02/14/05

Posts: 2,830

At 8/14/09 02:40 AM, WoogieNoogie wrote: All I can guess is that your function is causing your connection to close. Try removing that and see if it works.

That was one of the first things I tried (before posting here), so it's not the problem.

At 8/14/09 02:43 AM, VigilanteNighthawk wrote: I doubt it's the functions, though it's worth looking at. I would check that the database is actually being selected when you run mysql_select_db.

I did a simple check ( $db = mysql_select_db(), if(!db) ), and apparently it isn't selecting it. Is there a reason for that (there are no spelling errors or anything).

Also, your file extension checking function doesn't handle multiple extensions, such as tar.gz.

Not too worried about that (I don't have tar.gz in my list of allowed extensions - maybe in future I will change).


None

VigilanteNighthawk

Reply To Post Reply & Quote

Posted at: 8/14/09 03:03 AM

VigilanteNighthawk LIGHT LEVEL 03

Sign-Up: 02/13/03

Posts: 1,686

At 8/14/09 02:54 AM, Thomas wrote:


I did a simple check ( $db = mysql_select_db(), if(!db) ), and apparently it isn't selecting it. Is there a reason for that (there are no spelling errors or anything).

Check that the user you are using to access the database has permission to access the database. If you are using cpanel, you have to add the user to each database after your create it, and make sure to assign the necessary permissions to it. I once spent three aggravating hours trying to diagnose a db error because I had forgotten to do that. Dump the error after the db selection to confirm it.


Also, your file extension checking function doesn't handle multiple extensions, such as tar.gz.
Not too worried about that (I don't have tar.gz in my list of allowed extensions - maybe in future I will change).

No problem then. If you are only going to be handling single part file extensions, I would still check to see how many parts are being returned to be sure that the file only has a single part extension. The returned array should only have two elements. Otherwise, someone is probably monkeying around with the extension of the file they are submitting.


None

Thomas

Reply To Post Reply & Quote

Posted at: 8/14/09 03:07 AM

Thomas LIGHT LEVEL 13

Sign-Up: 02/14/05

Posts: 2,830

At 8/14/09 03:03 AM, VigilanteNighthawk wrote: Check that the user you are using to access the database has permission to access the database.

Haha, apparently I forgot to do that when I made the database xD

Thanks for your help!


All times are Eastern Standard Time (GMT -5) | Current Time: 09:16 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!