Blank form. It does not work

0

I have created the following form and I want the data entered to be saved in the database. But when I send the form, it goes blank and does nothing.

I created the database, the user and the table. All the data is correct:

index.php

    <!DOCTYPE html>
<html>
    <head>
        <title>Formulario De Contacto</title>
        <link rel="stylesheet" type="text/css" href="estilo.css">
    </head>
    <body>
        <div class="form">
            <form action="guardar.php" method="POST">
                <p>Nombre</p>
                <label for="nombre">Su nombre</label>
                <br>
                <input type="text" name="user" required>
                <p>Correo</p>
                <label for="correo">Direccion De Correo</label>
                <br>
                <input type="password" name="pass" required>
                <br>
                <br>
                <input type="submit" value="Enviar">
            </form>
        </div>
    </body>
</html>

save.php

<?php
//conectamos Con el servidor
$conectar=@mysql_connect('localhost','Usuario','Contraseña');
//verificamos la conexion
if(!$conectar){
    echo"No Se Pudo Conectar Con El Servidor";
}else{

    $base=mysql_select_db('NombreBaseDeDatos');
    if(!$base){
        echo"No Se Encontro La Base De Datos";          
    }
}
//recuperar las variables
$user=$_POST['user'];
$pass=$_POST['pass'];
//hacemos la sentencia de sql
$sql="INSERT INTO datos VALUES('$user',
                               '$pass')";
//ejecutamos la sentencia de sql
$ejecutar=mysql_query($sql);
//verificamos la ejecucion
if(!$ejecutar){
    echo"Hubo Algun Error";
}else{
    echo"Datos Guardados Correctamente<br><a href='index.html'>Volver</a>";
}
?>
  • Where I put "User" I have placed the created user, "Password" is the password of that user and "NombreDeBaseDatos" is the database itself.
  • The user is added to the database with all the privileges.
asked by Brahim 01.09.2018 в 17:52
source

2 answers

0

As you mentioned in the comment you are using instructions that have been declared obsolete See reference

I recommend changing from mysql_connect to mysqli_connect Here the reference of this function , therefore your connection string would look like this:

$conectar = mysqli_connect("localhost", "Usuario", "Contraseña", "NombreBaseDeDatos");

if (!$conectar) {
    die("Connection failed: " . mysqli_connect_error());
}

and you execute your query in this way:

//recuperar las variables
$user=$_POST['user'];
$pass=$_POST['pass'];
//hacemos la sentencia de sql
$sql="INSERT INTO datos VALUES('$user', '$pass')";

if (mysqli_query($conectar, $sql)) {
    echo "Datos Guardados Correctamente<br><a href='index.html'>Volver</a>";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conectar);
}

I see that you are not specifying the names of the columns in your table, so make sure that the order of the values is in the same order as the columns in the table. If not better use the instruction:

INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...); 

I hope it serves you.

    
answered by 01.09.2018 в 19:07
0

You are performing the connection to the database incorrectly. First of all it should be noted that the mysql functions are being replaced by mysqli in the new versions of PHP, so you should try to migrate to these new functions. There are also some concatenation errors in the query and it is necessary to assign the name of the columns where each data will be saved. In addition, you should do some kind of validation of the variables received by $_POST , since in this way it is very insecure and can lend itself to an SQL injection.

Here I leave as an example your updated and corrected code:

<?php
    //conectamos Con el servidor
    $conectar = new mysqli("localhost", "root", "", "stackoverflow");

    //verificamos la conexion
    if($conectar->connect_errno){
        echo "Fallo al conectar a MySQL: (" . $conectar->connect_errno . ") " . $conectar->connect_error;
    }

    //recuperar las variables
    $user = $_POST['user'];
    $pass = $_POST['pass'];

    //hacemos la sentencia de sql
    $sql = "INSERT INTO pruebas (user, pass) VALUES('".$user."', '".$pass.".')";

    //ejecutamos la sentencia de sql
    $resultado = $conectar->query($sql);

    //verificamos la ejecucion
    if(!($resultado)){
        echo"Hubo Algun Error(" . $conectar->errno . ") " . $conectar->error;
    }else{
        $conectar->close();
        echo"Datos Guardados Correctamente<br><a href='index.html'>Volver</a>";
    }
?>

I hope it serves you. Greetings

    
answered by 01.09.2018 в 18:54