UPDATE with AJAX

1

I'm doing an AJAX which, after the user selects a checkbox and sets a status (status is select ), AJAX gets the information and sends it to a page called status.php where the status change is executed different query that will change the status in the database.

The problem: Even though I define the id and status, the query simply does not execute and I do not understand why.

This is the ajax code that, after the user clicks on change, captures the data and sends it to the status.php

function altera_status()
{

//caso  seja selecionado mais de uma checkbox ( e consequente, mais de um id) agrupa eles

var checkboxValues = $.map($('.checkped:checked'), function(e){
  return $(e).val();
  }).join();
//dados a enviar, vai buscar os valores dos campos que queremos enviar para a BD
var dadosajax = {
    'changePedi' : $("#changePed").val(),
    'checkbox' : checkboxValues,

};
console.log(dadosajax), 
pageurl = 'status.php';
$.ajax({

    //url da pagina
    url: pageurl,
    //parametros a passar
    data: dadosajax,
    //tipo: POST ou GET
    type: 'POST',
    //cache
    cache: false,
    //se ocorrer um erro na chamada ajax, retorna este alerta
    //possiveis erros: pagina nao existe, erro de codigo na pagina, falha de comunicacao/internet, etc etc etc
    error: function(){
        alert('Erro: Inserir Registo!!');
    },
    //retorna o resultado da pagina para onde enviamos os dados
    success: function(response)
    { 

    }
});
}

And this is the status.php , which receives the data and should update bd

<?php
$statusPed = $_REQUEST['changePedi'];
$checkStatus = $_REQUEST['checkbox'] ; 


$connect = mysqli_connect("localhost","root","", "notas-34ca74") or die ("Forninho fall"); 



switch($statusPed){
case 'separacao': 
if(isset($checkStatus)){
    $sql = 'UPDATE pedidos SET status="Em separação" WHERE id="$checkStatus" ';
    $result = mysqli_query($connect,$sql);
}
break;
case 'cancelado':
function filter( $dados ){
    $arr = Array();
    foreach( $dados AS $dado ) $arr[] = (int)$dado;
    return $arr;
}     
if(isset($checkStatus)){
    $arr = filter( $checkStatus);
    $sql = 'UPDATE pedidos SET status="Cancelado" WHERE id IN('.implode( ',', $arr ).')';
    $result = mysqli_query($connect,$sql);
}
break;
    
asked by anonymous 17.06.2017 / 20:54

1 answer

0

The problem in your queries refers to the quotes, it will work if you change your updates as follows in the separation query:

  $sql = "UPDATE pedidos SET status = 'Em separação' WHERE id='$checkStatus' ";

Already in the condition of canceled so I realized the ids will be passed this way 345,345, ... then the foreach will not work if it is that and I did not get it wrong, in that case I would put an explode as follows below (must also be changed the quotation marks of the query):

function filter( $dados ){
    $dados = explode(',',$dados);
    $arr = Array();
    foreach( $dados as $dado ) $arr[] = (int)$dado;
    return $arr;
}     
if(isset($checkStatus)){
    $arr = filter( $checkStatus);
    $sql = "UPDATE pedidos SET status='Cancelado' WHERE id IN('".implode( ',', $arr )."')";
    $result = mysqli_query($connect,$sql);
}
    
18.06.2017 / 05:12