I am a newbie on PHP and mySQL. I am trying to create a simple balance sheet using both.
table will show me (account.php)

so, i have created mysql table for this. It has

accid, acctype, accnote, accinc, accexp, accudate.

and to add the transaction i have created another page.(accadd.php)

TYPE : --> DROPDOWN MENU that is showing EXPENSE and INCOME

now, i like to use accadd.php page to add new transactions and whenever i choose expense from drop down menu i like the amount will be inserted in expense column, and when i choose income, then amount will be inserted into income column.

please note that there is no amount column in the mysql table, only accinc (for income amount) and accexp (for expense amount).

Please help me for the query i want to create.here is my code below in accadd.php and it is obviously inserting only into expense column

$acc_udate = date ("Y-m-d");
$at = array ('E' => 'Expense', 'I' => 'Income');

if (isset($_POST['submitted'])) {

    require_once ('mysqli_connect.php'); //Connect to the db

    $errors = array(); // Initialize an error array.


    // Check for the notes:

    if (empty($_POST['acc_notes'])) {

        $errors[] = 'You forgot to enter the note.';

    } else {

        $an = mysqli_real_escape_string($dbc, trim($_POST['acc_notes']));


    // Check for the amount:

    if (empty($_POST['acc_amnt'])) {

        $errors[] = 'You forgot to enter the amount.';

    } else {

        $am = mysqli_real_escape_string($dbc, trim($_POST['acc_amnt']));


    // Check for the type:

    if (empty($_POST['acc_type'])) {

        $errors[] = 'You forgot to select the type.';

    } else {

        $at = mysqli_real_escape_string($dbc, trim($_POST['acc_type']));    }


    if (empty($errors)) { // If everything's OK.


        // insert the transaction in the database...

    require_once ('mysqli_connect.php'); //Connect to the db


        // Make the query:

        $q = "INSERT INTO hesap (acc_notes, acc_exp, acc_type, acc_udate) VALUES ('$an', '$am','$at', '$acc_udate')";
        $r = @mysqli_query ($dbc, $q); // Run the query.

        if ($r) { // If it ran OK.


            // Print a message:
            header("Location: accAdd.php");

            echo '<h1>Thank you! It has been inserted!</h1>



        } else { // If it did not run OK.


            // Public message:

            echo '<h1>System Error</h1>

            <p class="error">It was not inserted due to a system error. We apologize for any inconvenience.</p>'; 


            // Debugging message:

            echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $q . '</p>';


        } // End of if ($r) IF.


        mysqli_close($dbc); // Close the database connection.

        // Include the footer and quit the script:

        include ('includes/footer.html'); 



    } else { // Report the errors.


        echo '<h1>Error!</h1>

        <p class="error">The following error(s) occurred:<br />';

        foreach ($errors as $msg) { // Print each error.

            echo " - $msg<br />\n";


        echo '</p><p>Please try again.</p><p><br /></p>';


    } // End of if (empty($errors)) IF.

mysqli_close($dbc); //Close the database connection

} // End of the main Submit conditional.


or of you know any simple script for basic accounting please let me know.. thanks in advance and apologies for my horrible english. :)

6 Years
Discussion Span
Last Post by NettSite
$accinc = $at == "I" ? $am : 0; // If type is Income, the amount, else 0.
$accexp = $at == "E" ? $am : 0; // If type is Expense, the amount, else 0.

// Add accinc into the query as well as accexp.
$q = "INSERT INTO hesap (acc_notes, acc_inc, acc_exp, acc_type, acc_udate) VALUES ('$an', $accinc, $accexp,'$at', '$acc_udate')";

Edited by NettSite: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.