Naveen
Naveen

Reputation: 1

how to insert multiple php array values into mysql for the same table?

Hi I am very new to php and I am having 3 php arrays namely bookname, bookprice and bookisbn, I need to insert the values like

"bookisbn" "bookname" "bookprice" into mysql

eg:

isbn1 bookname1 bookprice1 
isbn2  bookname2 bookprice2
isbn3  bookname3 bookprice3

As of now I tried to iterate the three arrays something like,

foreach($booknamearray as $bookname && $bookpricearray as $bookprice && $bookisbnarray as $bookisbn) { .. }

and

while($booknamearray as $bookname && $bookpricearray as $bookprice && $bookisbnarray as $bookisbn){ .. }

Nothing worked me, please kindly help me out to achieve this.

Thanks in advance, Naveen.

Upvotes: 0

Views: 113

Answers (2)

General_Twyckenham
General_Twyckenham

Reputation: 2261

Assuming they all have the same number of elements, you can use a for loop, and make a string of all the values:

for($i = 0; $i < count($booknamearray); $i++) {
    $str = $booknamearray[$i] . " " . $bookpricearray[$i] . " " . $bookisbnarray[$i];
    //Insert $str into db
}

Upvotes: 2

Marc B
Marc B

Reputation: 360572

While you don't say so explicitly, I'm guessing your 3 arrays have their various values in the SAME KEY, e.g.

$booknamearray[0] -> 'name1';
$bookisbnarray[0] -> 'isbn1';
$bookpricearray[0] -> 'price1';

In which case you can do

foreach($booknamearray as $key => $name) {
   $isbn = $bookisbnarray[$key];
   $price = $bookpricearray[$key];
   etc...
}

Upvotes: 0

Related Questions