1

I am attempting to bind params to a sql statement using call_user_func_array as describe on Dynamically Bind Params in Prepared Statements with MySQLi; however, my mysqli_prepare keeps returning false.

Here is my data function that is called to store data:

function storeData($form_data, $table_name, $cxn){
if(!is_array($form_data)){
    return false;
    exit();
}
$types = str_repeat("s", count($form_data));
$params = array();
$params[] = &$types;
$keys = array_keys($form_data);
$values = array_values($form_data);
for ($i = 0; $i < count($values); $i++) {
    $params[] = &$values[$i];
}

$sql = "INSERT INTO $table_name (" . implode(',', $keys) . ") VALUES (" .
    implode(',', array_fill(0, count($values), '?')) . ")
    ON DUPLICATE KEY UPDATE ";
$updates = implode(',', array_map(function($col) {
    return "$col = VALUES($col)";
}, $keys));
$sql .= $updates;

if($stmt = mysqli_prepare($cxn, $sql)){
    call_user_func_array(array($stmt, 'bind_param'), $params);
    return mysqli_stmt_execute($stmt);
}

Here is my $sql string at time of prepare:

$sql"INSERT INTO interest (Baseball,Basketball,Camping,Canoeing,Cycling,Football,Gaming,Golf,Hiking,Parks,Photography,Runway,Skydiving,Soccer,Username) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE Baseball = VALUES(Baseball),Basketball = VALUES(Basketball),Camping = VALUES(Camping),Canoeing = VALUES(Canoeing),Cycling = VALUES(Cycling),Football = VALUES(Football),Gaming = VALUES(Gaming),Golf = VALUES(Golf),Hiking = VALUES(Hiking),Parks = VALUES(Parks),Photography = VALUES(Photography),Runway = VALUES(Runway),Skydiving = VALUES(Skydiving),Soccer = VALUES(Soccer),Username = VALUES(Username)"

Here is my $params and $key outputs:

$keysarray[15]
$keys[0]"Baseball"
$keys[1]"Basketball"
$keys[2]"Camping"
$keys[3]"Canoeing"
$keys[4]"Cycling"
$keys[5]"Football"
$keys[6]"Gaming"
$keys[7]"Golf"
$keys[8]"Hiking"
$keys[9]"Parks"
$keys[10]"Photography"
$keys[11]"Runway"
$keys[12]"Skydiving"
$keys[13]"Soccer"
$keys[14]"Username"

$paramsarray[16]
$params[0]"sssssssssssssss"
$params[1]"0"
$params[2]"0"
$params[3]"0"
$params[4]"0"
$params[5]"0"
$params[6]"0"
$params[7]"0"
$params[8]"0"
$params[9]"0"
$params[10]"0"
$params[11]"0"
$params[12]"0"
$params[13]"0"
$params[14]"0"
$params[15]"test0613"

$valuesarray[15]
$values[0]"0"
$values[1]"0"
$values[2]"0"
$values[3]"0"
$values[4]"0"
$values[5]"0"
$values[6]"0"
$values[7]"0"
$values[8]"0"
$values[9]"0"
$values[10]"0"
$values[11]"0"
$values[12]"0"
$values[13]"0"
$values[14]"test0613"
Kevlwig
  • 133
  • 2
  • 11
  • 1
    You seem to have overlooked the parts of the blog post that show what to do `if($stmt === false) { ...` – Michael - sqlbot Jun 15 '17 at 02:05
  • @Michael `if($stmt = mysqli_prepare($cxn, $sql))` does exactly that. The `if()` statement checks if the `$stmt` is false. If the express returns false $stmt will be 0. – Kevlwig Jun 15 '17 at 02:35
  • Yes, I understand that... but you have shown no `else` block, that should provide a subsequent check of `$cxn->error`, which, along with `$cxn->errno` and `$cxn->sqlstate` contain the answer to your question of **why** it was false. – Michael - sqlbot Jun 15 '17 at 11:24
  • @Michael `mysqli_error()==""` & `mysqli_sqlstate($cxn)=="0000"` – Kevlwig Jun 15 '17 at 16:19
  • That's unexpected. – Michael - sqlbot Jun 15 '17 at 16:46
  • @Michael-sqlbot you sent me down the right path to solve this issue. So i did find some other reporting measures that allowed me to produce the error which was an undefined column in my table – Kevlwig Jun 15 '17 at 17:29
  • Great... in addition to the citation of the other answer, would you also include some details from the other answer in your answer, below, for the benefit of future visitors? Or, we can close this as a duplicate of the question where the solution is already found. – Michael - sqlbot Jun 15 '17 at 17:38

1 Answers1

1

There error existed in a column i was attempting to map which did not exist. The error procedure was found here, which allowed me to produce fatal errors that noted a column did not exist in the table I was referencing.

Kevlwig
  • 133
  • 2
  • 11