За последние 24 часа нас посетили 21970 программистов и 987 роботов. Сейчас ищут 696 программистов ...

Insert Json file int Mysql error: Undefined index

Тема в разделе "PHP и базы данных", создана пользователем Vieru Igor, 12 ноя 2018.

  1. Vieru Igor

    Vieru Igor Новичок

    С нами с:
    12 ноя 2018
    Сообщения:
    4
    Симпатии:
    0
    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.
     
  2. Sail

    Sail Старожил

    С нами с:
    1 ноя 2016
    Сообщения:
    1.591
    Симпатии:
    360
    @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:
    1. $address = array_key_exists('address', $user) ? $user['address'] : '';
    --- Добавлено ---
    or use actual name (key, index) for item of array
     
  3. Vieru Igor

    Vieru Igor Новичок

    С нами с:
    12 ноя 2018
    Сообщения:
    4
    Симпатии:
    0
    Thank you Sail.
    I/m a beginner in this, can you please help how to check based on my example?
    Thank you
     
  4. Sail

    Sail Старожил

    С нами с:
    1 ноя 2016
    Сообщения:
    1.591
    Симпатии:
    360
    See the code under spoiler in my message (#2)
     
  5. Vieru Igor

    Vieru Igor Новичок

    С нами с:
    12 ноя 2018
    Сообщения:
    4
    Симпатии:
    0
    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
     
  6. Sail

    Sail Старожил

    С нами с:
    1 ноя 2016
    Сообщения:
    1.591
    Симпатии:
    360
    @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.
     
  7. Vieru Igor

    Vieru Igor Новичок

    С нами с:
    12 ноя 2018
    Сообщения:
    4
    Симпатии:
    0
    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.
     
  8. Sail

    Sail Старожил

    С нами с:
    1 ноя 2016
    Сообщения:
    1.591
    Симпатии:
    360
    @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()