i have to insert a lot of data where it might be also use as foreign key at different table.is that possible to add 1 time together the value to all table?can give any example?thx in advance!:)

Where do u have to insert the data, to a MySQL table?, or to an array?
It can be done assuming that the tables or arrays in which you want to insert the data have a common key.

Where do u have to insert the data, to a MySQL table?, or to an array?
It can be done assuming that the tables or arrays in which you want to insert the data have a common key.

first,thanks for reply in advance
ya,the data i insert all have common key with the table i gonna insert,for example:i have a food nutrition table(nutrition_id,nutrition)where the nutrition_id is the the foreign key of another table such as category table(category_id,category,nutrition id) and oso other table also have the nutrition id as the foreign key!so is that possible when i insert a new nutrition id can also add simultaneously the nutrition id to other table also?
(i want say that the id i gonna insert is set to auto_increment condition)

Hi, well it's fairly easy, assuming that you use MySQL, this would be done using SQL queries according to the number of tables that needs to be updated, in this particular example we're updating two tables so two queries are executed the queries should be wrapped in a single function so that they're executed as a block.

TABLE "nutrition"

CREATE TABLE nutrition
(
   n_id int NOT NULL AUTO_INCREMENT,
   nutrition varchar(30), 
   PRIMARY KEY(n_id) /* Set the primary key   */
)

TABLE "category"

CREATE TABLE nutrition
(
   n_id int NOT NULL AUTO_INCREMENT,
   category VARCHAR(30),
   PRIMARY KEY(n_id) /* Set the primary key   */
)

Now the PHP function that inserts records, I also assume that the MySQL Connection has been made, to make it clearer I've omitted the MySQL connection code.

<?php
   function addFood($nutrition_val,$category_val)
   {
      $q_nutrition="INSERT INTO nutrition (nutrition) VALUES ($nutrition_val)";
      $q_category="INSERT INTO category (category) VALUES ($category_val)";
      
      mysql_query($q_nutrition); /* For table Nutrition */
      mysql_query($q_category); /* For table category*/
   }
?>

Hope it helps.

Regards, Triztian

hi stanley87
there is way in php to get the primary key of the insert/update done look example
mysql_query("insert into table1 values .........");
$myquery = "SELECT LAST_INSERT_ID() as new_id";
$myres = mysql_query($myquery);
$myrow = mysql_fetch_array($myres);
//// now $myrow[0] contain the primary key of the last insert done
//// now you can use this var in another statement

mysql_query("insert into table2(x) values ($myrow[0])");


thanks

Hi, well it's fairly easy, assuming that you use MySQL, this would be done using SQL queries according to the number of tables that needs to be updated, in this particular example we're updating two tables so two queries are executed the queries should be wrapped in a single function so that they're executed as a block.

TABLE "nutrition"

CREATE TABLE nutrition
(
   n_id int NOT NULL AUTO_INCREMENT,
   nutrition varchar(30), 
   PRIMARY KEY(n_id) /* Set the primary key   */
)

TABLE "category"

CREATE TABLE nutrition
(
   n_id int NOT NULL AUTO_INCREMENT,
   category VARCHAR(30),
   PRIMARY KEY(n_id) /* Set the primary key   */
)

Now the PHP function that inserts records, I also assume that the MySQL Connection has been made, to make it clearer I've omitted the MySQL connection code.

<?php
   function addFood($nutrition_val,$category_val)
   {
      $q_nutrition="INSERT INTO nutrition (nutrition) VALUES ($nutrition_val)";
      $q_category="INSERT INTO category (category) VALUES ($category_val)";
      
      mysql_query($q_nutrition); /* For table Nutrition */
      mysql_query($q_category); /* For table category*/
   }
?>

Hope it helps.

Regards, Triztian

Thanks guys,I solved it already.really thanks in advance.i am newbie in php and i am multimedia background,just wanna move to web development side,hope guys really can help in future ya!hehe

hi stanley87
there is way in php to get the primary key of the insert/update done look example
mysql_query("insert into table1 values .........");
$myquery = "SELECT LAST_INSERT_ID() as new_id";
$myres = mysql_query($myquery);
$myrow = mysql_fetch_array($myres);
//// now $myrow[0] contain the primary key of the last insert done
//// now you can use this var in another statement

mysql_query("insert into table2(x) values ($myrow[0])");


thanks

Thanks guys,I solved it already.really thanks in advance.i am newbie in php and i am multimedia background,just wanna move to web development side,hope guys really can help in future ya!hehe

what if the n_id is auto_increment? what line of code should i use?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.