Problem with mysqli and php, just save me a data

1

What happens is that I have the following code in which I keep only 1 record in the database, after that I get "registration error" that I indicate that it would leave in case the data is not saved correctly. The strange thing is that if I delete this register from the database, I accept one again, but more than that, not someone who has the answer ....

connection

  <?php


$mysqli=new mysqli("localhost","root","","sistema"); //servidor, usuario de base de datos, contraseña del usuario, nombre de base de datos

if(mysqli_connect_errno()){
    echo 'Conexion Fallida : ', mysqli_connect_error();
    exit();
}?>

form ..

   <form name="nuevo_usuario" method="POST" action="guarda_usuario.php">
        <table width="50%">
            <tr>
                <td width="20"><b>Usuario</b></td>
                <td width="30"><input type="text" name="usuario" size="25" /></td>
            </tr>
            <tr>
                <td><b>Password</b></td>
                <td><input type="password" name="password" size="25" /></td>
            </tr>
            <tr>
                <td><b>Email</b></td>
                <td><input type="text" name="email" size="25" /></td>
            </tr>
            <tr>
                <td colspan="2"><center><input type="submit" name="eviar" value="Registrar" /></center></td>
            </tr>
        </table>
    </form>

and registration code ..

require('conexion.php');

$usuario=$_POST['usuario'];
$password=$_POST['password'];
$email=$_POST['email'];

$query="INSERT INTO usuarios (usuario, contrasenia, email) VALUES ('$usuario','$password','$email')";

$resultado=$mysqli->query($query);?>

   <html>
<head>
    <title>Guardar usuario</title>
</head>
<body>
    <center>    

        <?php if($resultado>0){ ?>
            <h1>Usuario Guardado</h1>
            <?php }else{ ?>
            <h1>Error al Guardar Usuario</h1>       
        <?php   } ?>        

        <p></p> 

        <a href="index.php">Regresar</a>

    </center>
</body>
</html>  
    
asked by Fernando Espinosa 27.02.2017 в 21:47
source

1 answer

0

Add a unique index (autoincrement) to the table (eg 'id') and modify your query like this:

$query="INSERT INTO usuarios (id,usuario, contrasenia, email)
     VALUES (null,'$usuario','$password','$email')";
    
answered by 27.02.2017 / 22:19
source