0
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?

4
Contributors
13
Replies
44
Views
2 Years
Discussion Span
Last Post by diafol
Featured Replies
  • 1

    @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. Read More

  • 1

    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 … Read More

  • 1
    diafol 3,720   2 Years Ago

    >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 … Read More

0

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"]));

Edited by Aeonix

0

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

0
$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.

Edited by Aeonix

1

@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.

Votes + Comments
Thanks for the information, I really forgot and haven't even noticed.
0

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";
    }
}

Edited by Aeonix

1

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
}
Votes + Comments
True, haven't thought of that.
0

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?".

1

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.

0

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

0

Ensure that the mob_no field is indexed (unique):

CREATE UNIQUE INDEX indexname ON tablename (fieldname)

Give indexname a memorable 'short' name

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.