-1

Some days ago I had this problem with bind_param and i found a temporarly solution, but this is not the right thing to do. I need to dinamically bind variables while registering a user, so I am using this with an INSERT INTO query. At beginning I thought it could be because I had to pass the value by reference, but as I read on php.net, since php 5.3 you don't have to pass the value by reference anymore. So i tried, tried and tried but can't understand where I am wrong.

// INSERT INTO
public function QueryInsert($table, $fields, $values, $format) {
    $values = explode(',',$values); // Metto i VALUES di Insert in un array        
    $placeholders = ""; // Inizializzo Segnaposto
    $data =  "";
    $i = 0;
    foreach($values as $k => $v) { // Creo i placeholders in base al numero di Valori VALUES
        $placeholders .= '?,';            
    }                     
    $placeholders = substr($placeholders, 0, -1); // Tolgo l'ultima virgola

    $DB = DBConn::ConnettiDB(); // Richiamo la funzione di connessione al DB        
    $statem = $DB->prepare("INSERT INTO $table($fields) VALUES ($placeholders)"); // Preparo query per inserimento                                     

    $statem->bind_param("$format",$values); // WORKING
    $statem->execute(); // Eseguo query INSERT        
    // Controllo se la query INSERT ha avuto successo
    if($statem->affected_rows != -1) {          
        return true;
    }           
    echo "<br>ERRORE Insert: <br>" . $statem->error;

} // FINE INSERT

I made this work by replacing this

$statem->bind_param("$format",$values); // WORKING

with this

$statem->bind_param("$format",$values[0],$values[1],$values[2]); // WORKING

since the $values is an array made with registration fields, so to add the user i have username, password and email, 3 fields, this way it works. The problem is I can't use this script to run dynamic queries, because it doesnt work with the line I said, with variable $values that is an array and I don't understand why bind_param doesn't accept it. Infact the error i get is:

No data supplied for parameters in prepared statement

Because it seems that bind_param can't work with that variable $values

I tried with call_user_function_array too, but didn't make it work either. Can someone tell me where i fail? Could it be the ref values thing? Because I am not sure, since I read that now you don't need to pass values by reference anymore. And I tried to do that too, it didn't work, maybe i didn't do the right way.

eartahhj
  • 31
  • 4
  • 2
    According to the docs (http://php.net/manual/es/mysqli-stmt.bind-param.php), you have to pass every value, not an array of them. You could use an arbitrary amount of values using `call_user_func_array`, check this question: http://stackoverflow.com/questions/16236395/bind-param-with-array-of-parameters – Alejandro Iván Feb 07 '16 at 02:33
  • Tomorrow I will look better since now I am tired but, I already tried with CUFA and i tried that now too and it never worked.. I really can't understand what the problem is, 100% is about values but even if I pass them as reference it doesn't work... this is driving me crazy really. Thanks anyway for the help – eartahhj Feb 07 '16 at 02:52
  • Maybe `call_user_func_array("bind_param", array_merge($format, $values));`? – Alejandro Iván Feb 07 '16 at 04:15

1 Answers1

0

Two possibilities. One, as mentioned in comments, use call_user_func_array(), which allows you to pass an array of arguments to any function. EDIT: According to this answer, using call_user_func_array() with bind_param() is tricky.

array_unshift($values, $format);
$statem = $DB->prepare("INSERT INTO $table($fields) VALUES ($placeholders)");
call_user_func_array(
    array($statem, "bind_param"),
    refValues($values)
);
$statem->execute();

function refValues($arr){
    $refs = array();
    foreach($arr as $key => $value)
        $refs[$key] = &$arr[$key];
    return $refs;
}

Another might be more work but is worth investigating; you need to change your database object to PDO instead of mysqli. But then you can do this, because PDO has no binding of variables needed.

$statem = $DB->prepare("INSERT INTO $table($fields) VALUES ($placeholders)");
$statem->execute($values);
if($statem->rowCount() > 0) {
    return true;
}
Community
  • 1
  • 1
miken32
  • 42,008
  • 16
  • 111
  • 154
  • I also tried again with Call User Fun Array but I can't make this work... I don't know if it's a problem of my host or whatever... call_user_func_array(array($statem, "bind_param"),array_merge($format,$values)); – eartahhj Feb 07 '16 at 15:27
  • I don't think PHP said "I can't make this work!" What errors do you get? Also add some checks to the results for the statement preparation, binding, and execution. – miken32 Feb 07 '16 at 15:33
  • I posted the error already, it's "No data supplied for parameters in prepared statement". I tried different versions of call user func array It's like it cant access the params of the array, i thought it was for the reference but even if I pass them with a reference function the error is the same. The problem is with array $values and I can't figure out why, I tried this for like 3 days and I tried everything I could think but this is making me crazy for real. For now, it only works when i do this line: $statem->bind_param("$format",$values[0],$values[1],$values[2]) That's why maybe is the ref – eartahhj Feb 07 '16 at 16:37
  • I tried to pass $values to the function as a string, as an array, as a single value, there is no way the only way is when i use $statem->bind_param and i pass the values directly accessing them with the key of they array, like $values[0], etc. I also tried to make a string with the params of the array, but nothing it says "can't pass parameter 2 by reference". Even if I try with that reference function it's the same problem. Man i'm going insane – eartahhj Feb 07 '16 at 16:43
  • Just tried my code and got "Parameter 2 to mysqli_stmt::bind_param() expected to be a reference, value given". Edit coming shortly. – miken32 Feb 07 '16 at 17:30
  • Maybe i am not the only one then ahah i start to think it's a problem with php or maybe something is not clear with the usage in the manual – eartahhj Feb 07 '16 at 20:09
  • It's quite simple if you validate your input and read the error messages. – miken32 Feb 07 '16 at 20:11
  • What do you mean, i'm not sure i understood – eartahhj Feb 09 '16 at 13:10