Jump to content

inserting arrays into db


ppgpilot

Recommended Posts

A puzzle. Hope you have the answer...

 

I am trying to call data from a db and then input the data into another db

the data is for a diet website

the user chooses a recipe.

the recipe has several rows with several fields i.e.

 

item1, calorie, fat, protein

item2, calorie, fat, protein

item3, calorie, fat, protein

etc.

 

I need to pull the entries from the “recipe” db and place them into the “history” db containing the same field names.

 

I know I need a loop, but have not found the proper way to solve the problem

 

My latest attempt is included below.

 

I can echo the data correctly, but only the first entry enters the db

 

Any assist would be appreciated.

 

 

<?php

 

 

$sql=("select * from $tbl5 where recipe_name = '$recipe_name' ");

$result = mysql_query($sql);

$works = mysql_num_rows($result);

require 'my_error.php';

 

 

while($rows = mysql_fetch_array($result))

{

 

        $item = $rows['item'];

        $amount = $rows['amt'];

        $calorie = $rows['calorie'];

        $fat = $rows['fat'];

        $fat_calories = $rows['fat_calories'];

        $carb = $rows['carb'];

        $protein = $rows['protein'];

        $sugar = $rows['sugar'];

 

 

 

 

 

// insert this data into the history ref the date entered on the initial page

$sql = ("insert into $tbl3

 

        (

        user_name,

        entry,

        day,

        item,

        amt,

        calorie,

        fat,

        fat_calories,

        carb,

        protein,

        sugar

 

        )

        values

        (

 

        '$user_name',

        '$entry',

        '$day',

        '$item',

        '$amount',

        '$calorie',

        '$fat',

        '$fat_calories',

        '$carb',

        '$protein',

        '$sugar'

        )

 

        ");

 

 

 

 

$result = mysql_query($sql);

$works = mysql_affected_rows();

require 'my_error.php';

 

 

// end while

}

 

 

?>

 

Link to comment
https://forums.phpfreaks.com/topic/146316-inserting-arrays-into-db/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.