enter data to mysql with form

1

I recently posted the same question but it has stopped working! I'm trying to enter data into a table in MySql. but I get this error:

 Notice: Undefined variable: username in    H:\SERVER\htdocs\SITIO2\content\control_register_user.php on line 22

 Notice: Undefined variable: apellido in H:\SERVER\htdocs\SITIO2\content\control_register_user.php on line 22

 Notice: Undefined variable: email in H:\SERVER\htdocs\SITIO2\content\control_register_user.php on line 22

 Notice: Undefined variable: telefono in H:\SERVER\htdocs\SITIO2\content\control_register_user.php on line 22

 Notice: Undefined variable: usuario in H:\SERVER\htdocs\SITIO2\content\control_register_user.php on line 22

  Notice: Undefined variable: password in H:\SERVER\htdocs\SITIO2\content\control_register_user.php on line 22


Success

the file is as follows:

if you know how to do it in PHP7?

is that I downloaded the latest version of XAMPP and brings this version of PHP

This is the file responsible for inserting everything in MySQL.

I would also like to implement some control that says that the user already exists (if it exists!)

I do not want to abuse you but if you help me with some control for the length of the password, that asks to enter some uppercase, it would be explendido.

Thank you very much in advance !!

control_register_user.php:

      session_start();

      <?php include_once('header.php'); ?>

      <h1>Control Register Usuario</h1>

    <?php
   define ("MYSQLUSER", "jolunavi");
   define ("MYSQLPASS", "688200");
   define ("HOSTNAME", "localhost");
   define ("MYSQLDB", "msf");
   if ($coneccion = @new mysqli (HOSTNAME, MYSQLUSER, MYSQLPASS, MYSQLDB));

if (isset($_REQUEST['user_nombre']) && isset($_REQUEST['user_apellido']) && isset($_REQUEST['user_email']) && isset($_REQUEST['user_telefono']) && isset($_REQUEST['user_usuario']) && isset($_REQUEST['user_password']) && isset($_REQUEST['user_token']))
{
#Asignar a las variable
$nombre = isset($_REQUEST['user_nombre']) ? $_REQUEST['user_nombre'] : '';
$nombre = isset($_REQUEST['user_apellido']) ? $_REQUEST['user_apellido'] : '';
$nombre = isset($_REQUEST['user_email']) ? $_REQUEST['user_email'] : '';
$nombre = isset($_REQUEST['user_telefono']) ? $_REQUEST['user_telefono'] : '';
$nombre = isset($_REQUEST['user_usuario']) ? $_REQUEST['user_usuario'] : '';
$nombre = isset($_REQUEST['user_password']) ? $_REQUEST['user_password'] : '';
$nombre = isset($_REQUEST['user_token']) ? $_REQUEST['user_token'] : '';
$sql = "INSERT INTO usuarios (user_nombre,user_apellido,user_email,user_telefono,user_usuario,user_password) VALUES ('$username','$apellido','$email','$telefono','$usuario','$password')";
$query = mysqli_query($coneccion, $sql);
if (!$query) {
 echo "Error</br>";
 echo $sql;
 }
else {
 echo "Success";
 }
}
mysqli_close($coneccion);
?>

<?php include_once('footer.php'); ?>

Could you please help me solve?

here is the form:

   <?php include_once('header.php'); ?>   
   <h1>Registrate</h1>
   <form action="control_register_user.php" method="post">
   <div class="maintform">
   <p class="formulario"><label for="name">Nombre:  </label> <input placeholder="Ejm: Juan" type="text" name="user_nombre" id="user_nombre"  /></p>
   <p class="formulario"><label for="name">Apellido:</label> <input placeholder="Ejm: Gutierrez" type="text" name="user_apellido" id="user_apellido"  /></p>
   <p class="formulario"><label for="name">email:   </label> <input placeholder="Ejm: [email protected]"type="text" name="user_email" id="user_email"  /></p>
   <p class="formulario"><label for="name">telefono:</label> <input placeholder="Ejm: 0931234567"type="text" name="user_telefono" id="user_telefono"  /></p>
   <p class="formulario"><label for="name">Usuario: </label> <input placeholder="Ejm: Pedrito1980"type="text" name="user_usuario" id="user_usuario"  /></p>
   <p class="formulario"><label for="name">Contrase&ntilde;a:</label> <input placeholder="Ejm: **************"type="password" name="user_password" id="user_password"/></p>
   <p class="formulario">
   <input class="ok" type="submit" name="testform" value="Terminado" />
   <input class="no_ok" type="reset" name="testform" value="Restablecer" />
   <input class="cancel" type="button" value="Cancelar" onclick="window.location.href='../index.php';"/></p>
   <br />
   <br />
   <br />
   <?php 
     $salt = 'SomeSalt';
     $token = sha1(mt_rand(1,1000000) . $salt); 
     $_SESSION['token'] = $token;
     ?>
   <input type="hidden" name="user_token" id="user_token" value="<?php echo $token; ?>"/>
   </div>
   </form>
   <?php include_once('footer.php'); ?>

Thank you very much !!

    
asked by jolunavi 30.05.2016 в 17:28
source

4 answers

0
$link = mysqli_connect("localhost", "Root", "MyRootPassword", "MyDbName");

        #Usa esta Verificacion
        if (isset($_POST['username']) && isset($_POST['password']) && isset($_POST['name']) && isset($_POST['email'])) {

            #Asignalas a una variable asi
            $usuario_login =$_POST['username'];
            $usuario_passwd =$_POST['password'];
            $usuario_nombre =$_POST['name'];
            $usuario_email =$_POST['email'];

            $sql = "INSERT INTO members (username, password, name, email) VALUES    ('$usuario_login', '$usuario_passwd', '$usuario_nombre', ' $usuario_email')";

            $query = mysqli_query($link, $sql);
            if (!$query) {
                echo "Error";
            } else {
                echo "Success";
            }
        } else {
            echo "Parametros vacios. Verifique POST del Form";
        }
    
answered by 30.05.2016 / 18:10
source
0

It's not mysql error, it's php error where you use $ _ POST ['username'] nothing is coming to you.

suggestions,

1) to debug use the GET method and you will see in the address bar what is coming to you. 2) use $ _REQUEST instead of $ _POST works with GET and POST 3) Do not use the direct values because they can make you a sql injection

You can use a function to retrieve the $ _RQUEST data something like

function traerParametro( $nombre ){
  if ( array_key_exists( $nombre, $_REQUEST ) )
  {
    return funcionQueEviteSQLInjection( $_REQUEST[ $nombre ] );
  }
  return '';
}
    
answered by 30.05.2016 в 20:05
0

I can not find your error, but I pass a code I did a while ago, it's php Oriented to objects I think it's easier and more efficient. I hope you serve

Class responsible for managing connections to the database

class DB {

private $servidor; 
private $usuario; 
private $password; 
private $bd; 
private $link; 



//Constructor
function __construct(){
    $this->servidor = 'localhost';
    $this->usuario = 'root';
    $this->password = '';
    $this->bd = 'libreria';
    $this->conectar(); /*Al inicializar la clase ya estará conectada*/ 
} 

//Destructor   
function __destruct() {
    if (isset($this->link) && is_resource($this->link)) {
        mysql_close($this->link);
    }
}

/*Realiza la conexión a la base de datos.*/ 
private function conectar(){ 
  $this->link=mysql_connect($this->servidor, $this->usuario, $this->password);
  if (!$this->link){
       die('Error de coneccion' . mysql_error());  
  }
  else{
        mysql_select_db($this->bd,$this->link); 
        mysql_query("set names 'utf8'");
  }
}

Execute a query returns result

private function ExecuteQuery($query){
    $resultado = mysql_query($query,$this->link);
    if ($resultado)
        return $resultado;
    else
        return false;
} 

Execute a query and return no result

private function ExecuteNonQuery($query){
    if ( (mysql_query($query,$this->link)) == true ){
        return true;
    }
    else{
        return false;
    }
}

Simple insert with parameters

public function InsertarLibro($titulo, $paginas,$precio,$foto,$genero,$autor)
{
    $query="INSERT INTO libros(titulo,paginas,precio,foto,idGenero,idAutor) VALUES('$titulo', $paginas,$precio,'$foto',$genero,$autor)";
    $this->ExecuteQuery($query);
    $id = mysql_insert_id();
    return $id;
}

I hope you serve

    
answered by 30.05.2016 в 21:32
0

The error is telling you that the variables are not defined and in the php code the problem is visible, all the values are being recorded in the same variable $nombre

$nombre = isset($_REQUEST['user_nombre']) ? $_REQUEST['user_nombre'] : '';
$nombre = isset($_REQUEST['user_apellido']) ? $_REQUEST['user_apellido'] : '';
$nombre = isset($_REQUEST['user_email']) ? $_REQUEST['user_email'] : '';
$nombre = isset($_REQUEST['user_telefono']) ? $_REQUEST['user_telefono'] : '';
$nombre = isset($_REQUEST['user_usuario']) ? $_REQUEST['user_usuario'] : '';
$nombre = isset($_REQUEST['user_password']) ? $_REQUEST['user_password'] : '';
$nombre = isset($_REQUEST['user_token']) ? $_REQUEST['user_token'] : '';

You are not defining the other variables. On the other hand I recommend that if you receive the data by POST then you get them through $_POST

----------------------------------------------- -------- EDIT: ---------------------------------------- ---------------

@jolunavi, the problem is that you are using some variable names to get the POST data and other variable names in the VALUES at the time of insertion.

If you use these variable names:

