简体   繁体   English

PHP的bind_param无法正常工作

[英]php bind_param is not working

i'm having weird results from php $stmt->bind_param, here is what happens... if i do this $status="something"; 我从php $ stmt-> bind_param得到了奇怪的结果,这是发生了什么...如果我这样做$ status =“ something”; $var=5; $ 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. 它停止工作,vars,好吧,我在执行后将它们打印出来,它们实际上具有正确的值,我没有从php中得到任何错误,查询执行,它只是不将值保存在mysql db上,我想提一下这不是我第一次使用准备好的语句,但是我现在还不是专家,但这不是我第一次,但是在我知道bind_param出问题之前,这从未发生过,但是我没有找到任何信息,谢谢。

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

You used a $var variable in first sentence, and $idcarrito in the second one. 您在第一句中使用了$ var变量,在第二句中使用了$ idcarrito。 Could that be the problem ? 可能是问题所在吗?

I wrote a class that extends mysqli. 我写了一个扩展mysqli的类。 It handles all the bind_params automatically and makes using mysqli easy. 它会自动处理所有bind_params并使得使用mysqli变得容易。 You can download the class here: better_mysqli.php 您可以在此处下载该类: Better_mysqli.php

This page shows it's basic usage: basic_usage.php 此页面显示其基本用法: basic_usage.php

This page shows detailed usage: detailed_usage.php 此页面显示详细用法: detailed_usage.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM