I update my user as root

0

I have a question, I have a form where I update the fields of the users the problem is when I want to update the username I update it as root regardless of what you type it always updates it as root.

<form action="ejecutareditar.php" method="POST" role="form">
<legend>Editar Usuarios</legend>
<div class="form-group">
<label for="id">Fecha registro</label>
                                <input type="text" name="fecha_registro" class="form-control" id="nombre" value="<?php echo $fecha_registro ?>" readonly>
                            </div>
                            <div class="form-group">
                                <label for="id">ID</label>
                                <input type="text" name="id" class="form-control" id="nombre" value="<?php echo $id ?>">
                            </div>

                            <div class="form-group">
                                <label for="nombre">Nombre</label>
                                <input type="text" name="nombre" class="form-control" id="email"  value="<?php echo $nombre ?>">
                            </div>

                            <div class="form-group">
                                <label for="usuario">Usuario</label>
                                <input type="text" name="usuario" class="form-control" id="usuario"  value="<?php echo $usuario ?>">
                            </div>

                            <div class="form-group">
                                <label for="Email">Email</label>
                                <input type="email" name="email" class="form-control" required id="email"  value="<?php echo $email ?>">
                            </div>
                            <div class="form-group">
                                <label for="password">Password</label>
                                <input type="text" name="password" class="form-control" required id="password"  value="<?php echo $password ?>">
                            </div>

                            <div class="form-group">
                                <label for="privilegio">Perfil</label>
                                <SELECT name="privilegio" size ="1" id="privilegio" style="width:310px " value="<?php echo $privilegio?>">
                              <option >----Seleccione Perfil----</option>
                             <option value="1">Administrador</option>
                             <option value="2">Usuario</option>"

                            </div></SELECT></div>

                            <button type="submit" class="btn btn-success">Actualizar</button>
                            <a href="usuarios.php" title='Editar datos' class='btn btn-danger'>Volver</a>

Code that updates             

         extract($_POST);
          $server = "localhost";
              $usuario = "root";
             $contraseña = "";
             $bd = "bdpagina";

        $conexion = mysqli_connect($server, $usuario, $contraseña, $bd)
or die("error en la conexion");

       $sentencia ="UPDATE usuarios SET nombre = '$nombre ', usuario = '$usuario', email = '$email', password = '$password', fecha_registro = '$fecha_registro' ,privilegio='$privilegio' WHERE usuarios. id = $id";

$resent=mysqli_query($conexion,$sentencia);
if ($resent==null) {
    echo "Error de procesamieno no se han actuaizado los datos";
    echo '<script>alert("ERROR EN PROCESAMIENTO NO SE ACTUALIZARON LOS DATOS")</script> ';

header("location: usuarios.php");

    echo "<script>location.href='usuarios.php'</script>";
}else {


    echo "<script>location.href='usuarios.php'</script>";

    }
    ?>
    
asked by Daniela 08.06.2018 в 19:57
source

2 answers

5

Your SQL statement is taking the value of the variable $usuario that you have defined to make the connection to your database.

$server = "localhost";
          $usuario = "root";  <-- Esta linea.
         $contraseña = ""; <-- Esta linea también estaría dando problemas.
         $bd = "bdpagina";

I recommend changing the name of the variables you use to build your SQL statement to avoid that problem. You can use the names you want, I'll give you an example:

$sentencia ="UPDATE usuarios SET nombre = '$nombre_db ', usuario = '$usuario_db', email = '$email_db', password = '$password_db', fecha_registro = '$fecha_registro_db' ,privilegio='$privilegio_db' WHERE usuarios. id = $id_db";
  

It is very important to avoid using variable names that are already in use to avoid overwriting their values or getting unexpected results.

To get the values of your form you can use:

$nombre_db = mysqli_real_escape_string($conexion, $_POST['nombre']);
$email_db = mysqli_real_escape_string($conexion, $_POST['email']);
$password_db = mysqli_real_escape_string($conexion, $_POST['password']);
$privilegio_db = mysqli_real_escape_string($conexion, $_POST['nombre']);

I also recommend checking the attributes of the input of your form because it seems to me that some attributes have erroneous values.

    
answered by 08.06.2018 / 20:08
source
1

The problem is that you are overwriting the $ user variable. The solution is to make the connection and then extract the POST content like this:

          $server = "localhost";
              $usuario = "root";
             $contraseña = "";
             $bd = "bdpagina";

        $conexion = mysqli_connect($server, $usuario, $contraseña, $bd)
or die("error en la conexion");

       extract($_POST);

       $sentencia ="UPDATE usuarios SET nombre = '$nombre ', usuario = '$usuario', email = '$email', password = '$password', fecha_registro = '$fecha_registro' ,privilegio='$privilegio' WHERE usuarios. id = $id";

$resent=mysqli_query($conexion,$sentencia);
if ($resent==null) {
    echo "Error de procesamieno no se han actuaizado los datos";
    echo '<script>alert("ERROR EN PROCESAMIENTO NO SE ACTUALIZARON LOS DATOS")</script> ';

header("location: usuarios.php");

    echo "<script>location.href='usuarios.php'</script>";
}else {


    echo "<script>location.href='usuarios.php'</script>";

    }
    ?>
    
answered by 08.06.2018 в 20:02