Hello I've tried both these questions solutions (final goal added at bottom)
but I don't get the expected variables content in $fields and $newdata
so I kindly print here some var_dump and cast to kindly ask your support.
My array derivate from an html table
For simplicity in my learning experiment I'm working with a dummy table of just 5 fields, as you see they are: selected
, user_id
, user_name
, user_company
and user_email
.
Finally I have inserted just 2 rows of values.
The table content is posted as JSON.stringify.
Here you my results
Using the usual
print_r ( $Arr );
I can see this output
Array (
[0] => Array ( [selected] => [user_id] => 3 [user_name] => nome3 [user_company] => azien3 [user_email] => email3 )
[1] => Array ( [selected] => 1 [user_id] => 6 [user_name] => nome6 [user_company] => azien6 [user_email] => email6 )
)
next I try to apply the code of from the two above questions
24 $fields = implode(",", array_keys($Arr));
25 $newdata = "'" . implode("','", $Arr) . "'";
26
27 var_dump($fields);
28 echo "<br><br>";
29 var_dump($newdata);
But something is wrong in my interpretation or in my code , because the output is
Notice: Array to string conversion in D:\xampp\htdocs\ajax-json\post.php on line 25 Notice: Array to string conversion in D:\xampp\htdocs\ajax-json\post.php on line 25 string(3) "0,1" string(15) "'Array','Array'"
can you kindly point out what's wrong? e.g. is my array properly formed?
the final goal is to build a query where they are bind the keys names and key values taken from the associative array directly to columns and values for an INSERT into a mysql table.
In other words since the array's keys names are identical to the database table's columns names, I'm wondering how to make an automatism that creates the query like in the two questions in the opening of this question.
With "automatism" is meant to HAVE variables and maybe cycles to build a query INSTEAD than writing the single columns names and the same for the columns values to be inserted
Edit: from the accepted answer, this is the working code.
$my_keys = array_keys($Arr[0]);
// ---- This prevents PDO SQL Injection
$stmt=$pdo->prepare("DESC my_table");
$stmt->execute();
$whitelist_columns=$stmt->fetchAll(PDO::FETCH_COLUMN);
foreach($my_keys as $key){
if(!array_search($key,$whitelist_columns)){ echo "ERROR!"; }
}
// ---- End of prevention
$field_names = implode(",", $my_keys); // build column list
/** @JBH this foreach is needed otherwise the $q_markers will result not PDO placeholders like.
If this is missing, the query inserts all "" values, no matter if you'll adopt bindValue or bindParam**/
foreach($my_keys as &$key){
$key = ":".$key;
}
$q_markers = implode(",", $my_keys); // build PDO value markers
$stmt = $pdo->prepare("INSERT INTO my_table (".$field_names.") VALUES (".$q_markers.")");
foreach($Arr as $key => $val){
foreach($val as $bind_marker => &$bind_val){ /** @ JBH Without "&" here, it will work
only bindValue. Instead with "&", they work both bindParam and bindValue **/
$stmt->bindParam($bind_marker, $bind_val);
}
$stmt->execute();
}