We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,548 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

insert data from php array to mysql database

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

4
Contributors
4
Replies
1 Day
Discussion Span
3 Months Ago
Last Updated
22
Views
thilini_l
Newbie Poster
2 posts since Feb 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

pritaeas
Posting Prodigy
Moderator
9,534 posts since Jul 2006
Reputation Points: 1,194
Solved Threads: 1,494
Skill Endorsements: 98

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

Samyx
Junior Poster
104 posts since Sep 2009
Reputation Points: 46
Solved Threads: 2
Skill Endorsements: 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]')";

        }

     }



    ?>
thilini_l
Newbie Poster
2 posts since Feb 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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.

diafol
Keep Smiling
Moderator
10,838 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,534
Skill Endorsements: 61

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0753 seconds using 2.74MB