0

i need help with my function thet i build , i trying to use MYSQLI prepare but i am not so good .

this is my function :

   function insertToDb($table,$rowsarray,$valuequestionmarks,$lenstrings,$valarray){

        $this->mysqli->set_charset("utf8");
        if ($insert_stmt = $this->mysqli->prepare(
        "INSERT INTO $table ($rowsarray) 
         VALUES 
        ($valuequestionmarks)"
        ))
        {
        $insert_stmt->bind_param("$lenstrings",$valarray);
        // Execute the prepared query.
        if(!$insert_stmt->execute())
            {
             die("Execute failed: (" . $insert_stmt->errno . ") " . $insert_stmt->error);
            }
        }

   }

And this is how i call :

                   $img = "something.jpg";
                   $uip = ulUtils::GetRemoteIP(false);
                   $table='forgotpassqm';
                   $rowsarray = 'email,text,img,ip';
                   $valuequestionmarks ='?,?,?,?';
                   $lenstrings ='ssss';
                   $valarray ='$email,$text,$img,$uip';

                   $func->insertToDb($table,$rowsarray,$valuequestionmarks,$lenstrings,$valarray);

And i keep get this error :

  Warning: mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables

And the execute error :

Execute failed: (2031) No data supplied for parameters in prepared statement

i tryed allot of combination none work , i read other question none as my , and none worked or help either.

And i know this is about the ssss , but i using 4 and its seem to be alright so where i wrong here ? Thanks allot.

EDIT :

$table output : forgotpassqm .
$rowsaray output: email,text,img,ip .
$valuequestionmarks output : ?,?,?,? .
$lenstrings output: ssss.
$valarray output: $email,$text,$img,$uip.

I think the problem is at $valarray.

0

1 Answer 1

1

Judging by the looks of it you are attempting to send a comma-delimited list of variables as an array (not how an array works) and you are using single quotes so variables aren't being interpolated to their values.

bind_param() expects a list of arguments after the type definitions. You aren't sending a list, you are sending the string '$email,$text,$img,$uip'.
Your call to that function should look like this:

$stmt->bind_param("ssss", $email, $text, $img, $uip);
Sign up to request clarification or add additional context in comments.

4 Comments

What wrong with how i did ? did you look how i inserted the paramters via vars?
Yes, I did, however the statement you have of $valarray ='$email,$text,$img,$uip'; is nothing more than a string since you are using single quotes. I think I see what you are trying to do, but I would suggest you approach the implementation a bit different.
Ok i got it , i debuged and this is the problem , you have suggesion? maybe i use it as array() and then in the function i will use list ?
Have a look at this SO question: stackoverflow.com/questions/793471/… I think that will talks about what you are looking to do there (dynamic amount of vars).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.