Jump to content

MYSQL INSERT Prepared statement - Whats wrong?


Ovenmit

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);
  
            $stmt->execute();
            $stmt->close();
        }
        else {
            echo "ERROR: SQL statement failure!";
            echo "<a href='addnews.php'> -> OK</a>";
        }
        $mysqli->close();

 

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

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?

 

ie:

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

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.

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.

 

- SOLVED -

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.