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中,應該仍然是輸出,從我可以看到。爲什麼不是這種情況?
啊我覺得這麼愚蠢。你能否在你的答案中給出一個例子,這是我第一次嘗試編程,所以對我來說一切都是新的。 –
我編輯了我的答案,就像'if($ sth-> rowCount()== 0)'應該在每個while($ row = $ sth-> fetch())之前加上' – Naryl