3 votes

Why does the UPDATE with the WHERE line in mysql not perform the updates in my database?

    <html lang="es">
       <body class="body">
            <form action="editar.php" method="post" class="form_contact">
                <input type="text" value="<?php echo $datos['nombre_apellido'];?>" class="name entry " name="nombre_apellido" placeholder="Nombre y Apellido" required/>
                <input type="email" value="<?php echo $datos['email'];?>" class="email entry" name="email" placeholder="E-mail"/>
                <textarea class="message entry" name="mensaje" placeholder="Mensaje"><?php echo $datos['mensaje'];?></textarea>
                <button class="submit" name="guardar">Guardar</button>
            if(isset($_POST['guardar'])) {
            $nombre_apellido = $_POST['nombre_apellido'];
            $email = $_POST['email'];
            $mensaje = $_POST['mensaje'];
            mysqli_query($conexion,"UPDATE contactos SET
            nombre_apellido = '$nombre_apellido',
            email = '$email',
            mensaje = '$mensaje',
            WHERE mensaje = '$mensaje'"); #El where de un update debe ir sobre una llave primaria
            header('Location: solicitudes_contacto.php');

Why UPDATE with WHERE in mysql does not make changes to my database. I clarify that message is a primary key. If I remove the line of code from WHERE it does update but it modifies all the rows in the database.


Excorpion Points 6

The OP has several errors left over, He contacted him by chat to answer everything.

  • Lack of ID usage
  • Bad application of primary keys
  • Typographic problems

It ended up editing the function to this:

 if (isset($_POST['guardar'])) {
    $nombre_apellido = $_POST['nombre_apellido'];
    $email = $_POST['email'];
    $mensaje = $_POST['mensaje'];
    $pepe = mysqli_query($conexion, "UPDATE contactos SET mensaje = '$mensaje' WHERE email = '$email'");
    echo "UPDATE contactos SET mensaje = '$mensaje', WHERE email = '$email'";
    //header('Location: editar.php');

The OP understands current errors and problems, but will make future corrections.


HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by: