Skip to content
Advertisement

how can I upload csv file and password_hash [closed]

I have this code on my website, work great, I can upload CSV file to my database, but I want the $item2 to change from numerical password to a password_hash. ($data[1] is numerical password in my CSV)

$connect = mysqli_connect('hostname', 'username', 'password', 'database');
if(isset($_POST["submit"]))
{
 if($_FILES['file']['name'])
 {
  $filename = explode(".", $_FILES['file']['name']);
  if($filename[1] == 'csv')
  {
   $handle = fopen($_FILES['file']['tmp_name'], "r");
   while($data = fgetcsv($handle))
   {
             $item1 = mysqli_real_escape_string($connect, $data[0]);  
                $item2 = mysqli_real_escape_string($connect, $data[1]);
                $query = "INSERT into voters(voters_id, password) values('$item1','$item2')";
                mysqli_query($connect, $query);
   }
   fclose($handle);
   echo "<script>alert('Import done');</script>";
  }
 }
}
?>  
<!DOCTYPE html>  
<html>  
 <head>  
  <title>Webslesson Tutorial</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>  
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
 </head>  
 <body>  
  <form method="post" enctype="multipart/form-data">
   <div align="center">  
    <label>Select CSV File:</label>
    <input type="file" name="file" />
    <br />
    <input type="submit" name="submit" value="Import" class="btn btn-info" />
   </div>
  </form>
 </body>  
</html>
            ```

Advertisement

Answer

just add password_hash()

$item2 = mysqli_real_escape_string($connect, password_hash($data[1], PASSWORD_DEFAULT));

you can read this for additional information. https://www.php.net/manual/en/function.password-hash.php

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement