0

我有一个相对简单的 PHP 页面,名为 editcustomers,有 3 列。我遇到的问题是,当数据库中有记录时,表单将显示,并且字段将填充该信息。

当不存在这样的记录时,甚至不会显示表单,从而消除了插入记录的可能性。

我的页面布局如下:

  • 第 1 列显示了一个包含客户信息的表单,允许对其进行编辑。
  • 第 2 列允许订购产品并显示订购了多少产品
  • 第 3 列显示到目前为止支付的总额和欠款总额。

我目前拥有的页面代码:

<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>

以及所有数据库技巧的代码:

<?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;
?>

如果没有记录,为什么表格甚至不显示?一个错误或我可能理解的东西....但是表单仍然在 HTML 中,并且应该仍然被输出,据我所见。为什么不是这样?

4

1 回答 1

1
while($row = $sth->fetch())

这意味着:为每个返回的行做一些事情。如果没有返回的行,则 while 中包含的任何内容都不会执行,因此它不会打印任何输入!

您应该在 rowCount 中包含一个 if,如果这等于 0 个打印输入,以便用户可以填充它们。

<?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 } ?>
于 2012-11-26T10:54:22.860 回答