Insert into Table associative array

General discussions related to php

Moderators: macek, egami, gesf

New php-forum User
New php-forum User
Posts: 2
Joined: Fri Feb 07, 2014 9:32 am

Insert into Table associative array

Postby Joak190867 » Fri Jun 06, 2014 12:51 pm

Hi guys

A mobile application send data in Json format, so I want to insert the data into Mysql table, I don't have any error message, I can insert into table, but not with the same values, only insert 0 in ID column, and null in name column, what's wrong?

The array has this data


[0] => ('id', '2')
[1] => ('name', 'Jhon')


The function to parse and insert data ...

function redeem() {
// Check for required parameters
$json = file_get_contents('php://input');
$obj = json_decode($json,true);
// prepare the query
$stmt = $this->db->prepare('INSERT INTO prueba (id,nombre) VALUES (?, ?)');

$assocArray = array('id' => '', 'name' => ''); // define $assocArrat var first

// bind the values to the placeholders, describing their data types
$stmt->bind_param('is', intval($assocArray['value1']), $assocArray['value2']);

// loop througght he json objects to insert into mysql
foreach($obj as $assocArray) {
$stmt->execute(); // execute the query
$stmt = $this->db->prepare('COMMIT');


Return to “PHP General”

Who is online

Users browsing this forum: No registered users and 2 guests