I have a problem, it turns out that I am using ajax, I already managed to implement it in insert, delete and read but I can't implement it for update I don't know if you can help me.
The edit.php file is fine since it works, since it brings me the data to a modal without problem Name: edit.php
<?php
include "model/conexion.php";
$edit_id = $_POST['edit_id'];
$sentencia = $bd->prepare("SELECT * FROM productos WHERE id = ?;");
$sentencia->execute([$edit_id]);
$var_productos = $sentencia->fetch(PDO::FETCH_OBJ);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Editar producto</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script>
</head>
<body>
<form id="form" action="post">
<div class="form-row">
<input id="edit_id" value="<?php echo $var_productos->id ?>">
<div class="form-group col-md-12">
<label for="">Producto</label>
<input class="form-control" type="text" name="producto" id="edit_producto" value="<?php echo $var_productos->producto; ?>">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label for="">Codigo</label>
<input class="form-control" type="text" name="codigo" id="edit_codigo" value="<?php echo $var_productos->codigo; ?>">
</div>
<div class="form-group col-md-4">
<label for="">Valor unidad</label>
<input class="form-control" type="text" name="valor_unidad" id="edit_valor_unidad" value="<?php echo $var_productos->valor_unidad; ?>">
</select>
</div>
<div class="form-group col-md-2">
<label for="">Stock</label>
<input class="form-control" type="text" name="stock" id="edit_stock" value="<?php echo $var_productos->stock; ?>">
</div>
</div>
</form>
</body>
</html>
Name: update.php
<?php
include "model/conexion.php";
$edit_id = $_POST['edit_id'];
$codigo = $_POST['edit_codigo'];
$producto = $_POST['edit_producto'];
$stock = $_POST['edit_stock'];
$valor_unidad = $_POST['edit_valor_unidad'];
$sentencia = $bd->prepare("UPDATE productos SET codigo = ?, producto = ?, stock = ?, valor_unidad = ? WHERE id = ?;");
$resultado = $sentencia->execute([$codigo, $producto, $stock, $valor_unidad, $edit_id]);
?>
File: index.php I'm just going to put the respective ajax
$(document).on("click", "#update", function(e) {
e.preventDefault();
var edit_producto = $("#edit_producto").val();
var edit_codigo = $("#edit_codigo").val();
var edit_valor_unidad = $("#edit_valor_unidad").val();
var edit_stock = $("#edit_stock").val();
var update = $("#update").val();
var edit_id = $("#edit_id").val();
/* alert(edit_producto);
alert(edit_codigo);
alert(edit_valor_unidad);
alert(edit_stock);
alert(edit_id); */
$.ajax({
url: "update.php",
type: "post",
data: {
edit_id: edit_id,
edit_producto: edit_producto,
edit_codigo: edit_codigo,
edit_valor_unidad: edit_valor_unidad,
edit_stock: edit_stock,
update: update
},
success: function(data) {
fetch();
$("#show").html(data);
}
})
});
The error that the console throws me is
I don't know why it doesn't really bring me any data and I've been going around for like 3 hours and I can't find the error. It is evident that the chain does not bring anything since when it arrives at post no data is ever printed, even the console tells me.
The data is taken to the JS function, but where it does not enter is in the data array:
It's funny but 10 minutes after writing the bug I found that it's a jquery problem.
error: the slim version of jquery has this error
solution: change to a non-slim version