0

i'm having weird results from php $stmt->bind_param, here is what happens... if i do this $status="something"; $var=5;

$consulta="UPDATE carrito SET status='$status' WHERE id_carrito='$var'";

and prepare and execute it works... but as soon as i do this:

$consulta="UPDATE carrito SET status=? WHERE id_carrito=?";
if($stmt=$mysqli->prepare($consulta))
{
    $stmt->bind_param("si",$status,$var);
  .
  .
  .

it stops working, vars, are ok i print them after the execute and they have the correct value actually, i don't get any error from php, query executes, it just dont save values on mysql db also, i want to mention this is not the first time i work with prepared statements i've doing this for a while now, i'm not an expert yet, but this has never happened to me before i know there is something wrong with bind_param, but i don't find any information thanks.

here is complete code

/*_____________________ DATOS DE CONEXION _________________________*/

$mysqli = new mysqli('localhost', 'root', '', 'ambarb');

if(mysqli_connect_errno()) 
    {
        echo "Connection Failed: " . mysqli_connect_errno();
        exit();
    }
/*_____________________ fin de los datos de conexion _______________*/
 $idcarrito=$_POST["id"];
 $status="cancelado";
 $resultado=array();
  $consulta="UPDATE carrito SET status='$status' WHERE id_carrito=$idcarrito";
   if($stmt=$mysqli->prepare($consulta))
{
    //$stmt->bind_param("si",$status,$idcarrito);
    $stmt->execute();
    if($stmt->errno==true)
        {
            $resultado['status']='error';
        }
    else
        {
            $resultado['status']='ok';
        }
    $stmt->close();
}
else
{
    $resultado['status']='error al preparar la consulta PHP ERROR CODE ';
}
 echo json_encode($resultado);
//echo "el ide del carrito ".$idcarrito;
$mysqli->close();
?>
that code actually works, prints $resultado['status']=ok, but i think is not the point, because as soon as i change for this $consulta="UPDATE carrito SET status=? WHERE id_carrito=?"; with respective bind_param it stops working, i mean prints $resultado['status']=ok, but doesn't make anychange at database
  • can you show full code with execute and params etc. ? – bitWorking Jul 30 '13 at 21:00
  • the query executes but the values don't get updated in the database? that is **very** hard to believe ... it's more likely that the query is not executing at all. at any rate the code you posted looks fine, so the problem is probably somewhere else. – user428517 Jul 30 '13 at 21:04
  • what does `$stmt->affected_rows` return after running `$stmt->execute()`? – Phylogenesis Jul 30 '13 at 21:07
  • code showed :) it's actually quite simple i have to mention, this is called from .ajax() call from a js, does that has anything to do? – user2624819 Jul 30 '13 at 23:24

2 Answers2

0

You used a $var variable in first sentence, and $idcarrito in the second one. Could that be the problem ?

Pierrito
  • 135
  • 1
  • 2
  • 9
0

I wrote a class that extends mysqli. It handles all the bind_params automatically and makes using mysqli easy. You can download the class here: better_mysqli.php

This page shows it's basic usage: basic_usage.php

This page shows detailed usage: detailed_usage.php

Drew
  • 4,035
  • 3
  • 23
  • 37