        $nombre =$_POST['user_nombre'];
        $apellido =$_POST['user_apellido'];
        $email =$_POST['user_email'];
        $telefono =$_POST['user_telefono'];
        $usuario =$_POST['user_usuario'];
        $password =$_POST['user_password'];

Then you must correct by changing this line:

$sql = "INSERT INTO usuarios (user_nombre,user_apellido,user_email,user_telefono,user_usuario,user_password) VALUES ('$username','$apellido','$email','$telefono','$usuario','$password')";

For this:

$sql = "INSERT INTO usuarios (user_nombre,user_apellido,user_email,user_telefono,user_usuario,user_password) VALUES ('$nombre','$apellido','$email','$telefono','$usuario','$password')";

You should check the names of the fields very well, the order, and avoid changing the variables.

----------------------------------------------- -------- EDIT 2 ---------------------------------------- --------------

Thanks Adres,

you were right, it was that variable that gave problems

solved like this:

      <?php include_once('header.php'); ?>
      <h1>Control Register Usuario</h1> 
      <?php 
       if (isset($_POST['user_nombre']) && isset($_POST['user_apellido']) && isset($_POST['user_email']) && isset($_POST['user_telefono']) && isset($_POST['user_usuario']) && isset($_POST['user_password']) && isset($_POST['user_token']))
       {      
       # Asigno a una variable asi:
    if (!empty($nombre =$_POST['user_nombre']));
      if (!empty($apellido =$_POST['user_apellido']));
      if (!empty($email =$_POST['user_email']));
      if (!empty($telefono =$_POST['user_telefono']));
      if (!empty($usuario =$_POST['user_usuario']));
      if (!empty($password =$_POST['user_password']));
      if (!empty($password =$_POST['user_token']));

In this I have problems

How do I set up a control?

So if the user already exists ... that allows me to change my username or something like that

        # creí poder solucionar asi, pero no funciona
        if ($_GET['user_usuario']) == $_POST['user_usuario'])
        {
          echo "usuario ya existente"
         }

continuation ......

    //imprimo las variables para saber si llegan o no
    $nombre =   $_POST['user_nombre'];     echo "nombre: ";   echo $_POST ['user_nombre'];   echo "<br />";
    $apellido = $_POST['user_apellido'];   echo "apellido: "; echo $_POST ['user_apellido']; echo "<br />"; 
    $email =    $_POST['user_email'];      echo "email: ";    echo $_POST ['user_email'];    echo "<br />";
    $telefono = $_POST['user_telefono'];   echo "telefono: "; echo $_POST ['user_telefono']; echo "<br />"; 
    $usuario =  $_POST['user_usuario'];    echo "usuario: ";  echo $_POST ['user_usuario'];  echo "<br />"; 
    $password = $_POST['user_password'];   echo "password: "; echo $_POST ['user_password']; echo "<br />"; 
    $token = $_POST['user_token'];         echo "token: ";    echo $_POST ['user_token'];    echo "<br />";
    echo "Los Archivos se reciben perfectamente. <br />";

    $sql = "INSERT INTO usuarios (user_nombre,user_apellido,user_email,user_telefono,user_usuario,user_password) VALUES ('$nombre','$apellido','$email','$telefono','$usuario','$password')";
      $query = mysqli_query($coneccion, $sql);
       if (!$query) {
       echo "Error</br>";
       echo $sql;
       }
       else {
       echo "Success";
       }
       }
       mysqli_close($coneccion);
       ?>
       <?php include_once('footer.php'); ?>
    
answered by 11.11.2016 в 05:46