简体   繁体   中英

fetch data from an array and save in database

i have an array that contains 50 index values. a part of tha array that i got after print_r($result); is

Array
(
    [0] => Array
        (
            [ClassScheduleID] => 
            [Location] => Array
                (
                    [Latitude] =>
                )
            [ClassDescription] => 
                (
                    [ImageURL] => 
                    [Level] =>
                        (
                            [Name] => 
                        )
                    [Program] =>
                        (
                            [Name] => 
                        )
                )
            [Staff] => Array
                (
                    [FirstName] => 
                )
        )
    [1] => Array
       (
            [ClassScheduleID] => 
            [Location] => Array
                (
                    [Latitude] =>
                )
            [ClassDescription] => 
                (
                    [ImageURL] => 
                    [Program] =>
                        (
                            [Name] => 
                        )
                )
            [Staff] => Array
                (
                    [FirstName] => 
                )
        )
    [2] => Array
        (
            [ClassScheduleID] => 
            [Location] => Array
                (
                    [Latitude] =>
                )
            [ClassDescription] => 
                (
                    [ImageURL] => 
                    [Program] =>
                        (
                            [Name] => 
                        )
                )
            [Staff] => Array
                (
                    [FirstName] => 
                )
        )
)       

i have fetched the data from the array by the following code (part of the code)

if (!empty($result)) 
    {
        foreach ($result as $res) 
            {
                $classscheduleid = $res['ClassScheduleID'];
                if(isset($res['Location'])) 
                    {
                        $l_latitude   = $res['Location']['Latitude'];
                    }

                if(isset($res['ClassDescription']))
                    {
                        $c_img = $res['ClassDescription']['ImageURL'];
                    }

                if(isset($res['ClassDescription']['Level']))
                    {
                        $c_l_name = $res['ClassDescription']['Level']['Name'];
                    }

                if(isset($res['ClassDescription']['Program']))
                    {
                        $c_p_name = $res['ClassDescription']['Program']['Name']; 
                    }   

                if(isset($res['Staff'])) 
                    {
                        $s_firstname = $res['Staff']['FirstName'];
                    }   

                $sql = "INSERT INTO class_detail (classscheduleid,l_latitude,c_img,c_l_name,c_p_name,s_firstname) VALUES ('".$classscheduleid."','".$l_latitude."',,'".$c_img."','".$c_l_name."','".$c_p_name."','".$s_firstname."')";
                if (mysqli_query($conn, $sql)) {
                    echo "New record created successfully";
                } else {
                    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
                }

            } 
    }   

The problem that i am facing is that when i am tryin to insert the value in database then it is not inserting the first 8 index values, it is starting to save values from [9] position. I am getting all the data properly, as i have checked by echo all the variables. can anyone tell how i can save the value of array properly in database

After $l_latitude = $res['Location']['Latitude']; insrt it into database with your normal insert query, get the last inserted id from here after that write an update query after $c_img = $res['ClassDescription']['ImageURL']; and put the check on last inserted id u had got earlier. do it similarly with rest of the part. It is a bit lengthy method, but will surely give you correct values and is easy to code

您可以尝试使用array_keys()获取数组键,并使用array_values()获取数组中的值,并将implode()逗号都插入,然后将其插入数据库。

Change

foreach ($result as $res)

To

foreach ($result as $key=>$res)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM