Number of variables doesn't match number of parameters in prepared statement

I’m sure there are other posts about this, but I can’t figure why the first INSERT works and the second INSERT gives me the stupid error… :mad:


	if ( !$isSecondHandItem )
	{
		$sql = "INSERT INTO
				Catalogue
				(
				Title,
				Author,
				Price,
				Publisher,
				Description,
				Format,
				Available,
				DisplayOnIndex,
				Category,
				Image,
				SubCategory,
				offer,
				Weight 
				)
			VALUES
				( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )";
	}
	else
	{
		$sql = "INSERT INTO
				secondhandBooks
				(
				bookTitle,
				bookAuthor,
				condition,
				price,
				Available,
				Weight,
				Publisher,
				Description,
				Format 
				)
			VALUES
				( ?, ?, ?, ?, ?, ?, ?, ?, ? )";
	}

    // fetch the data from the DB
    if ( ! $stmt = $db->prepare($sql))
    {
        $feedback = $stmt->error;
    }
    else
    {
        if ( !$isSecondHandItem )
	{
		$stmt->bind_param('ssissiisiiiii', $fTitle, $fAuthor, $fPrice, $fPublisher, $fDescription, $fFormat, $fAvailable, $fNewAddition, $fCategory, $fImage, $fSubCategory, $fFreeDelivery, $fWeight);
	}
	else
	{
		$stmt->bind_param('ssiiiissi', $fTitle, $fAuthor, $fCondition, $fPrice, $fAvailable, $fWeight, $fPublisher, $fDescription, $fFormat);
	}

        if ( ! $stmt->execute())
        {
            $feedback = $stmt->error;
        }
		else
		{
			$_SESSION['updatefeedback'] = '<p>Item was added</p>';
			$stmt->close();
		}
    }

    $db->close();

Please can someone help me with this? It makes no sense. :frowning:

Looks like you passing 14 params to the first statement and only have 13 placeholders. Just count them, why do we have to count them for you?

You don’t have to count them… Check again; it wants 13 and I’m passing 13. :wink:

The first SQL runs fine though, it’s the second SQL that fails.

I count 14 here:
$stmt->bind_param(‘ssissiisiiiii’, $fTitle, $fAuthor, $fPrice, $fPublisher, $fDescription, $fFormat, $fAvailable, $fNewAddition, $fCategory, $fImage, $fSubCategory, $fFreeDelivery, $fWeight);

the ‘ssissiisiiiii’ is param too

The second has a reserved word http://dev.mysql.com/doc/refman/5.4/en/reserved-words.html

Confusing because the error message doesn’t say that, but I’d bet that’s what it is.

Hi lampcms.com,

It’s my understanding that the ‘ssissiisiiiii’ isn’t a ‘real’ parameter as it isn’t bound to the query; it merely represents the data formats for the parameters that follow it. It’s only used for the bind and NOT passed into the SQL. So there are 13, not 14.

FYI I have an answer for this particular issue (though I have a similar issue elsewhere on the site):

Turns out that ‘condition’ is a SQL reserved word and so it needs to be ‘condition’ to make it work. Joy.

Though that doesn’t fix the issue I’m having with the UPDATE version of this statement… Same error but the quote fix doesn’t fix it.

Cheers Mittineague. :wink:

I found that out 10 minutes ago, thanks anyway!

Doesn’t fix my UPDATE error though… Same issue, almost identical code.