UPDATE does not execute anything in PHP

2

On the page when the user gives you to save changes in the form:

echo"<form id='contact' action='actualizar.php' method='post' name='actualizar'>";
echo "<fieldset>";
                echo "    Nombre:<span class='obligatorio'>*</span>";
                echo "    <input placeholder='Nombre' type='text' tabindex='1' name='nombre' required autofocus value='$nombre'>";
echo "</fieldset>";
echo "<fieldset>";
                echo "    Dirección:<span class='obligatorio'>*</span>";
                echo "    <input placeholder='Dirección' type='text' tabindex='2' name='direccion' required value='$direccion'>";
echo "</fieldset>";
echo "    <button name='submit' type='submit' id='contact-submit' data-submit='Enviando...'>Enviar</button>";
                echo "</fieldset>";

And in my page to update I make the following SQL statement to update the data that the user has sent

<?php
$mysqli = new mysqli("localhost", "root", "", "Clientes");
    $nombre = $_POST['nombre'];
    $direccion = $_POST['direccion'];

    $id = $_GET['idCliente'];
    if ($stmt = $mysqli->prepare("UPDATE clientes SET Nombre = '$nombre', Direccion = '$direccion'  WHERE idCliente = ?")) {
    $stmt->bind_param("i", $id);
    $stmt->execute();
    if(!$stmt->execute()){
        echo "Error actualizando al usuario";
    }
    else 
        echo "<a href='mostrar.php'>Datos guardados correctamente, Volver</a>";
    $mysqli->close();
}
?>

I just run it but it shows the link to go back but it does not update the data

    
asked by Alberto Martínez 23.04.2018 в 21:36
source

1 answer

2

Your code suffers from some errors:

  • The prepared query criteria does not apply properly here: UPDATE clientes SET Nombre = '$nombre', Direccion = '$direccion' WHERE idCliente = ? . You must change all the values by signs of ? and then pass those values through the bind_param method. We are touching the core of the prepared consultations.
  • These values must be in the same order in which they appear in the query, since what bind_param does is replace the signs of ? with the real values.
  • In the case of data of type VARCHAR , the% sign ? is never placed in single quotes. The sign does not represent the data, it is only a placeholder that bind_param will use to know where to assign the values.
  • You are running twice, since you have a execute before the if and another one that in turn serves as an evaluation mode, with the last one: $stmt->execute(); if(!$stmt->execute()){ ...

The corrected code would then be like this:

<?php
$mysqli = new mysqli("localhost", "root", "", "Clientes");
    $nombre = $_POST['nombre'];
    $direccion = $_POST['direccion'];

    $id = $_GET['idCliente'];
    if ($stmt = $mysqli->prepare("UPDATE clientes SET Nombre = ?, Direccion = ?  WHERE idCliente = ?")) {
    $stmt->bind_param("ssi", $nombre, $direccion, $id);
    //$stmt->execute(); /*Esto no hace falta*/
    if(!$stmt->execute()){
        echo "Error actualizando al usuario";
    }
    else 
        echo "<a href='mostrar.php'>Datos guardados correctamente, Volver</a>";
    $mysqli->close();
}
?>

If it still does not work then:

  • Debug your variables, to verify that you are receiving them correctly.
  • Make sure the UPDATE is not creating a duplicate record, in which case the query would fail.
answered by 23.04.2018 / 21:58
source