简体   繁体   中英

function to dynamic insert to Database using mysqli prepare

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM