I was learning how to add values to the database with PHP and I have the problem with some code bellow where I want to add values to the database.

I tried this code bellow but every time I get this Warning: mysqli_stmt_bind_param(): Number of variables doesn't match number of parameters in prepared statement in C:\path\to\the\file.php on line 38

The line 38 is the line with the mysqli_stmt_bind_param() method

$sql  = "INSERT INTO times(uid,a,b,c,d,e,f,g,t12,t13,t14,t15,t16,t17,t18,t19,t20) VALUES ('?','?','?','?','?','?','?','?',?,?,?,?,?,?,?,?,?);";

  $stmt = mysqli_stmt_init($conn);
  if (!mysqli_stmt_prepare($stmt, $sql)) {
    header("Location: ../index.php?error=sqlerror");
  else {
    mysqli_stmt_bind_param($stmt, "sssssssssssssssss", $uid, $a, $b, $c, $d, $e, $f, $g, $t12, $t13, $t14, $t15, $t16, $t17, $t18, $t19, $t20);
    echo "done!";

The uid, a, b, c, d, e, f and g columns in the database are TINYTEXT type and the other columns are TINYINT(1) type.

There are 17 question marks in the $sql variable and exact 17 s's and parameters in the mysqli_stmt_bind_param() methon in the line 38. I was expecting that the values will be added into the table in my database but I can see just this warning on the website and there are no values in the database.

Can someone help me with this issue?

2 Answers

e.dan On

Try changing the first line to:

$sql  = "INSERT INTO times(uid,a,b,c,d,e,f,g,t12,t13,t14,t15,t16,t17,t18,t19,t20) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

The parameter markers ? should not be quoted in the SQL statement.

GMc On

Try not putting your question mark placeholders inside single quotes. '?' is a character string consisting of a single question mark. It is not a placeholder for a parameter.