[英]PHP - value not displayed in edit form
即使我將php代碼放在表單中以從數據庫中檢索值,它也沒有在下拉列表和文本框中顯示該值。 該代碼可以正常工作,並且可以更新值,但是刷新頁面后它沒有顯示該值。 看起來不錯,但似乎找不到我犯的錯誤。
<?php
require("config.php");
$id = $_GET['id'];
$sql = "SELECT * FROM contracts WHERE id= '$id'";
$result = $con->query($sql);
while ($row = $result->fetch_assoc())
{
$client_type = $row['client_type'];
?>
<html>
<head>
<title> Submit a Contract </title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form method="post" action="" enctype="multipart/form-data">
ID: <?php echo $id; ?><br>
<input type="hidden" name="id" value="<?php echo $id; ?>" />
Division:
<select name="client_details" />
<option value="Choose" <?php $row['client_details'] == 'Choose' ? print "selected" : ""; ?> />Choose Division...</option>
<option value="Distribution" <?php $row['client_details'] == 'Distribution' ? print "selected" : ""; ?> />Distribution</option>
<option value="Transmission" <?php $row['client_details'] == 'Transmission' ? print "selected" : ""; ?> />Transmission</option>
<option value="Generation" <?php $row['client_details'] == 'Generation' ? print "selected" : ""; ?> />Generation</option>
<option value="Procument" <?php $row['client_details'] == 'Procument' ? print "selected" : ""; ?> />Procument</option>
<option value="Other" <?php $row['client_details'] == 'Other' ? print "selected" : ""; ?> />Others</option>
</select>
<br><br>
Others:
<input type="text" name="client_details" value="<?php $row['client_details']; ?>">
<input type="submit" name="submit" value="Submit"/>
</form>
</body>
</html>
<?php
}
if(isset($_POST['submit']))
{
$client_details = isset($_POST ['client_details']) ? $_POST['client_details'] :null;
if($client_details == 'Other'){
$client_details = isset($_POST ['client_details']) ? $_POST['client_details'] :null;
}
$query = "UPDATE contracts set `client_details` = '$client_details' WHERE `id` = '$id'";
if ($con->query($query) === TRUE)
{
echo "<br><br> Updated successfully <br>";
echo $query;
}
else {
echo "Error: " . $query . "<br>" . $con->error;
}
$con->close();
}
?>
<option value="Choose" <?php echo $row['client_details'] == 'Choose' ? print "selected" : ""; ?> />Choose Division...</option>
您的代碼很好,只需添加echo即可
您直接關閉選擇元素;
<select ... />
注意/
。 與html規范中一樣,它將認為<option>
標記不屬於<select>
。
正如@ParthGoswami所說; 別忘了echo
顯值
這是固定的,干凈的代碼。
未來的一些建議:如果您從數據庫中僅獲得一條記錄,請始終嘗試遵循最佳實踐,例如不要使用while / loop。 不要在while循環內使用,在與DB交互以及如何解決它,遵循代碼格式化模式等時始終牢記SQL注入。
<?php
require("config.php");
$id = filter_input(INPUT_GET, 'id');
?>
<html>
<head>
<title> Submit a Contract </title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form method="post" action="" enctype="multipart/form-data">
ID: <?php echo $id; ?><br>
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<?php
$sql = "SELECT * FROM contracts WHERE id = $id";
$result = $con->query($sql);
$row = $result->fetch_assoc();
$client_type = $row['client_type'];
?>
Division:
<select name="client_details">
<option value="Choose" <?php $row['client_details'] == 'Choose' ? echo "selected" : ""; ?> />Choose Division...</option>
<option value="Distribution" <?php $row['client_details'] == 'Distribution' ? echo "selected" : ""; ?> />Distribution</option>
<option value="Transmission" <?php $row['client_details'] == 'Transmission' ? echo "selected" : ""; ?> />Transmission</option>
<option value="Generation" <?php $row['client_details'] == 'Generation' ? echo "selected" : ""; ?> />Generation</option>
<option value="Procument" <?php $row['client_details'] == 'Procument' ? echo "selected" : ""; ?> />Procument</option>
<option value="Other" <?php $row['client_details'] == 'Other' ? echo "selected" : ""; ?> />Others</option>
</select>
<br><br>
Others:<input type="text" name="client_details" value="<?php $row['client_details']; ?>">
<input type="submit" name="submit" value="Submit"/>
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])) {
$client_details = isset($_POST ['client_details']) ? $_POST['client_details'] :null;
if($client_details == 'Other') {
$client_details = isset($_POST ['client_details']) ? $_POST['client_details'] :null;
}
$query = "UPDATE contracts set `client_details` = '$client_details' WHERE `id` = '$id'";
if ($con->query($query) === TRUE) {
echo "<br><br> Updated successfully <br>";
echo $query;
} else {
echo "Error: " . $query . "<br>" . $con->error;
}
$con->close();
}
?>
並使用echo,因為據w3schools所述,echo比打印要快一些。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.