Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ERRROR PDOStatement::bindParam() expects parameter 3 to be long, string given [duplicate]

Tags:

php

pdo

mysqli

Hopefully a quick and obvious error on my part, I had a peek through previous questions but could not find question directly related to my question, so forgive me for opening what should be considered a fairly simple question, but after years of working with mysql the PDO switch is not coming easily

What I want to do

Simply insert entered form data to a new row of my table

Code

HTML

<form name="animals" id="animals" method="post">
    <label for="animalType">Animal Type: </label>
    <input type="text" required="required" name="animalType" />
    <br />
    <br />
    <label for="animalName">Animal Name:</label>
    <input type="text" required="required" name="animalName" />
    <br />    
    <button type="submit" name="submit">Add</button>
</form>

PHP

if(isset($_POST['submit'])){
    //here we get the data submitted in form and assign to vars
    $type = $_POST['animalType'];
    $name = $_POST['animalName'];
    //here we are preparing our SQL statment
    $sql = "INSERT INTO animals
            (animal_type, animal_name)
             VALUES
             (?,?)";
    //prepare DB as per above        
    $statement = $db->prepare($sql);
    $statement->bindParam("ss", $type, $name);
    $success = $statement->execute();
    if($success){
        echo'Welldone Chuck, Successfully Updated'; 
    }
    else{
        $error = $db->error;
        echo'Whoopsy Doopsy someone made a boopsy, ERROR INFO: $error'; 
    }
    $statement->close();
}

Error:

PDOStatement::bindParam() expects parameter 3 to be long, string given

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: no parameters were bound'

Possible Reason For Error?

Dreamweaver as I am typing brings up a help function, as it often does (paramNo, Param, ParamType) should I specify that I am binding 2 parameters here? (which I tried but same error as above though.)

Table

enter image description here

Thanks a million

like image 711
Timothy Coetzee Avatar asked Jun 07 '26 03:06

Timothy Coetzee


1 Answers

Replace

$statement = $db->prepare($sql);
$statement->bindParam("ss", $type, $name);

with

$statement = $db->prepare($sql);
$statement->bindParam(1, $type, PDO::PARAM_STR);
$statement->bindParam(2, $name, PDO::PARAM_STR);

For more information, refer to the manual PDO Bind Param

like image 75
Naresh Kumar Avatar answered Jun 10 '26 16:06

Naresh Kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!