How can I implement a condition that does not allow me to add records with the same username: example, if the username test1 is registered, it should not allow another user to be saved with the same name.
this is my insert:
<script src="sweet/sweetalert.min.js"></script>
<body bgcolor="#131313" style="color:#131313;font-family: sans-serif;">
<?php
include "conexiones.php";
$usuario_nom=$_POST['usuario_nom'];
$nombre_comple=$_POST['nombre_comple'];
$apellidos=$_POST['apellidos'];
$emailusuario=$_POST['emailusuario'];
$pasusuario=$_POST['pasusuario'];
$estatus=$_POST['estatus'];
{
$sql="INSERT INTO usuarios(usuario_nom,nombre_comple,apellidos,emailusuario,pasusuario,estatus)
VALUES ('$usuario_nom','$nombre_comple','$apellidos','$emailusuario','$pasusuario','$estatus')";
$res=mysqli_query($cn,$sql);
if($res){
echo"<script type='text/javascript'>
swal({
icon: 'success',
title: 'Perfecto!',
text: 'Usuario agregado correctamente!'
}).then(function() {
window.location = 'usuarios.php';
});
</script>";
}else{
die("Error".mysqli_error($cn));
}
}
?>
</body>
To achieve what you want, at the code level you should:
COUNT
based on a criterion to filter, that is, with aWHERE
you will seek to establish a count of all the rows that coincide in a column with the dynamic value that comes to you in a variableThen:
I do not handle php but if you want a field not to be repeated you must add the UNIQUE clause in the field that you do not want to be repeated: