Dealing with bind_param when inserting data from an array
Posted: Sat Mar 10, 2007 3:31 pm
I am trying to write a function that will insert a new record using data from an array. I have most of the function written out except for the bind_param. Does anyone have any suggestions on how to finish this function or a better way of achieving the same thing?
Code: Select all
// $valueArray could look like this...
// [0] => array('fieldName'=>'name_first', 'fieldValue'=>'Mike', 'fieldType'=>'s');
// [1] => array('fieldName'=>'name_last', 'fieldValue'=>'Jenkins', 'fieldType'=>'s');
// [2] => array('fieldName'=>'userid', 'fieldValue'=>'5016', 'fieldType'=>'i');
function addNewRecord($valueArray, $tableName){
$recordAdded = false;
$numOfValues = count($valueArray);
//first make sure some values were passed to this function so that we can create a record
if($numOfValues > 0){
//create question mark string and value type string
for($i=0, $i < $numOfValues, $i++){
if($i == 0){
$questionMarks = "?";
}else{
$questionMarks .= ", ?";
}
$valueTypes .= $valueArray[i]["fieldType"];
}
$statement = $this->connection->prepare("INSERT INTO $tableName VALUES ($questionMarks)");
$statement->bind_param($valueTypes, /***** any ideas on how do I handle this part *****/);
$recordAdded = $statement->execute();
}
return $recordAdded;
}