Bobj-C
Bobj-C

Reputation: 5426

what is wrong with this code

class MyClass {

    private $db;

    // Constructor 
    function __construct() {
        $this->db = new mysqli('localhost', 'root', 'root', 'Test_db');
        $this->db->autocommit(FALSE);
    }

    // Destructor 
    function __destruct() {
        $this->db->close();
    }

    // Main method 
    function MyFun() {

        // Check for required parameters
        if (isset($_POST["name"]) && isset($_POST["username"]) && isset($_POST["password"]) && isset($_POST["email"])) {
        echo "Before \n";
        $name = $_POST["name"];
        $username = $_POST["username"];
        $password = $_POST["password"];
        $email = $_POST["email"];
        $activation = 0;
        echo "After \n";
            // tracking 
            $stmt = $this->db->prepare("INSERT INTO users (name, username, password, email,activation) VALUES (?, ?, ?, ?, ?)");
            $stmt->bind_param("is", $name, $username, $password, $email, $activation); //Line 95
            $stmt->execute();
            $stmt->close();

        }

Output:

Before

After

Invalid request

MAMP Console:

[15-Apr-2011 15:09:10] PHP Warning:  mysqli_stmt::bind_param() [<a href='function.mysqli-stmt-bind-param'>function.mysqli-stmt-bind-param</a>]: Number of elements in type definition string doesn't match number of bind variables in /Applications/MAMP/htdocs/Test/reg.php on line 95

The number is the same but I don't know why this error appears

Upvotes: 0

Views: 280

Answers (3)

Phill Pafford
Phill Pafford

Reputation: 85348

The "is" is the sixth variable, I suggest you remove this or add the field name in the statement:

$stmt->bind_param("is", $name, $username, $password, $email, $activation);

Either remove from bind_param:

$stmt = $this->db->prepare("INSERT INTO users (name, username, password, email,activation) VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param($name, $username, $password, $email, $activation);

or add to field names:

$stmt = $this->db->prepare("INSERT INTO users (**is**, name, username, password, email,activation) VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param("**is**", $name, $username, $password, $email, $activation);

or $stmt = $this->db->prepare("INSERT INTO users (name, username, password,
email,activation) VALUES (?, ?, ?, ?, ?)"); $stmt->bind_param("issss", $name, $username, $password, $email, $activation);

Upvotes: 1

Jeff Parker
Jeff Parker

Reputation: 7517

$stmt->bind_param("is", $name, $username, $password, $email, $activation);

Your "definition" string ("is") contains only two definitions, integer and string ... you should have 5 in there.

$stmt->bind_param("sssss", $name, $username, $password, $email, $activation);

... for example ...

Upvotes: 4

halfdan
halfdan

Reputation: 34224

You are only having five ? placeholders in your query, yet you're trying to bing six values to the query. $stmt->bind_param("is", $name, $username, $password, $email, $activation);

  1. "is"
  2. $name
  3. $username
  4. $password
  5. $email
  6. $activation

The format you are giving does only contain 2 definition, yet it must contain 5 to match your query. Try "sssss".

Upvotes: 2

Related Questions