1,105,232 Community Members

insert data from php array to mysql database

Member Avatar
thilini_l
Newbie Poster
2 posts since Feb 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

how to insert data in multi dimension array into mysql database in php.

Member Avatar
pritaeas
mod_pritaeas
11,285 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,829 [?]
Skill Endorsements: 154 [?]
Moderator
Featured
Sponsor
 
0
 

Can you show what you have, and how you want it inserted?

Member Avatar
Samyx
Junior Poster
104 posts since Sep 2009
Reputation Points: 36 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

It would help if we could take a look at the array.

Member Avatar
thilini_l
Newbie Poster
2 posts since Feb 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

this is what I have done so far.I'm little bit new to this suff.please help...

    <?php

       require 'connection1.php';

     if(isset($_POST['btnsub'])){

        $num=$_POST['txtnum'];
        $name=$_POST['txtname'];
        $age=$_POST['txtage'];

        $stu=array(

            0=>array('number'=>$num,'name'=>$name,'age'=>$age),
            1=>array('number'=>$num,'name'=>$name,'age'=>$age),
            2=>array('number'=>$num,'name'=>$name,'age'=>$age),
        );

        foreach( $stu as $key =>$info){

            $in="INSERT INTO $tbl ('number','name','age') VALUES ('$key','$info[number]','$info[name]','$info[age]')";

        }

     }



    ?>
Member Avatar
diafol
Where are my eyes?
12,940 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,845 [?]
Skill Endorsements: 90 [?]
Moderator
Featured
Sponsor
 
0
 

THat won't work - as you probably know. WHy are you using a repeat of the data into a multidimensional array? THe loop won't work as you expect either. If you have a Primary Key field, just ignore this in the SQL - so no need for a $key - anyway you have different number of fields and values, so it'll throw an error. Also you need to sanitize your input (post) variables before using this type of query. If you use a bound, parameterized query (mysqli or PDO), then it's OK.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article