MechaMetalHead
MechaMetalHead

Reputation: 51

PHP - value not displayed in edit form

Even though I put the the php code in the the form to retrieve the value from the database, it didn't display the value in the dropdown list and the textbox. The code works fine and can update the value but it didn't display the value after I refreshed the page. It looks fine but can't seem to find the mistake I made.

<?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();                                 
}

?>

Upvotes: 0

Views: 113

Answers (3)

Mubashar Iqbal
Mubashar Iqbal

Reputation: 385

Here is fixed, clean code.

Few suggestions for future: Always try to follow best practices like don't use while/loop if you are getting only one record from database. Don't use inside while loop, always keep SQL injection in mind when interacting with DB and how to tackle it, follow Code Formatting Pattern etc etc.

<?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();                                 
}
?>

And use echo because according to w3schools, echo is marginally faster than print.

Upvotes: 0

giorgio
giorgio

Reputation: 10202

You directly close your select element;

<select ... />

Note the /. As in html specs it will consider the <option> tags as not being part of the <select>.

And as @ParthGoswami said; don't forget to echo the values

Upvotes: 0

Parth Goswami
Parth Goswami

Reputation: 348

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

your code is fine just add echo before will work

Upvotes: 2

Related Questions