Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6617049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:39:26+00:00 2026-05-25T20:39:26+00:00

I’m continuing to hack away at my newbie php/mySQL ‘Invoicer’ app. I now have

  • 0

I’m continuing to hack away at my newbie php/mySQL ‘Invoicer’ app.

I now have a form page in which I want to run one of two queries – either an INSERT or an UPDATE, depending on whether an ID is present. When present,
the ID is used to retrieve the record and pre-populate the form accordingly, which I have working. My problem now is that my conditional bits are
obviously not right because in either case when submitting the form the INSERT query is run, can’t get the UPDATE to run, and I’ve exhausted my
understanding (and guess-ology).

I’d love to know why this ain’t working, even if it’s not the best approach, and I’m definitely open to suggestions to move the queries to a process.php,
etc. I’m also wondering if I should use ‘if(isset($_GET[‘ID’])’ to simply include one block or the other.

Many thanks in advance for any help or suggestions. (p.s. my intention is to overhaul for best practices/security once I’ve got the broad strokes wired up)

cheers, s

<?php

    // CASE I: 'EDIT RECORD':
    //  If there's an ID ...
    if  (isset($_GET['ID']) && is_numeric($_GET['ID'])) {
        $id = $_GET['ID'];
        echo "<p class=\"status\"><strong>ID IS SET ... ergo we're editing/UPDATING an existing record</strong></p>";

        //  ... retrieve the record ....
        $query = sprintf("SELECT * FROM Invoices WHERE ID = %s", $id);
        $result = mysql_query($query) or die(mysql_error());
        $row = mysql_fetch_array($result);

        //  ... assign variables to pre-populate the form
        $id         = $row['ID'];
        $invNumber  = $row['invNumber'];
        $invDate        = $row['invDate'];
        // [ snip: more variables > field data ]

        // on submit: get the form values ...
        // no worky: if (isset($_GET['ID']) && isset($_POST['submit'])) {
        if  (isset($_POST['submit']))   {
            $invNumber  = $_POST['invoice-number'];
            $invDate        = $_POST['invoice-date'];
            $projNumber = $_POST['project-number'];
            // [ snip: more variables > field data ]


            // ... and UPDATE the db:
            $qUpdate = "UPDATE Invoices SET invNumber='$invNumber', invDate='$invDate', projNumber='$projNumber', client='$client', task='$task', issueDate='$issueDate', subTotal='$subTotal', tax='$tax', invTotal='$invTotal', datePaid1='$datePaid1', datePaid2='$datePaid2', comments='$comments' WHERE ID='3'";
            $result = mysql_query($qUpdate) or die(mysql_error());
            if($result) {
                echo "<p class=\"status\"><strong>SUCCESS: RECORD UPDATED!</strong></p>";
            }
            else die("DAMMIT JIM I'M A DOCTOR NOT A DB ADMIN!" . mysql_error());

        } // CLOSE '(isset($_POST['submit']))
    } // END CASE I: ID present


    // CASE II: 'NEW RECORD'; query = INSERT
    elseif (empty($_GET['ID'])) {
        echo "<p class=\"status\"><strong>No ID ... ergo we're INSERTING a new record:</strong></p>";

        // on submit: get the form values ...
        if (isset($_POST['submit']))    {
            $invNumber  = $_POST['invoice-number'];
            $invDate        = $_POST['invoice-date'];
            $projNumber = $_POST['project-number'];
            // [ snip: more variables > field data ]

            $qInsert =  "INSERT INTO Invoices (invNumber,invDate,projNumber,client,task,issueDate,subTotal,tax,invTotal,datePaid1,datePaid2,comments)
                VALUES('$invNumber','$invDate','$projNumber','$client','$task','$issueDate','$subTotal','$tax','$invTotal','$datePaid1','$datePaid2','$comments')";
            $result = mysql_query($qInsert) or die(mysql_error());
            if($result) {
                echo "<p class=\"status\"><strong>SUCCESS: NEW RECORD INSERTED!</strong></p>";
            }
            else die("DAMMIT JIM I'M A DOCTOR NOT A DB ADMIN!" . mysql_error());
        } // CLOSE '(isset($_POST['submit']))
    } // END CASE II: No ID present

?>

and:

<form id="invoiceData" method="post" action="/html/form.php">       
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:39:27+00:00Added an answer on May 25, 2026 at 8:39 pm

    When you submit the form, you need to include the ID again, otherwise it is silently dropped off since you are posting to the hard-coded value /html/form.php (with ID removed). This will cause the empty($_GET['ID']) part to match and run, causing the INSERT. You can simply include the ID value back into the action of every form post like this:

    <form
      id="invoiceData"
      method="post"
      action="/html/form.php?ID=<?php echo $_GET['ID']; ?>"
    >
    

    This should work in both the cases of the UPDATE and the INSERT, because if there was no ID to begin with, this will render as /html/form.php?ID=, which will match the case of ID being empty, I believe. You may want to test this logic out for sure.

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a text area in my form which accepts all possible characters from
I have a jquery bug and I've been looking for hours now, I can't
I have an autohotkey script which looks up a word in a bilingual dictionary
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.