I can not save the data in a mysql database

2

I'm trying to save data from a form made in php the codes are as follows.

Registration form

<html>
<head>
    <title> Formulario de registro </title>
</head>

<body>
    <h1> Formulario de registro </h1>
    <h5> Los campos con (*) son requeridos </h5>
    <form method="POST" action="">
        <table>
            <tr> 
                <td>
                    Nombre y apellidos:
                </td> 
                <td>
                    <input type="name" name="realname" />
                </td>
            </tr>
            <tr>
                <td>
                    *Nick de usuario
                </td>
                <td>
                    <input type="name" name="nick" />
                </td>
            </tr>
            <tr>
                <td>
                    *Contraseña:
                </td>
                <td>
                    <input type="password" name="pass" />
                </td>
            </tr>
            <tr>
                <td>
                    *Repetir contraseña:
                </td>
                <td>
                    <input type="password" name="rpass" />
                </td>
            </tr>
        </table>
        <input type="submit" name="submit" value="Registrarme" /> <input type="reset" />
    </form>
    <?php
        if (isset($_POST['submit'])) {
            require("registro.php");
        }
    ?>
</body>
</html>

php file where the post method is found.

<?php
    $realname = $_POST['realname'];
    $nick = $_POST['nick'];
    $pass = $_POST['pass'];
    $rpass = $_POST['rpass'];
    $reqlen   = strlen($nick) * strlen($pass) * strlen($rpass);
    if ($reqlen > 0) {
        if ($pass === $rpass) {
            require("connect_db.php");
            $pass = md5($pass);
            mysql_query("INSERT INTO registro VALUES('','$realname','$nick','$pass')");
            mysql_close($link);
            echo 'Se ha registrado exitosamente.';
        } else {
            echo 'Por favor, introduzca dos contraseñas idénticas.';
        }
    } else {
        echo 'Por favor, rellene todos los campos requeridos.';
    }
?>

Why do not you save the data in the database? You can help me, thank you very much.

    
asked by Grindor 05.06.2016 в 03:52
source

3 answers

2

As Elenasys says, we still have to define the action in the form, assuming that your file is called registro.php, in your case it would be:

<form method="POST" action="registro.php">

Something additional that I see is that in your Query SQL you are not defining the columns you want to enter that data:

mysql_query("INSERT INTO registro VALUES('','$realname','$nick','$pass')");

It should be:

mysql_query("INSERT INTO registro(columna1, columna2, columna3, columna4) VALUES('','$realname','$nick','$pass')");

Assuming you have an autoincrementable field where you do not need to place the name of the column, and where there are no more columns to insert data, but you generate an error when executing the query.

As an additional data, do not use the Driver mysql to connect to the database, instead use mysqli or PDO , since the mysql driver was obsoleted in PHP 5.5 and will be deleted in PHP 7.

    
answered by 05.06.2016 в 04:31
0

You have not yet defined in which fields you are going to save the data, look at your code is like this:

mysql_query("INSERT INTO registro VALUES ('','$realname','$nick','$pass')");

When it really should be like this, depending on which fields you have on your table:

mysql_query("INSERT INTO registro ('','realname','nick','pass') VALUES ('','$realname','$nick','$pass')");

Only what I do not understand is, why do you leave an empty field?

    
answered by 05.06.2016 в 04:51
0

The name of your .php file needs to be defined in the action property:

<form method="POST" action="archivo.php">

so you can perform the operation correctly.

There is another problem inside your .php file, your query is incorrect:

mysql_query("INSERT INTO registro VALUES('','$realname','$nick','$pass')");

You must define the fields:

mysql_query("INSERT INTO registro (nombrecampo1, nombrecampo2, nombrecampo3, nombrecampo4) VALUES('','$realname','$nick','$pass')");
    
answered by 05.06.2016 в 04:18