Steve Fitzgerald wrote:
In my example then it would be:
if ( mysql_query("insert into parent (company) values (CompanyName,WebSite)", $conn))
{
$CompanyID = mysql_insert_id($conn);
mysql_query("insert into child (ContactID) values ($ContactID)", $conn);
Is this correct? Also, when referring to $conn do you mean $connection or is it something different?
Steve Fitzgerald also wrote:
I'm trying to create an INSERT statement that will change a field in one table based upon the id defined in another table.
Here is what I tried:
$sql1 = "INSERT INTO $table_name1 (FirstName,LastName,WorkPhone,HomePhone,EmailName,Birthday) VALUES ('[$FirstName]','[$LastName]','[$WorkPhone]','[$HomePhone]','[$EmailName]','[Birthday]')";
$sql2 = "INSERT INTO $table_name2 (CompanyName,WebSite) VALUES ('[$CompanyName]','[$WebSite]') WHERE contacts ON company.CompanyID = contacts.CompanyID WHERE ContactID='$ContactID'";
The goal is based upon a unique field call ContactID I want to be able to update the CompanyName associated with the ContactID.
For example: ContactID=1 might have an associated CompanyName= Smith, Inc. What I want to do is UPDATE/INSERT a new CompanyName and have that reflected by changing the CompanyID associated with the ContactID.
And I will now attempt to reply:
Okay, here's the best I can figure: You have two tables, $table_name1 (which holds people's information and has an auto-increment column called ContactID, or something like that) and $table_name2 (which holds company information, and has an auto-increment column called CompanyID as well as another column called ContactID, which holds an ID from $table_name1). When you insert a new person into your database, you also want to insert the company associated with it. Since there are currently no companies associated with this person you just now added, you only need to worry about getting their ID into the ContactID field of $table_name2.
Assuming that I've got all that right, here's what you can do:
if (mysql_query($sql1, $connection)){
$ContactID = mysql_insert_id($connection);
$result = mysql_query("INSERT INTO $table_name2 (CompanyName,WebSite,ContactID) VALUES ('$CompanyName','$WebSite','$ContactID')", $connection);
}
And, if you need CompanyID:
if ($result){
$CompanyID = mysql_insert_id($connection);
}
If you're interested, here's the manual page on mysql_insert_id():
http://www.php.net/manual/en/function.mysql-insert-id.php
Hope that helps.
-Reha