Jump to content

MYSQL INSERT Prepared statement - Whats wrong?


Recommended Posts

Hi, Im just having some trouble with this...maybe a fresh pair of eyes can help?  :shrug:


Im getting a "Warning: mysqli_stmt::bind_param() [mysqli-stmt.bind-param]: Number of elements in type definition string doesn't match number of bind variables" error when I try run this:


$date = date("Y-m-d");
        $header = $_POST['header'];
        $summary = $_POST['summary'];
        $content = $_POST['content'];

        $query = "INSERT INTO articles (pubdate, title, summary, content) VALUES(?, ?, ?, ?)";
        $stmt = $mysqli->stmt_init();
        if ($stmt->prepare($query)){
            $stmt->bind_param('i,s,s,s', $date, $header, $summary, $content);
        else {
            echo "ERROR: SQL statement failure!";
            echo "<a href='addnews.php'> -> OK</a>";


It looks fine to me, just can't see whats wrong lol!

Link to comment
Share on other sites

Why '?'

    $query = "INSERT INTO articles (pubdate, title, summary, content) VALUES(?, ?, ?, ?)";


Not sure how to answer... because I wanted to use a prepared statement? Are you saying I should just go with a straight query?



$query = "INSERT INTO articles (pubdate, title, summary, content) VALUES('$date', '$header', '$summary', '$content')";

Link to comment
Share on other sites

Sorry, had to step out and see a client. Running it as a straight query works.


You are separating the types field when you shouldn't be, Try this.

$stmt->bind_param('isss', $date, $header, $summary, $content);

Edit: In fact, you should be using 'ssss' because the $date variable is not an integer.


Thanks a bunch, that was the problem lol, 'ssss' instead of 'i,s,s,s'. Stupid mistake on my part and thanks for the date tip.






Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.