php bind_param is not working

余生颓废 提交于 2020-01-16 14:53:10

问题


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

回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/17957178/php-bind-param-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!