I have several records (about 300) where users have their password in plain text and I want to pass it to HASH
use a file to convert the passwords and then insert them, but it happens that some are blank, because?.
To verify that it was not the password_hash I made a list with tables and place the conversion there and the whole list comes out with hash, the problem I think is in the query.
Here I leave both files:
Hashear.php
<?php
$stmt =$conexion->prepare("SELECT * FROM usuarios");
$stmt->execute();
$resultados=$stmt->get_result();
$contador=0;
while ($datos= $resultados->fetch_assoc())
$contraseña=$datos["contrasena"];
$hash=password_hash($contraseña);
$stmt = $conexion->prepare("UPDATE usuarios SET hash=? WHERE contrasena=?");
$stmt->bind_param("ss",$hash,$contraseña);
$stmt->execute();
$stmt->close();
// --------------------------------------- //
?>
Listing in HTML
<?php
$stmt =$conexion->prepare("SELECT * FROM usuarios");
$stmt->execute();
$resultados=$stmt->get_result();
$contador=0;
while ($datos= $resultados->fetch_assoc()) {
$contador=$contador+1;
echo '<td bordercolor="#FFFFFF" align="center" id="" width="1">'.$datos["id_usuario"].'</td>';
echo '<td bordercolor="#FFFFFF" align="center" id="" width="1">'.$datos["usuario"].'</td>';
echo '<td bordercolor="#FFFFFF" align="center" id="" width="45">'.$datos["contrasena"].'</td>';
$contraseña=$datos["contrasena"];
$hash=password_hash($contraseña);
echo '<td bordercolor="#FFFFFF" align="center" id="" width="45">'.$hash.'</td>';
$nivel=$datos["nivel_id"];
$stmt =$conexion->prepare("SELECT desc_nivel FROM niveles WHERE id_nivel=?");
$stmt->bind_param("i",$nivel);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($desc_nivel);
$stmt->fetch();
$stmt->close();
echo '<td bordercolor="#FFFFFF" align="center" id="" width="45">'.$desc_nivel.'</td>';
echo '<td bordercolor="#FFFFFF" align="center" id="" width="45">'.$datos["unidad_id"].'</td>';
echo "</tr>";
}
// --------------------------------------- //
?>
Updated combining the code suggested in the answers and my code.
Displays "The data was updated successfully." but not all records > get, this time only the last ones were generated with HASh the first ones not:
<?php
include '__conexion.php';
require_once 'funciones/passwordLib.php'; // Libreria de PHP5.5 Hash de contraseñas
?>
<?php
$stmt =$conexion->prepare("SELECT * FROM usuarios");
$stmt->execute();
$resultados=$stmt->get_result();
while ($datos= $resultados->fetch_assoc())
$contraseña=$datos["contrasena"];
$hash=password_hash($contraseña,PASSWORD_BCRYPT);
$stmt = $conexion->prepare("UPDATE usuarios SET hash=? WHERE contrasena=?");
$stmt->bind_param("ss",$hash,$contraseña);
$comprobar=$stmt->execute();
//Comprobamos si se ejecuto nuestra sentencia.
if (false===$comprobar) {
exit('Nuesta sentencia UPDATE fallo: ' . htmlspecialchars($actualiza->error));
} else {
echo "Los datos se actualizaron correctamente.";
}
$stmt->close();//Cerramos sentencia 'select'.
?>