How to update a MySQL database using php and Javascript?

2

I have a page like the one above. It is assumed that if I give new, in the upper part to the left, let me create a new user with your name, phone, email, etc. If I give the zafacon I should delete the entire line of my page and my database. If I give the pencil something like this:

On the screen let me edit what I already have written. By changing something and giving the Update button, it is not updated in my data base or on the page. How could I solve this problem? These are the codes I have to update:

<!doctype html>
<html>
    
<head>

</head>
    
<body style="margin: 20px">
    
  
<?php
$servername = "localhost";
$username = "root";
$password = "usbw";
$dbname = "contactos";

$email = $_POST['ult'];

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "select * from USER " .
    " where email = '" . $email . "'";

    
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {  
//name, telefono, email, Dia, Mes, UltimoCelebrado
        $nombre = $row['name'];
        $tel = $row['telefono'];
        $email = $row['email'];
        $dia = $row['Dia'];
        $mes = $row['Mes'];
        $ult = $row['UltimoCelebrado'];
      
    }
}
$conn->close();
?> 
    <form action= "demofinalUP2.php" method="post">
        <label>Nombre:</label>
        <input type='text' id='nombre' value='<?php echo $nombre ?>'><br>
        <label>Telefono:</label>
        <input type='text' id='tel' value='<?php echo $tel ?>'><br>        
        <label>Email:</label>
        <input type='text' id='email' value='<?php echo $email ?>'><br>   
        <label>Dia:</label>
        <input type='text' id='dia' value='<?php echo $dia ?>'><br> 
        <label>Mes:</label>
        <input type='text' id='mes' value='<?php echo $mes ?>'><br> 
         <label>Ultimo Celebrado:</label>
        <input type='text' id='ult' value='<?php echo $ult ?>' readonly><br> <br>          
        
        <button type='button' id= 'updateRec'>Actualizar</button>
    </form>
 
    
</body>
</html>

<?php
$servername = "localhost";
$username = "root";
$password = "usbw";
$dbname = "contactos";
 
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
 

$name = $_POST['name'];
$tel = $_POST['telefono'];
$email = $_POST['email'];
$dia = $_POST['Dia'];
$mes = $_POST['Mes'];
$ultcel = $_POST['UltimoCelebrado'];

$sql = "UPDATE user " .
     "SET name = '" . $name . "'," .
     "telefono = '" . $tel . "'," .
     "email = '" . $email . "'," .
     "Dia = '" . $dia . "'," .
     "Mes = '" . $mes . "'," .
    "WHERE UltimoCelebrado = '" . $ult . "'";
 
if ($conn->query($sql) === TRUE) {
    echo "Record was updated";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
 
$conn->close();
?>

These are the Javascript ones:

$(document).on("click","#editRec", function () {
    var clickedItem = $(this).parent('td').parent('tr');
     $('#edit-window').css('visibility','visible');
 
    $.post("demofinalUP.php",
           { ult: clickedItem.find("#email").html() })
    .done(function( data ) {
        $('#forma-edit').html(data);
    });
   
});


$(document).on("click","#updateRec", function () {
   
     $('#edit-window').css('visibility','hidden');
    
    $.post( "demofinalUP2.php",
           { 
        "name" : $('#nombre').val(),
        "telefono" : $('#tel').val(),        
        "email" :  $('#email').val(),
        "Dia" :  $('#dia').val(),        
        "Mes" :  $('#mes').val(),     
        "UltimoCelebrado" :  $('#ult').val(),  
        
//           UltimoCelebrado: $('#ult').val(), 
//           name: $('#nombre').val(),
//           telefono: $('#tel').val(),
//           email: $('#email').val(),
//           Dia: $('#dia').val(),
//           Mes: $('#mes').val(),
           })
    .done(function( data ) {
        $('#forma-edit').html(data);
    });
   
});

I'm missing something? I have something wrong? They would help me a lot.

    
asked by Luis Emanuel 29.04.2016 в 04:37
source

2 answers

1

Brother to where you can not pass the data supplied directly from php to html which is what you currently have in the code, that is a process that would include ajax but I give you a very practical solution and see if it suits you.

I suggest you use session type variables so that when you give the button to update this one it would take the value contained in $ ult. so that the example would be the following ...

On page 1

    <?php
       session_start();
           .
           .
           //tu codigo aqui
           $_session['s_ult'] = $ult; // aqui es donde le asignas el valor
           .
           . 
    ?>

Now on the page where you execute the UPDATE code

    <?php
       session_start();
           .
           .
           //tu codigo aqui
           $ult = $_session['s_ult']; // aqui es donde obtienes el valor de ult
                                   // de la pagina anterior
           .
           . 
    ?>

This is how I used it and I can assure you that it works, try it and let us know how it went.

    
answered by 29.04.2016 в 05:51
0
$name = $_POST['name'];
$tel = $_POST['telefono'];
$email = $_POST['email'];
$dia = $_POST['Dia'];
$mes = $_POST['Mes'];

> $ultcel = $_POST['UltimoCelebrado'];

$sql = "UPDATE user " .
     "SET name = '" . $name . "'," .
     "telefono = '" . $tel . "'," .
     "email = '" . $email . "'," .
     "Dia = '" . $dia . "'," .
     "Mes = '" . $mes . "'," .
    "WHERE UltimoCelebrado = '" . $ult . "'";

Instead of WHERE UltimoCelebrado='".$ult."' it should be:

WHERE UltimoCelebrado='".$ultcel."' which is the variable you declared to receive $_POST['UltimoCelebrado']

    
answered by 16.05.2016 в 07:17