Page 1 of 1

Inserting values into 2 seperate tables (MySQL)

Posted: Thu Jun 06, 2002 2:55 pm
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

Posted: Thu Jun 06, 2002 7:53 pm
by volka
You have to add them with two queries.

Posted: Thu Jun 06, 2002 10:50 pm
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

Posted: Thu Jun 06, 2002 11:37 pm
by volka
yes, but DO the queries ;)

Posted: Fri Jun 07, 2002 1:42 am
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

Posted: Fri Jun 07, 2002 10:35 am
by cooler75
Thank you guys,
work great! :P