943,172 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 523
  • PHP RSS
Sep 3rd, 2010
0

Insert same data into different table in one time?

Expand Post »
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!
Similar Threads
Reputation Points: 10
Solved Threads: 2
Light Poster
stanley87 is offline Offline
37 posts
since Aug 2010
Sep 3rd, 2010
0
Re: Insert same data into different table in one time?
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.
Reputation Points: 10
Solved Threads: 4
Light Poster
Triztian is offline Offline
28 posts
since Sep 2009
Sep 3rd, 2010
0

thanks for reply

Click to Expand / Collapse  Quote originally posted by Triztian ...
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)
Reputation Points: 10
Solved Threads: 2
Light Poster
stanley87 is offline Offline
37 posts
since Aug 2010
Sep 3rd, 2010
0

Fairly easy

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"
SQL Syntax (Toggle Plain Text)
  1. CREATE TABLE nutrition
  2. (
  3. n_id INT NOT NULL AUTO_INCREMENT,
  4. nutrition VARCHAR(30),
  5. PRIMARY KEY(n_id) /* Set the primary key */
  6. )

TABLE "category"
SQL Syntax (Toggle Plain Text)
  1. CREATE TABLE nutrition
  2. (
  3. n_id INT NOT NULL AUTO_INCREMENT,
  4. category VARCHAR(30),
  5. PRIMARY KEY(n_id) /* Set the primary key */
  6. )

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 Syntax (Toggle Plain Text)
  1. <?php
  2. function addFood($nutrition_val,$category_val)
  3. {
  4. $q_nutrition="INSERT INTO nutrition (nutrition) VALUES ($nutrition_val)";
  5. $q_category="INSERT INTO category (category) VALUES ($category_val)";
  6.  
  7. mysql_query($q_nutrition); /* For table Nutrition */
  8. mysql_query($q_category); /* For table category*/
  9. }
  10. ?>

Hope it helps.

Regards, Triztian
Last edited by Triztian; Sep 3rd, 2010 at 8:09 pm.
Reputation Points: 10
Solved Threads: 4
Light Poster
Triztian is offline Offline
28 posts
since Sep 2009
Sep 3rd, 2010
0
Re: Insert same data into different table in one time?
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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
mohammed yaghi is offline Offline
9 posts
since Sep 2010
Sep 5th, 2010
0
Re: Insert same data into different table in one time?
Click to Expand / Collapse  Quote originally posted by Triztian ...
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"
SQL Syntax (Toggle Plain Text)
  1. CREATE TABLE nutrition
  2. (
  3. n_id INT NOT NULL AUTO_INCREMENT,
  4. nutrition VARCHAR(30),
  5. PRIMARY KEY(n_id) /* Set the primary key */
  6. )

TABLE "category"
SQL Syntax (Toggle Plain Text)
  1. CREATE TABLE nutrition
  2. (
  3. n_id INT NOT NULL AUTO_INCREMENT,
  4. category VARCHAR(30),
  5. PRIMARY KEY(n_id) /* Set the primary key */
  6. )

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 Syntax (Toggle Plain Text)
  1. <?php
  2. function addFood($nutrition_val,$category_val)
  3. {
  4. $q_nutrition="INSERT INTO nutrition (nutrition) VALUES ($nutrition_val)";
  5. $q_category="INSERT INTO category (category) VALUES ($category_val)";
  6.  
  7. mysql_query($q_nutrition); /* For table Nutrition */
  8. mysql_query($q_category); /* For table category*/
  9. }
  10. ?>

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
Reputation Points: 10
Solved Threads: 2
Light Poster
stanley87 is offline Offline
37 posts
since Aug 2010
Sep 5th, 2010
0
Re: Insert same data into different table in one time?
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
Reputation Points: 10
Solved Threads: 2
Light Poster
stanley87 is offline Offline
37 posts
since Aug 2010
Dec 26th, 2010
0
Re: Insert same data into different table in one time?
what if the n_id is auto_increment? what line of code should i use?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sahromo is offline Offline
7 posts
since Dec 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: How to set a convert Special Characters to normal like ê to ê
Next Thread in PHP Forum Timeline: PHP 2 MsSQL





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC