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 8905929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:31:10+00:00 2026-06-15T02:31:10+00:00

I have a relatively simple PHP page called editcustomers with 3 columns. The problem

  • 0

I have a relatively simple PHP page called editcustomers with 3 columns. The problem I’m having is that the form will show when there is a record in the database and the fields will be populated with that info.

When no such records exists, the form is not even shown, eliminating the possibility to insert a record.

My page layout is as follows:

  • Column 1 shows a form containing customer information, allowing it to
    be edited.
  • Column 2 allows ordering of products and showing how many products were ordered
  • Column 3 shows the total paid so far, and the total owing.

The code for the page I have at present:

<html>
<?php
$id = $_GET['id'];
require_once('connect.php');
$sth = $dbh->query("SELECT * FROM users where id = '$id';");
$sth->setFetchMode(PDO::FETCH_ASSOC);
$eth = $dbh->query("SELECT * FROM purchases where id = '$id';");
$eth->setFetchMode(PDO::FETCH_ASSOC);
?>

<div id="main">
    <div id="left">
    <form name="custInfo" action ="process.php" method ="post" >
    <input type = "hidden" name ="formType" value="custInfo"/>
    <?php while($row = $sth->fetch()){ ?>
    <p><input type = "hidden" name ="id" value="<?php echo $row["id"] ?>"/>
    <p><input type = "text" name ="firstName" size ="30" value=" <?php echo $row["firstName"]?>"/>
    <p><input type = "text" name ="lastName" size ="30" value="<?php echo $row["lastName"]?>"/>
    <p><input type = "text" name ="country" size ="30" value="<?php echo $row["country"]?>"/>
    <p></p>
    <input type="submit" value="Update" />
    <?php }?>
    </div>

    <div id="mid">
   <form name="custCosts" action ="process.php" method ="post" >
   <input type = "hidden" name ="formType" value="custCosts"/>
    <?php while($row = $eth->fetch()){ ?>
    <p><input type = "hidden" name ="id" value="<?php echo $row["id"] ?>"/>
    <p><input type = "text" name ="amountOwed" size ="30" value=" <?php echo $row["amountOwed"]?>"/>
    <p><input type = "text" name ="numAaa" size ="30" value="<?php echo $row["numAaa"]?>"/>
    <p><input type = "text" name ="numBbb" size ="30" value="<?php echo $row["numBbb"]?>"/>
    <p></p>
    <input type="submit" value="Update" />
    <?php }?>
    </div>

    <div id="right">
    <b>Total Balance</b>
    <p> Money owed: </p>
    <p> aaa total: </p>
    <p> bbb total: </p>
    <p> Total: </p>
    <input type = "text" name ="pay" size ="20" /></p>
    <input type="submit" value="Make Payment" />
    </div>
<?php
$dbh =null;
?>
</body>
</html>

And the code for all the database trickery:

<?php
require_once 'connect.php';
$formType = $_POST['formType'];
$id = $_POST['id'];
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$country = $_POST['country'];
$amountOwed = $_POST['amountOwed '];
$numAaa = $_POST['numAaa'];
$numBbb = $_POST['numBbb'];

if(empty($_POST['id'])) {
$sth = $dbh->prepare("INSERT INTO customers (firstName, lastName, country)
VALUES ('$firstName', '$lastName', '$country')");
$sth->execute();

} elseif(!empty($_POST['id']) && !isset($_POST['stayCost']) && $_POST['formType'] == 'guestInfo'){
$sth = $dbh->prepare("UPDATE customers SET firstName = '$firstName', lastName = '$lastName', country = '$country' WHERE id = '$id'");
$sth->execute();

}elseif(!empty($_POST['id']) && isset($_POST['stayCost']) && $_POST['formType'] == 'guestInfo'){
$sth = $dbh->prepare("INSERT INTO purchases (id, amountOwed, numAaa, numBbb)
VALUES ('$id', '$amountOwed', '$numAaa', '$numBbb'");
$sth->execute();

}elseif(!empty($_POST['id']) && $_POST['formType'] == 'guestCosts'){
$sth = $dbh->prepare("UPDATE purchases SET amountOwed= '$amountOwed', numAaa = '$numAaa', numBbb= '$numBbb' WHERE id = '$id'");
$sth->execute();
}

$dbh =null;
?>

Why does the form not even display if there is no record? An error or something I might understand….but the form is still in the HTML and should still be being output, from what I can see. Why is this not the case?

  • 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-06-15T02:31:12+00:00Added an answer on June 15, 2026 at 2:31 am
    while($row = $sth->fetch())
    

    That means: for each returned row do something. If there’s no returned row, nothing included inside the while will execute, so it isn’t printing any input!

    You should include a if with the rowCount, if this is equal to 0 print inputs so the user can fill them.

    <?php while($row = $sth->fetch()){ ?>
        <p><input type = "hidden" name ="id" value="<?php echo $row["id"] ?>"/>
        <p><input type = "text" name ="firstName" size ="30" value=" <?php echo $row["firstName"]?>"/>
        <p><input type = "text" name ="lastName" size ="30" value="<?php echo $row["lastName"]?>"/>
        <p><input type = "text" name ="country" size ="30" value="<?php echo $row["country"]?>"/>
        <p></p>
        <input type="submit" value="Update" />
    <?php
       }
       if($sth->rowCount()==0){
     ?>
     <p><input type = "hidden" name ="id" value=""/>
        <p><input type = "text" name ="firstName" size ="30" value=""/>
        <p><input type = "text" name ="lastName" size ="30" value=""/>
        <p><input type = "text" name ="country" size ="30" value=""/>
        <p></p>
        <input type="submit" value="Update" />
    <?php } ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a relatively simple question. Will using PHP guarantee that a form is
I have a relatively simple HTML / JavaScript form. Such as this: <form method='POST'
I have a relatively simple object with a method that makes an HTTP request.
I have a relatively simple question that I cannot seem to find the answer
I have this relatively simple regex for usernames // Enforce that username has to
This is a relatively simple question that may just not have a solution. Is
Relatively new to PHP here, but here's where I'm at: I have a simple
I have two relatively simple codes. One main activity and one intent service. Main
I have a relatively simple class which deletes a post: function delete_post($postid, $reason){ //Stuff
I have a relatively simple application which I need to make native Mac OSX

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.