Jump to content

Uploading to sql


unknown101

Recommended Posts

Hi Guys,

 

Im currently trying to create a basic web interface from which you can select a file from your hard disk and upload to a mysql database.

 

This is the form:

 

uploadafile1.php

 

<form method="post" enctype="multipart/form-data" action="uploadfile.php">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile">
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>

 

The php is:

 

uploadfile.php

 


<?php

error_reporting(E_ALL);

include 'connection.php';

if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

echo 'Content has been Gathered-1';

if(!get_magic_quotes_gpc())

{
    $fileName = addslashes($fileName);
}

echo 'All file information has been sucessfully collected-2';

$mysql_conn = @mysql_connect("localhost", "$db_user", "$db_pass");

       if(isset($mysql_conn))
        {
                echo 'Sorry there has been an error -3';
                exit;
        }

        mysql_select_db('xxxx');


$query = "INSERT INTO upload (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";

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


echo "<br>File $fileName uploaded<br>";
}
?>

 

I hit the upload button and i get to the error:  "echo 'Sorry there has been an error -3'"

 

I have double checked the connection.php file and the sql connection details all of which are fine.

 

Can anyone see why im getting this problem?

 

Many thanks

 

Link to comment
Share on other sites

oh dear i should have spotted that sorry, my bad.  Thanks for that.

 

Sadly its still not working though.  Now when i hit the upload button i get the error "'Error, query failed'

 

I have double checked the query and ensured all field names match, but still no change.

 

Could anyone suggestion why I may be getting this error?

 

Thanks

Link to comment
Share on other sites

oh dear i should have spotted that sorry, my bad.  Thanks for that.

 

Sadly its still not working though.  Now when i hit the upload button i get the error "'Error, query failed'

 

I have double checked the query and ensured all field names match, but still no change.

 

Could anyone suggestion why I may be getting this error?

 

Thanks

 

 

Looks like there's an error with teh query from this line:

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

 

change it to

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

and run the script again

 

hopefully it will give you more clues as to where the problem is.

=============

 

additionally, just so theres no confusion, i would change your query from

$query = "INSERT INTO upload (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";

 

to

 

$query = "INSERT INTO upload (name, size, type, content ) ".
"VALUES ('{$fileName}', '{$fileSize}', '{$fileType}', '{$content}')";

 

or

 

$query = "INSERT INTO upload (name, size, type, content ) ".
"VALUES ('" . $fileName . "', '" . $fileSize . "', '" . $fileType . "', '" . $content . "')";

 

This is just to be sure that the query is not reading the string "$filename" etc rather than the data in the php variable.

Link to comment
Share on other sites

Probably should change that to mysql_error() instead of mysqli if that doesn't work.

 

yes, def. typo is the result of habit from typing "mysqli" lately so much :P

 

if you use mysqli, you need to supply the $link also as a parameter of "mysqli_error()"

 

(I fixed the typo in my original reply)

Link to comment
Share on other sites

Thanks for the quick replies guys,  still got some problems tho=(

 

I have added the mysql_error() in and am now getting the following problem:

 

"Error, query failed Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'(2)

 

 

Just so you know the SQL is below for how i created the table:

 

CREATE TABLE upload (

id INT NOT NULL AUTO_INCREMENT,

name VARCHAR(30) NOT NULL,

type VARCHAR(30) NOT NULL,

size INT NOT NULL,

content MEDIUMBLOB NOT NULL,

PRIMARY KEY(id)

);

 

 

Regards

 

 

 

Link to comment
Share on other sites

Try changing your query to:

 

$query = "INSERT INTO upload (`name`, `size`, `type`, `content` ) VALUES ('".
mysql_real_escape_string($fileName)."','".
mysql_real_escape_string($fileSize)."','".
mysql_real_escape_string($fileType)."','".
mysql_real_escape_string($content)."')";

Link to comment
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.