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>
            </form>
            
            <?php
            break;
            }
            }
      
            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');
            exit();
            }
    
            include("cerrar_conexion.php");
            ?>
    
        </body>
    </html>

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.

2voto

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'";
    var_dump($pepe);
    //header('Location: editar.php');
    exit;
}

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

HolaDevs.com

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:

X