Current location: Hot Scripts Forums » Programming Languages » PHP » insert data to multiple tables !


insert data to multiple tables !

Reply
  #1 (permalink)  
Old 04-02-04, 09:01 AM
puppy puppy is offline
Newbie Coder
 
Join Date: Apr 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
insert data to multiple tables !

hi,
i am having trouble inserting from a form to multiple mysql tables, i know it can be done with asp but my dev environment is:

php 4.3.4
mysql 4.13nt
dreamweaver mx

i am new to php, have been cramming but i'm only half way through the book and my brain is fried !

any suggestions would be greatly appreciated.

rob
Reply With Quote
  #2 (permalink)  
Old 04-02-04, 10:18 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
this can be achived with loops or with normal method ..
if you want to insert the exact data in more than one table then a loop is good for that ..
example:
PHP Code:

//I will assume that the form is sending username, comments and email ..

//and they are being posted via POST method..
$user trim(addslahes($_POST['username']));
$email trim($_POST['email']);
$comment trim(addslashes($_POST['comment']));

//add as many as you want of tables to this array
$tables[] = 'Table_1';
$tables[] = 'Table_2';
$tables[] = 'Table_3';
//if you want to add more just type:
//$tables[] = 'TABLE_NAME';

//ESTABLISH CONNECTION HERE..

//we start the loop here 
for ($i=0$i<count($tables); $i++) {
   
$insert mysql_query("INSERT INTO $tables[$i] (name, email, comment) VALUES ('$username', '$email', '$comment')")or
  die(
mysql_error());

note that this method can be only used if tables are identicales in structure

but if you want to insert e.g. $username to a table and $comment to another table then use the regular method
PHP Code:

$user trim(addslahes($_POST['username']));

$email trim($_POST['email']);
$comment trim(addslashes($_POST['comment']));

//here we insert username and comment only..
$insert mysql_query("INSERT INTO table_1 (name, comment) VALUES ('$username', '$comment')")or
die(
mysql_error());

//here we insert username and email
$insert mysql_query("INSERT INTO table_2 (name, email) VALUES ('$username', '$email')")or
die(
mysql_error());

//and so on .. 
hope that helps
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
  #3 (permalink)  
Old 04-02-04, 01:48 PM
puppy puppy is offline
Newbie Coder
 
Join Date: Apr 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
thanks, your time is much appreciated,

i'm going to need a couple of days to digest & "attempt deployment" oddly enough, it was loops that i had got to in the book when my brain "hung".

whilst the syntax seems logical, i'm not seeing the whole picture. It is hard to place all these concepts in a real practical context.

thanks again

yours "newbie" ly

rob
Reply With Quote
  #4 (permalink)  
Old 06-02-04, 10:14 AM
puppy puppy is offline
Newbie Coder
 
Join Date: Apr 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
$user = trim(addslahes($_POST['username']));

so, in the above variable the $user is variable name, the ['username'] is the variable value, what if i'm using an array of values, how would you construct for that ?

would you concatenate ?

$game = trim(addslashes($_POST['pacman', 'defender','space invaders']));

rob
Reply With Quote
  #5 (permalink)  
Old 06-02-04, 10:40 AM
puppy puppy is offline
Newbie Coder
 
Join Date: Apr 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
..........cont

and what if in the case of a normalised database such as:

users (user_id, address & so on)
game table (game_id, game) (1, pacman, 2, defender, 3 space invaders)
user_rel_game (user_id, game_id)

i would want to enter values 1, 2 or 3 via the variable directly in to the user_rel_game table, how would i specifically script that ?

hope thats not asking too much, beginning to understand this now.

your help is much appreciated.

rob
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
UPDATE multiple tables Grüssle PHP 0 02-03-04 02:41 AM
MySQL table problem perleo PHP 9 12-16-03 01:16 PM
sorting multiple tables elmeto PHP 1 08-13-03 09:07 AM
Mail Script sending multiple times when Table has a lot of data dsumpter PHP 12 07-16-03 05:17 PM


All times are GMT -5. The time now is 10:22 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.