Array ( [0] => Array ( [sn] => 1 [mob_no] => 9602858989 [date] => 06-May-2015 [time] => 12:02:33 PM ) [1] => Array ( [sn] => 2 [mob_no] => 7795055128 [date] => 06-May-2015 [time] => 12:29:44 PM ) )

how to insert these array values in seperate database table fields?

Recommended Answers

We provide answer to unfinished script, hoping, that the OP will change his/her script sometime to more secure one. As of now, the question is other than "how do I protect my script?".

I disagree as I don't think this can be assumed. If you ('the expert') provide …

Jump to Post

All 13 Replies

Re: how to store array values into mysql table 80 80

Let's say, this array is called $myarray.

mysqli_query("INSERT INTO table (sn, mob_no, date, time) VALUES ($myarray["sn"], $myarray["mob_no"], $myarray["date"], $myarray["time"]));
Re: how to store array values into mysql table 80 80

thanks for reply Aeonix

my code is like this:

$output = file_get_contents($api_url);
if($output=="")
{
    echo "No output received";
}
else
{
    $arr_output = json_decode($output, true);
    if(isset($arr_output['msg']))
    {
        $msg = $arr_output['msg'];
        $msg_text = $arr_output['msg_text'];

        if($msg == "SUCCESS")
        {
            if(isset($arr_output['data']))
            {
                $arr_data = $arr_output['data'];

                // above array will contain your data
                // print_r($arr_data);
            }
            else
            {
                echo "Json data not set !!";
            }
        }
        else
        {   
            echo "Error : ".$msg_text;
        }
    }
    else
    {
        echo "Output not set !!";
    }
}

while($item = array_shift($arr_data))
{
    foreach ($item as $key => $value)
    {
        echo $key.' => '.$value."\n";
    }
}

right now i am getting output as

sn => 1 mob_no => 9602858989 date => 06-May-2015 time => 12:02:33 PM sn => 2 mob_no => 7795055128 date => 06-May-2015 time => 12:29:44 PM

now how to store into database table fields like sn, mob_no, date, time

Re: how to store array values into mysql table 80 80
$output = file_get_contents($api_url);
if($output=="")
{
    echo "No output received";
}
else
{
    $arr_output = json_decode($output, true);
    if(isset($arr_output['msg']))
    {
        $msg = $arr_output['msg'];
        $msg_text = $arr_output['msg_text'];
        if($msg == "SUCCESS")
        {
            if(isset($arr_output['data']))
            {
                $arr_data = $arr_output['data'];
                // above array will contain your data
                // print_r($arr_data);

                mysqli_query("INSERT INTO table (sn, mob_no, date, time) VALUES ($myarray["sn"], $myarray["mob_no"], $myarray["date"], $myarray["time"]));
            }
            else
            {
                echo "Json data not set !!";
            }
        }
        else
        {   
            echo "Error : ".$msg_text;
        }
    }
    else
    {
        echo "Output not set !!";
    }
}
while($item = array_shift($arr_data))
{
    foreach ($item as $key => $value)
    {
        echo $key.' => '.$value."\n";
    }
}

I'm assuming that commented array is my target array. You need to make sure to have config file out there, and include it, which would connect to SQL database. And execute the script.

If it's still not working, specify where exactly are you getting your input from.

Re: how to store array values into mysql table 80 80

@Aeonix, your query is having problem with the double quotes. mysqli_query("INSERT INTO table (sn, mob_no, date, time) VALUES ($myarray["sn"], $myarray["mob_no"], $myarray["date"], $myarray["time"])); The sn,mob_no,date,time will read as variable and is not defined.

commented: Thanks for the information, I really forgot and haven't even noticed. +2
Re: how to store array values into mysql table 80 80

Oh, crap! Thanks! Oh, crap, use this one instead:

$output = file_get_contents($api_url);
if($output=="")
{
    echo "No output received";
}
else
{
    $arr_output = json_decode($output, true);
    if(isset($arr_output['msg']))
    {
        $msg = $arr_output['msg'];
        $msg_text = $arr_output['msg_text'];
        if($msg == "SUCCESS")
        {
            if(isset($arr_output['data']))
            {
                $arr_data = $arr_output['data'];
                // above array will contain your data
                // print_r($arr_data);
                mysqli_query("INSERT INTO table (sn, mob_no, date, time) VALUES ($myarray['sn'], $myarray['mob_no'], $myarray['date'], $myarray['time'])");
            }
            else
            {
                echo "Json data not set !!";
            }
        }
        else
        {   
            echo "Error : ".$msg_text;
        }
    }
    else
    {
        echo "Output not set !!";
    }
}
while($item = array_shift($arr_data))
{
    foreach ($item as $key => $value)
    {
        echo $key.' => '.$value."\n";
    }
}
Re: how to store array values into mysql table 80 80

As from my view, since $arr_data containing values of Array ( [0] => Array ( [sn] => 1 [mob_no] => 9602858989 [date] => 06-May-2015 [time] => 12:02:33 PM ) [1] => Array ( [sn] => 2 [mob_no] => 7795055128 [date] => 06-May-2015 [time] => 12:29:44 PM ) )
So probably we need a looping and since the query cannot directly nested an array variable, I suggested:

foreach($arr_data as $data){
    $query = "INSERT INTO table (sn, mob_no, date, time) VALUES (".$data['sn'].", ".$data['mob_no'].", ".$data['date'].", ".$data['time'].")";
    //carry out the query here
}
commented: True, haven't thought of that. +0
Re: how to store array values into mysql table 80 80

Okay, forget what I said, ^^^^, better solution.

Re: how to store array values into mysql table 80 80
Member Avatar

Is it me or are we open to SQL injections here as we're using raw input and not sanitizing. Better to use prepared statements.

Have a look at a class I created recently for multiple insert - you do not want to run multiple INSERTs nor inject raw data into a query:

https://www.daniweb.com/web-development/php/code/495371/insert-multiple-records-with-pdo-prepared-statement

It uses PDO, but could be modified quite easily for mysqli.

Re: how to store array values into mysql table 80 80

Is it me or are we open to SQL injections here as we're using raw input and not sanitizing. Better to use prepared statements.

We provide answer to unfinished script, hoping, that the OP will change his/her script sometime to more secure one. As of now, the question is other than "how do I protect my script?".

Re: how to store array values into mysql table 80 80
Member Avatar

We provide answer to unfinished script, hoping, that the OP will change his/her script sometime to more secure one. As of now, the question is other than "how do I protect my script?".

I disagree as I don't think this can be assumed. If you ('the expert') provide solutions using an unsecure method for the 'beginner', then I would assume that the beginner would accept the solution as being safe to use. If you have some experience with a language and can see the dangers, e.g. SQL injections, I think you are duty-bound to point this out to a user.

Re: how to store array values into mysql table 80 80

thanks lps, Aeonix & diafol for helping me to solve this issue.

Re: how to store array values into mysql table 80 80

one more thing, how to check the mob_no is already exist in table, it should store unique values i.e. unique mobile number in mob_no field

Re: how to store array values into mysql table 80 80
Member Avatar

Ensure that the mob_no field is indexed (unique):

CREATE UNIQUE INDEX indexname ON tablename (fieldname)

Give indexname a memorable 'short' name

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.