我有这个数组
(
[0] => ('id', '2')
[1] => ('name', 'Jhon')
)
这是插入的内容,但是doesen工作,请帮助......任何想法?
function redeem() {
$json = file_get_contents('php://input');
$obj = json_decode($json,true);
$rows = array();
foreach($obj as $key => $value)
{
$rows[] = "('" . $key . "', '" . $value . "')";
error_log(print_r($rows,true));
}
$stmt = $this->db->prepare('INSERT INTO prueba (id,nombre) VALUES (%d,%s)',$value->id,$value->name) or die(mysqli_error($this->db));
$stmt->execute();
}
答案 0 :(得分:0)
function redeem() {
$json = file_get_contents('php://input');
$obj = json_decode($json,true);
// Assumes $obj == array(0 => $assocArray, 1 => $assocArray)
foreach($obj as $index => $assocArray) {
// Assumes $assocArray == array(0 => array('id' => '2'), 1 => array('name' => 'John'))
$stmt = $this->db->prepare('INSERT INTO prueba (id,nombre) VALUES (%d,%s)',$assocArray[0]['id'],$assocArray[1]['name']) or die(mysqli_error($this->db));
$stmt->execute();
}
}
不优雅,但可能会奏效
这里有很多假设,而且我会感到困倦叹息,但我也假设$this->db
是一个带有数据库包装器的类,因为prepare语句就像我以前从未见过的那样,MySQL预备语句应该类似于:
$prep = $this->db->prepare("INSERT INTO prueba (id,nombre) VALUES (?,'?')")
$prep->bindParam(1, $assocArray[0]['id']);
$prep->bindParam(2, $assocArray[1]['name']);
$prep->execute();