简体   繁体   中英

php mysql prepared statement bind param error

i have been stuck on this error. Appreciate any help on this:

this function is part of a longer code working with ajax. Ajax has no problem reaching into the function, retrieving post data. code returns results with $message and it gets back as to ajax, and data is retrieved as response.message

Error happens the moment it runs into the bind_param.

Tried commenting the codes from bind_param down and ajax returns test messages just fine.

When un-commented bind_param, even with if bind_param fails send message 'fail', else send message 'pass'. nothing gets into the $message.

any ideas to why this happens?

code:

function edit_Loc_Name($connection){//67
    $new_loc_name = mysqli_real_escape_string($connection, $_POST['location_editloc_name']);
    $projid = mysqli_real_escape_string($connection, $_POST['projid']);
    $loc_id = mysqli_real_escape_string($connection, $_POST['edit_loc_id']);
    $checklocname = checkLocationLoc($projid,$new_loc_name,$connection);
    if ($checklocname === "Duplicate location."){
        $message = "Duplicate location.";
    }else if($checklocname === "Location okay"){
        $stmt = $connection->prepare("UPDATE projectlocation SET locname = ? WHERE id = ?");
        if($stmt === false){
            $message = "Ajax err:67 1";$stmt->close();
        }else{
            $stmt->bind_param('si',$new_loc_name,$loc_id);
            $rc = $stmt->execute();
            if($rc === false){
                $message = "Ajax err:67 3";$stmt->close();
            }else{
                $message = "Location updated.";$stmt->close();
            }
        }
    }else{
        $message = "Ajax err:67 5";
    }
    $connection->close();
    return $message;
}

You are passing the data to real_escape_string (as commented above, it is not needed if you bind later).

This function returns a string, so your $loc_id is a string now.

But in the binding:

$stmt->bind_param('si',$new_loc_name,$loc_id);

You declare the data as si (string, integer) instead of ss.

Try to fix this.

Update:

As an example, I ran this script in my localhost, and it is working properly:

    $connection=mysqli_connect("localhost", "root", "", "my-db");

function edit_Loc_Name($connection){
$loc_id = 1;
$checklocname = "Location okay";    

if ($checklocname === "Duplicate location."){
    $message = "Duplicate location.";
}else if($checklocname === "Location okay"){
    $stmt=$connection->prepare("select * from my-table where id=?");
    if($stmt === false){
        $message = "Ajax err:67 1";//$stmt->close();
    }else{
        $stmt->bind_param('i',$loc_id);
        $rc = $stmt->execute();
        if($rc === false){
            $message = "Ajax err:67 3";//$stmt->close();
        }else{
            $message = "Location updated.";//$stmt->close();
        }
    }
}else{
    $message = "Ajax err:67 5";
}
$connection->close();
return $message;
}
echo edit_Loc_Name($connection)

During multiple function calls, $connection was closed.

checkLocationLoc($projid,$new_loc_name,$connection) had closed the $connection.

Therefore all $connection related methods are not responding.

Once the $connection->close(); is removed, all is working well.

Sorry for the trouble guys. It's a painstaking lesson to me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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