<?php
$str = '{"fname":"lorem","sname":"ipsum","nick":"dolor"}';
function btn_send($str){
global $db;
$data = json_decode($str,true);
$column_names = "`". implode("`,`",array_keys($data)) . "`";
$prepared_chars = implode(",",array_fill(0,count(array_values($data)),'?'));
$sq = "insert into members ($column_names) values ($prepared_chars)";
$st = $db->prepare($sq);
$st->execute(array_values($data));
}
You can make the column names out of the json keys and have those many ?
placeholders as much as values for those keys. While executing the prepared query, you can just supply the values.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…