form not showing for empty records

Posted by Chris Hodges on Stack Overflow See other posts from Stack Overflow or by Chris Hodges
Published on 2012-11-26T10:51:41Z Indexed on 2012/11/26 11:03 UTC
Read the original article Hit count: 151

Filed under:
|
|
|
|

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?

© Stack Overflow or respective owner

Related posts about php

Related posts about html