Forum Moderators: coopster

Message Too Old, No Replies

Insert data into two tables

Using php to enter data into two MySQL tables

         

abbeyvet

4:13 pm on Aug 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am working with a preexisting script which uses a form to submit data to a database. It works fine, but I want to submit some of the form data to one table, some to a second table and some to both tables.

At the moment I have this:
=================================================
@$this_strQuery = "INSERT INTO `students`(`student_firstname`,`student_lastname`,`phone`,`mobile`,`email`)VALUES(\"$FirstName\",\"$LastName\",\"$Phone\",\"$Mobile\",\"$email\")";
@$this_host = "localhost";
@$this_user = "username";
@$this_pw = "password";
@$this_db = "dbname";
$this_link = mysql_connect($this_host, $this_user, $this_pw);
if (!$this_link) {
die('Could not connect: ' . mysql_error());
}
$this_db_selected = mysql_select_db($this_db, $this_link);
if (!$this_db_selected) {
die ('Can not use $this_db : ' . mysql_error());
}

//insert new record
$this_result = mysql_query($this_strQuery);
if (!$this_result) {
die('Invalid query: ' . mysql_error());
}
mysql_close($this_link);
=====================================================

But say I want to insert form data also into a table called 'qualifications' - now would I go about that?

coopster

5:57 pm on Aug 22, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Just form up your second query statement and run it before you close your database connection ...
//insert new record 
$this_result = mysql_query($this_strQuery);
if (!$this_result) {
die('Invalid query: ' . mysql_error());
}
// insert record in qualifications table:
$next_query = 'INSERT INTO qualifications ...';
mysql_query($next_query);

mysql_close($this_link);