Hello all, please help in solving the next issue, I readed alot of posts but still didn't manage to fix it. I have a json file and want to insert this file with php in a mysql table. What I tried: file dp.php: <? session_start(); $SERVER_NAME = "localhost"; $DB_LOGIN = "root"; $DB_PASS = ""; $DB_NAME = "terminals"; $db = mysql_pconnect ( $SERVER_NAME, $DB_LOGIN, $DB_PASS ); @mysql_query ( "SET NAMES `utf8`" ); mysql_select_db( $DB_NAME, $db ); @mysql_query ( "SET NAMES `utf8`" ); ?> file for inserting json file in mysql: <?php require('db.php'); // reading json file $json = file_get_contents('terminals.json'); //converting json object to php associative array $data = json_decode($json, true); // processing the array of objects foreach ($data as $user) { $agent = $user['agent']; $id = $user['id']; $name = $user['name']; $address = $user['address']; $work_time = $user['work_time']; $lat = $user['lat']; $lng = $user['lng']; $type = $user['type']; $status = $user['status']; } //insert into mysql table $sql = "INSERT INTO terminale(agent, id, name, address, work_time, lat, lng, type, status) VALUES('$agent', '$id', '$name', '$address', '$work_time', '$lat', '$lng', '$type', '$status')"; ?> And the error what a recive in browser: **Notice: Undefined index: address in C:\xampp\htdocs\TEST\Test_Map\json-mysql\json-mysql.php on line 12** Please help with this.
@Vieru Igor, you must check if the given key or index exists in the array, using the (for example) function array_key_exists Спойлер like this: PHP: $address = array_key_exists('address', $user) ? $user['address'] : ''; --- Добавлено --- or use actual name (key, index) for item of array
Thank you Sail. I/m a beginner in this, can you please help how to check based on my example? Thank you
Ok Sail I see but what I have or need to with it? --- Добавлено --- I tried: $address = array_key_exists('address', $user) ? $user['address'] : ''; if ($address) { echo "Key exists!"; } else { echo "Key does not exist!"; } and receive the answer: Key exists! Notice: Undefined index: address in C:\xampp\htdocs\TEST\Test_Map\json-mysql\json-mysql.php on line 12
@Vieru Igor, make sure you edit the "C:\xampp\htdocs\TEST\Test_Map\json-mysql\json-mysql.php" file. And use var_dump($user, $user['address']) for visually check array.
yes Sail, I'm using:"C:\xampp\htdocs\TEST\Test_Map\json-mysql\json-mysql.php" file But if is possible to show in my code what to change. Thank you.
@Vieru Igor, @User is items of array $data May be some of them do not have item @User['address']. show results of var_dump(@DaTa), placed after @DaTa = json_decode($json, true); and var_dump(@User), allocated in cycle foreach()