7

php mysql prepared statement bind param error

 2 years ago
source link: https://www.codesd.com/item/php-mysql-prepared-statement-bind-param-error.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

php mysql prepared statement bind param error

advertisements

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)




About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK