how to update in mysql an array received in php by ajax

0

I have problems when doing an update in php, I can see each element of the arrangement when I receive it in $ _POST but at the moment of executing the update it does not do anything, I do not know ... if it is an error of syntax or what problem is being generated, here I share my code in php.

<?php
include_once 'bin/conn.php'; //DATOS DE CONEXION    
$ids = $_POST["id_nota"];
for ($i=0; $i<count($ids); $i++)
{

    $upd= "UPDATE notas SET estado = 'CA', actualizacion=NOW() WHERE id_nota='"$ids[$i]."";
    $mysqli->query($upd);
    //echo $ids[$i]. "<br>";Imprime cada elemento por salto de linea
    echo "Datos Enviados";
}

?>
    
asked by Janeth HURTADO PACHECO 01.10.2018 в 22:02
source

1 answer

0

I share my solution to my problem in case they were in the same situation, in this case make a statement prepared, a topic that I found here How to avoid SQL injection in PHP?

<?php

$mysqli=new mysqli("**host o ip**", "**usuario**", "**contraseña**", "**base_de_datos**");

foreach($_POST["id_nota"] as $id) //id_nota es el nombre de mi diccionario nombrado en el ajax con este ciclo iterare en cada elemento del mismo.
    {

        //echo $id. "<br>";
        $upd= $mysqli->prepare("UPDATE notas SET estado = 'CA', actualizacion=NOW() WHERE id_nota=?");//creo la sentencia a ejecutar, en mi caso un UPDATE
        $upd ->bind_param("i", $id);//la "i" es por los datos que enviare en este caso son numeros, si fuesen letras se coloca una "s"
        $upd->execute();//se ejecuta la sentencia
        echo "Datos enviados";//imprimo en pantalla que se ejecuto la accion
    }

As mentioned above,

    
answered by 02.10.2018 в 17:49