2

Method for inserting data into two tables:

  • We do transfer the 2nd table in our Database.
  • I made a separate connection for two tables.

table name is transaction_held_record The columns that I need to fill-in was:

  • transaction_desc
  • product_code
  • product_name
  • selling-price

Here's the sample code for the first table:

include("system/connxn.php");

if (!isset($_POST['eid'])) customError("Failed: Invalid access.", $log);

//Columns for first table.

$eid = $_POST['eid'];
$customer = $_POST['customer'];
$title = $_POST['title'];
$startTime = $_POST['startTime']; 
$endTime = $_POST['endTime'];
$roomID = $_POST['roomID'];
$personnel = $_POST['personnel'];
$service = $_POST['service'];
$status = $_POST['status'];
$remarks = $_POST['remarks'];
$adjustment = $_POST['adjustment'];
$totalprice = $_POST['totalprice'];


$query_str = "UPDATE reservationlist SET `title` = '$title', `start` = '$startTime', `end` = '$endTime', `customer` = '$customer', `resource` = '$roomID', `personnel` = '$personnel', `services` = '$service', `status` = '$status', `remarks` = '$remarks', `totalprice` = '$totalprice', `modflag` = '$adjustment' WHERE id = $eid";
//echo $query_str;
mysql_query($query_str) or customError("Failed: (".mysql_errno().") ".mysql_error(), $log);
//echo mysql_insert_id();
$log->writelog("Update on table [reservationlist] record id: ".$eid);
echo "Saved";

Thank you guys!

Neri
  • 47
  • 1
  • 6

2 Answers2

2

You may create A PHP class with two(2) functions. One function that receives arguments(parameters) for the values to be updated by record id to the first table and another function for the other table. for example:

class dbUpdates
{

function updateReservations($eid, $val1, $val2, $val3, $val4, $val5) {
mysql_query("UPDATE reservationlist SET ... WHERE id = $eid");
//do handling or some additional codes here...
}

function insertTransaction($recid, $val1, $val2, $val3, $val4, $val5) {
mysql_query("INSERT INTO transaction_held_record (reffield, val1field, val2field, val3field, val4field, val5field) VALUES($recid, $val1, $val2, $val3, $val4, $val5)");
//do handling or some additional codes here...
}

}

Include the class file include("dbUpdates.php"); Instantiate the class by

$updateDB = new dbUpdates;

and you may call the functions $updateDB->updateReservations("your arguments here..."); and $updateDB->insertTransaction("your arguments here...");

hope this helps a bit...

Adro
  • 675
  • 13
  • 27
0

Correct me if I'm wrong.You need to insert data into two different tables, in two different databases?

for this you need two connection and an script for each one. check if the first script has succeeded then run the second.

but if you could somehow access both tables with one connection, you could run both your scripts in one action!

UPDATE: I think you need to get the ID of the inserted row in first table and use that for second table, right?

do like this : How to get the ID of INSERTed row in mysql?

UPDATE: you can do multiple update, insert query you want done in a single action.like:

$query = "UPDATE `tbl_something` SET col1 = 'first' WHERE col1 LIKE '%something%';INSERT INTO tbl_another (col1 , col2 , col5) VALUES ('firstValue' , 'secondValue' , 'fifthValue');UPDATE ... ; ";

then run it with mysq_query($query);

if you want you can get the id of effected row in the query, like I mentioned earlier and use it in the next one.

hope this helped

Community
  • 1
  • 1
Developerium
  • 7,155
  • 5
  • 36
  • 56
  • only in one database, but different table and different ID's. – Neri Sep 23 '13 at 09:31
  • I think the link is not the answer to my question, but I appreciate it. All i want to accomplish is the insert it to 2nd table, but different column names. Using the condition. – Neri Sep 23 '13 at 09:42
  • you can run both your update scripts in a single action! just seperate them with semicolonand use condition on them and you're done.like $query_str = 'UPDATE reservationlist set ...;UPDATE reservationlist set ....;'; all done in one action.btw it's not safe to just insert unfiltered data to your database! – Developerium Sep 23 '13 at 10:03
  • do you have an example for the condition? I didn't get your explanation. – Neri Sep 23 '13 at 10:21
  • could you explain more what do you exactly want do and waht do you want to achieve from this? – Developerium Sep 23 '13 at 11:11