문제

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!

도움이 되었습니까?

해결책

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...

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top