Inserting values into 2 seperate tables (MySQL)

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
cooler75
Forum Commoner
Posts: 40
Joined: Wed May 29, 2002 2:43 pm
Location: RichIsland

Inserting values into 2 seperate tables (MySQL)

Post by cooler75 »

Hi all,

I have a form for user to input:

Code: Select all

if ($submit)
{
$query = "INSERT INTO home (address1, address2)
         values ( '$address1', '$address2')";
print "Your listing has been added!";
}
print "<form name="add" action="add.php" method=post>";
print "Address1: <input type="text" name="address1">";
print "Address2: <input type="text" name="address2">";
print "<input type=submit name=submit value=Submit Listing></form>";
Here is my question, i have 2 tables, one called home, one called office, I want address1 to be inserted into home table, and I want address2 to be inserted into office table, anybody knows what i have to do with mySQL query? Or what is the better way to do this?


Thank you very much
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

You have to add them with two queries.
cooler75
Forum Commoner
Posts: 40
Joined: Wed May 29, 2002 2:43 pm
Location: RichIsland

Post by cooler75 »

hi, thank you

so you are saying it should be something like that?

Code: Select all

if ($submit) 
&#123; 
$query = "INSERT INTO home (address1) 
         values ( '$address1')";
$query = "INSERT INTO office (address2) 
         values ('$address2')"; 
 
print "Your listing has been added!"; 
&#125; 
print "<form name="add" action="add.php" method=post>"; 
print "Address1: <input type="text" name="address1">"; 
print "Address2: <input type="text" name="address2">"; 
print "<input type=submit name=submit value=Submit Listing></form>";
Ok, i will try it

thank you
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

yes, but DO the queries ;)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Further to what Volka said:

Code: Select all

if ($submit) 
&#123; 
@$db_conn = mysql_connect($host, $user, $pass) or die(mysql_error());
@mysql_select_db($db) or die(mysql_error());

$query1 = "INSERT INTO home (address1) 
         values ( '$address1')"; 
$query2 = "INSERT INTO office (address2) 
         values ('$address2')"; 

@mysql_query($query1) or die(mysql_error());
@mysql_query($query2) or die(mysql_error());

@mysql_close() or die(mysql_error());

print 'Your listing has been added!'; 
&#125; 
print '<form name="add" action="add.php" method=post>'; 
print 'Address1: <input type="text" name="address1" />'; 
print 'Address2: <input type="text" name="address2" />'; 
print '<input type="submit" name="submit" value="Submit Listing" /></form>';
Don't forget to connect and actually run the query :) . Also using single quotes around the print strings means you don't have to escape all the double quotes in the HTML.

Mac
cooler75
Forum Commoner
Posts: 40
Joined: Wed May 29, 2002 2:43 pm
Location: RichIsland

Post by cooler75 »

Thank you guys,
work great! :P
Post Reply