0

Sorry for my bad english.

How do i insert into a database table and update another database table at the same time? I want to insert a new "income" entry, while i am doing this i want the account table that the income is inserted into, is going to update with the current account balance + the new income account. Are there any more simple methods out there?

This is my php code

    // Add New Entry
    if (isset($_POST['submit']) && $_POST['submit'] == 'saveEntry') {
        // User Validations
        if($_POST['money_income_id'] == '...') {
            $msgBox = alertBox($catReq, "<i class='fa fa-times-circle'></i>", "alert-danger");
        } else if($_POST['money_income_description'] == '') {
            $msgBox = alertBox($money_income_descriptionReq, "<i class='fa fa-times-circle'></i>", "alert-danger");
        } else if($_POST['money_accounts_catogerytitles'] == '...') {
            $msgBox = alertBox('Failed! Income Account Needed', "<i class='fa fa-times-circle'></i>", "alert-danger");
        } else if($_POST['money_income_amount'] == '...') {
            $msgBox = alertBox('Failed! Amount Needed', "<i class='fa fa-times-circle'></i>", "alert-danger");
        } else if($_POST['money_income_time'] == '...') {
            $msgBox = alertBox('Failed! Time Needed', "<i class='fa fa-times-circle'></i>", "alert-danger");
        } else if($_POST['money_payer_title'] == '...') {
            $msgBox = alertBox('Failed! Payer Needed', "<i class='fa fa-times-circle'></i>", "alert-danger");
        } else if($_POST['money_incomecategory_title'] == '...') {
            $msgBox = alertBox('Failed! Income Category Needed', "<i class='fa fa-times-circle'></i>", "alert-danger");
        } else if($_POST['money_paymentmethod_title'] == '...') {
            $msgBox = alertBox('Failed! Payment Method Needed', "<i class='fa fa-times-circle'></i>", "alert-danger");

        } else {
            $money_income_account = $_POST['money_accounts_catogerytitles'];
            $money_income_description = encodeIt($_POST['money_income_description']);
            $money_income_amount = $_POST['money_income_amount'];
            $money_income_time = $_POST['money_income_time'];
            $money_income_payer = $_POST['money_payer_title'];
            $money_income_category = $_POST['money_incomecategory_title'];
            $money_income_paymentmethod = $_POST['money_paymentmethod_title'];

            $stmt = $mysqli->prepare("
                                INSERT INTO
                                    money_income(
                                        money_income_account,
                                        id,
                                        money_income_description,
                                        money_income_amount,
                                        money_income_time,
                                        money_income_payer,
                                        money_income_category,
                                        money_income_paymentmethod,
                                        school_income_date,
                                        school_income_ipaddress
                                    ) VALUES (
                                        ?,
                                        ?,
                                        ?,
                                        ?,
                                        ?,
                                        ?,
                                        ?,
                                        ?,
                                        ?,
                                        ?
                                    )
            ");
            $stmt->bind_param('ssssssssss',
                $money_income_account,
                $pw_userId,
                $money_income_description,
                $money_income_amount,
                $money_income_time,
                $money_income_payer,
                $money_income_category,
                $money_income_paymentmethod,
                $todayDt,
                $actIp
            );





            $stmt = $mysqli->prepare("UPDATE
                                        money_accounts
                                    SET
                                        money_accounts_catogeryamount = ?,
                                        money_accounts_catogerylastupdated = ?
                                    WHERE
                                        money_accounts_catogeryid = ?"
            );
            $stmt->bind_param('sss',
                                    $money_income_amount,
                                    $todayDt,
                                    $money_income_account
            );







            $stmt->execute();
            $stmt->close();
            header('index.php?page=income');

            // Add Recent Activity
            $activityType = '8';
            $activityTitle = $newEntryNavLink.' "'.decodeit($money_income_description).' '.$createdText;
            updateActivity($pw_userId,$activityType,$activityTitle);
            $msgBox = alertBox('New income entry '."<b>\"".decodeit($money_income_description)."\"</b> ".$newcatMsg2, "<i class='fa fa-check-square'></i>", "alert-success");


            // Clear the Form of values
            $_POST['money_income_description'] = $_POST['money_income_amount'] = $_POST['money_income_time'] = '';
        }
    }

?>

This is my html code

<div class="modal fade slide-up disable-scroll" id="addNewAppModal" tabindex="-1" role="dialog" aria-labelledby="addNewAppModal" aria-hidden="true">
          <div class="modal-dialog modal-lg">
          <div class="modal-content-wrapper">
            <div class="modal-content">
              <div class="modal-header clearfix ">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="pg-close fs-14"></i>
                </button>
                <h4 class="p-b-5"><span class="semi-bold">New</span> Income</h4>
              </div>
              <div class="modal-body">
                <p class="small-text text-center">Create a new income using this form, make sure you fill them all</p>
                <form action="" method="post">

                  <div class="row">
                    <div class="col-md-4">
                        <div class="form-group">
                            <label for="money_income_description">Description</label>
                            <input type="text" class="form-control" name="money_income_description" required="required" value="<?php echo isset($_POST['money_income_description']) ? $_POST['money_income_description'] : ''; ?>" />
                            <span class="help-block"><?php echo $money_income_descriptionHelp; ?></span>
                        </div>
                        </div>
                        <div class="col-md-4">
                        <div class="form-group">
                            <label for="money_income_amount">Amount</label>
                            <input type="number" class="form-control" name="money_income_amount" required="required" value="<?php echo isset($_POST['money_income_amount']) ? $_POST['money_income_amount'] : ''; ?>" />
                            <span class="help-block"><?php echo $money_income_descriptionHelp; ?></span>
                        </div>
                        </div>
                        <div class="col-md-4">
                        <div class="form-group">
                            <label for="money_income_time">Time</label>
                            <input id="date" type="text" name="money_income_time" required="required" class="form-control input-sm" value="<?php echo isset($_POST['money_income_time']) ? $_POST['money_income_time'] : ''; ?>" id="startDate">
                            <span class="help-block"><?php echo $money_income_descriptionHelp; ?></span>
                        </div>
                    </div>
                  </div>
                  <div class="row">
                    <div class="col-sm-2">
                      <div class="form-group">
                            <label for="money_accounts_catogerytitles">Select Account</label>
                            <select class="cs-select cs-skin-slide" required="required" data-init-plugin="cs-select" name="money_accounts_catogerytitles" id="money_accounts_catogerytitles">
                                <option value="...">Select</option>
                                <?php
                                    $sqlStmt = "SELECT
                                                    money_accounts_catogeryid,
                                                    money_accounts_catogerytitles,
                                                    UNIX_TIMESTAMP(money_accounts_catogerydate) AS income3Date
                                                FROM
                                                    money_accounts
                                                WHERE
                                                    id = ".$pw_userId."
                                                ORDER BY income3Date DESC";
                                    $results = mysqli_query($mysqli, $sqlStmt) or die('-2'.mysqli_error());
                                ?>
                                <?php while ($row = mysqli_fetch_assoc($results)) { ?>
                                    <option value="<?php echo $row['money_accounts_catogerytitles']; ?>"><?php echo decodeit($row['money_accounts_catogerytitles']); ?></option>
                                <?php } ?>
                            </select>
                        </div>
                    </div>
  • 2
    Since you are using the same variable (`$stmt`), the second `prepare` is replacing the first. Try using `$stmt1` and `$stmt2`. IN CASE you need to run both queries or none, take a look at [transactions](http://php.net/manual/en/mysqli.begin-transaction.php)... _or triggers_ \/ – FirstOne Mar 07 '16 at 16:21
  • Please have a look at mysql triggers: http://stackoverflow.com/questions/16892070/mysql-after-insert-trigger-which-updates-another-tables-column – mario.van.zadel Mar 07 '16 at 16:23
  • 1
    Yes, this is the textbook scenario for tranasctions. I would also consider the use of triggers to make any updates automatic. – Shadow Mar 07 '16 at 16:25
  • I know [that link](http://php.net/manual/en/mysqli.begin-transaction.php) is just `begin_transaction`, but in the `see also` there is more. You could also take a look at [How to start and end transaction in mysqli?](http://stackoverflow.com/q/12091971/4577762) – FirstOne Mar 07 '16 at 16:28

0 Answers0