This question already has an answer here:
I see there is alot of these similar questions, but I don't find anyone with similar code.
The script uploads files to the server, but I also want to insert the data about the file that has been uploaded. Files get uploaded as they should without errors, but the table in phpmyadmin stays empty.
Error in error_log:
PHP Warning: mysql_query(): Access denied for user ''@'localhost' (using password: NO) in *path*/upload.php on line 65
PHP Warning: mysql_query(): A link to the server could not be established in *path*/upload.php on line 65
The connection seem to be working as I get the output "Localhost via UNIX socket 127.0.0.1 via TCP/IP" instead of a mysql warning/error at the website.
The connection script (uploadconnect.php):
<?php
$mysqli = new mysqli("localhost", "*username*", "*password*", "*database*");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "
";
$mysqli = new mysqli("127.0.0.1", "*username*", "*password*", "*database*", 3306);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "
";
?>
The upload PHP script:
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<button type="submit" name="btn-upload">upload</button>
</form>
<?php
include_once 'uploadconnect.php';
if(isset($_POST['btn-upload']))
{
$file = rand(1000,100000)."-".$_FILES['file']['name'];
$file_loc = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$file_type = $_FILES['file']['type'];
$folder="uploads/";
$new_size = $file_size/1024;
$new_file_name = strtolower($file);
$final_file=str_replace(' ','-',$new_file_name);
if(move_uploaded_file($file_loc,$folder.$final_file))
{
$sql="INSERT INTO uploadedfiles(file,type,size) VALUES('$final_file','$file_type','$new_size')";
mysql_query($sql); #<--- LINE 65
?>
<script>
alert('successfully uploaded');
window.location.href='upload.php?success';
</script>
<?php
}
else
{
?>
<script>
alert('error while uploading file');
window.location.href='upload.php?fail';
</script>
<?php
}
}
?>
The error seem to occur at "mysql_query($sql);" at line 65 in the upload file.
</div>