Upload a file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Aravinthan
Forum Commoner
Posts: 84
Joined: Mon Jan 28, 2008 6:34 pm

Re: Upload a file

Post by Aravinthan »

But I still get Errory: QUery Failed for the upload script....
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Upload a file

Post by mmj »

replace

Code: Select all

mysql_query($query) or die('Error, query failed');
with

Code: Select all

mysql_query($query) or die('Error: ' . mysql_error());
and then post the output that you get.
Aravinthan
Forum Commoner
Posts: 84
Joined: Mon Jan 28, 2008 6:34 pm

Re: Upload a file

Post by Aravinthan »

Ok this is what I get:
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 '����O�o�Ϥ�v� d� _�R�2\�N555555���p��B��⿿��'�����������?�笫Q�����?n�'�' at line 1
Those characters are unreadble.....
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Upload a file

Post by mmj »

Aravinthan wrote:Ok this is what I get:
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 '����O�o�Ϥ�v� d� _�R�2\�N555555���p��B��⿿��'�����������?�笫Q�����?n�'�' at line 1
Those characters are unreadble.....
Are you sure the column type is blob for binary data?

I personally don't have much experience inserting binary data into mysql, try Google:

http://google.com/search?q=mysql+insert+binary+data
Aravinthan
Forum Commoner
Posts: 84
Joined: Mon Jan 28, 2008 6:34 pm

Re: Upload a file

Post by Aravinthan »

It is blob, and everywhere I look they have almost the same code has me. Only difference the names...
But When I looked at the oder of my rows in phpmyadmin:
id
name
type
size
content

but when I look in my coding:

Code: Select all

 
$query = "INSERT INTO upload (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";
 
It isnt the same order. Could that be the problem?
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: Upload a file

Post by Syntac »

No, the order isn't the problem. Use mysql_real_escape_string() to keep your data from clobbering the query.
Aravinthan
Forum Commoner
Posts: 84
Joined: Mon Jan 28, 2008 6:34 pm

Re: Upload a file

Post by Aravinthan »

I dont get it Syntac.... where to pu tthe mysql espace...
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Upload a file

Post by mmj »

Aravinthan wrote:I dont get it Syntac.... where to pu tthe mysql espace...
http://php.net/mysql_real_escape_string
If binary data is to be inserted, this function must be used.
hmm, maybe that is it.
Aravinthan
Forum Commoner
Posts: 84
Joined: Mon Jan 28, 2008 6:34 pm

Re: Upload a file

Post by Aravinthan »

I read the article, but I still dont get where to put the escape string..... is it after my inserted values?
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: Upload a file

Post by Syntac »

Instead of inserting the raw string, insert the escaped string. Of course, you would know this if you had read the entire page.
Aravinthan
Forum Commoner
Posts: 84
Joined: Mon Jan 28, 2008 6:34 pm

Re: Upload a file

Post by Aravinthan »

OK I changed my coding to this(first section):

Code: Select all

 
$link = mysql_connect ("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("aoe3clan_division1") or die(mysql_error());
 
$gameid = $_POST['gameid'];
$player1 = $_POST['player1'];
$player2 = $_POST['player2'];
$winner = $_POST['winner'];
$fileName= $player1 . ' vs ' . $player2 . ' on ' . $map;
$tmpName  = mysql_real_escape_string($_FILES['uploaded_file']['tmp_name'], $Link);
$fileSize = mysql_real_escape_string($_FILES['uploaded_file']['size'], $Link);
$fileType = mysql_real_escape_string($_FILES['uploaded_file']['type'], $Link);
 
$sql="SELECT COUNT(*) AS count
        FROM games
        WHERE gameid='$gameid'";
$result=mysql_query($sql);
$row=mysql_fetch_assoc($result);
 
if ($row['count']>0){
   echo "There is already a game with this id. <a href='http://www.aoe3clan.com'>Home</a>";
   }
else {
  mysql_query("INSERT INTO games (gameid, player1, player2, winner, map) VALUES ('$gameid', '$player1', '$player2', '$winner', '$map')") or die(mysql_error());
  print "Game $gameid has been successfully inserted into the database. Details:</br>GameId: $gameid</br> Player1: $player1</br> Player2: $player2</br> Winner:$winner</br> Map:$map</br>";
 
$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
fclose($fp);
include 'library/config.php';
include 'library/opendb.php';
 
$query = "INSERT INTO upload (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";
mysql_query($query) or die('Error: ' . mysql_error());
include 'library/closedb.php';
echo "File $fileName uploaded<br>";
echo $size;
echo $type;
 
I get the message that the file has been uploaded but it has a size of 0 KB, I used phpmyadmin to see it and used mysql to display the results:
http://www.aoe3clan.com/index.php?name=36drew

Thanks for your help
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Upload a file

Post by mmj »

You aren't calling mysql_real_escape_string on the $content variable.
Aravinthan
Forum Commoner
Posts: 84
Joined: Mon Jan 28, 2008 6:34 pm

Re: Upload a file

Post by Aravinthan »

OK I changed it to:

Code: Select all

 
$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
fclose($fp);
include 'library/config.php';
include 'library/opendb.php';
$content1 = mysql_real_escape_string($content, $Link);
$query = "INSERT INTO upload (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content1')";
mysql_query($query) or die('Error: ' . mysql_error());
include 'library/closedb.php';
echo "File $fileName uploaded<br>";
 
Still no use....
Aravinthan
Forum Commoner
Posts: 84
Joined: Mon Jan 28, 2008 6:34 pm

Re: Upload a file

Post by Aravinthan »

Any help please?
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Upload a file

Post by mmj »

There is still no data in the db?
Post Reply