Search code examples
phpmysqlsqlyog

Insert data into two tables in one php script


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!


Solution

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