PHP - 未以编辑形式

时间:2017-03-22 07:40:43

标签: php html mysql

即使我将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();                                 
}

?>

3 个答案:

答案 0 :(得分:3)

<option value="Choose" <?php echo $row['client_details'] == 'Choose' ? print "selected" : ""; ?> />Choose Division...</option>

您的代码很好,只需添加echo即可使用

答案 1 :(得分:1)

您可以直接关闭您的选择元素;

<select ... />

请注意/。与html规范一样,它会将<option>标记视为不属于<select>

正如@ParthGoswami所说;不要忘记echo

答案 2 :(得分:0)

这是固定的,干净的代码。

很少有关于未来的建议:如果您只从数据库中获取一条记录,请始终尝试遵循最佳做法,例如不要使用while / loop。不要在循环中使用内部,在与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,回声比打印快一点。