1

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 . "\n";

    $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 . "\n";
    ?>

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.

saltcracker
  • 321
  • 3
  • 17
  • First of all.. From the first error message it looks like you left your username and password blank. Secondly, anytime you receive that message it likely means that there is an issue with your grants... If the issue was not there yesterday it is possible somebody flushed the grants and a change was made a while ago that was never applied. This specific case looks as though you never established a connection and queried with the same library. – Charles D Pantoga Dec 04 '15 at 16:16

1 Answers1

5

You've created a database connection using mysqli. That isn't available to the obsolete mysql library. Use mysqli consistently.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335