我有一个相对简单的PHP页面,名为editcustomers,有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中,并且仍然应该从我能看到的输出。为什么不是这样?
答案 0 :(得分: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 } ?>