简体   繁体   中英

Select ID from database as option value is not working in php

I am trying to build a data update program. But select ID from database as option values is not working in php. Also data from db can not be displayed in textarea. I visited two almost similar questions found in Stackoverflow, but unable to solve mine. Please someone guide me. My code is given below. Apart from that text box shows the encoding issue for Indian text.[Screen shot of the present state]

    <?php
include_once 'inclues/connection_string.php';
$id="7";
$Eng_Word="";
$As_Meaning="";
$New_Meaning="";
?>
<!doctype html>

<div class ="container">
    <div class="card p-3 bg-secondary">
        <form action = "update.php" method ="post">
            <div class="form-row">
                <div class="col-sm-2">
                <label for="ID">SlNo</label>
                <select class="form-control" id="sel1">
                <option value="">Sl no</option>
                <?php
                    $stmt = "SELECT id FROM bengali";
                    $stmt= $conn->prepare($stmt);
                    $stmt->execute();
                    while ($row = $stmt->fetch())
                    {
                    echo '<option value=' .$row['id'] . '>' . '</option>';
                    }
                ?>
                </select>
                </div>
            </div>
            <?php
                if (isset($_POST['id']))
                    {
                    $id= $_POST['id'];
                    }
                else
                {
                    echo "Please select ID";
                }
                $stmt = "SELECT * FROM bengali WHERE id=$id";
                $stmt= $conn->prepare($stmt);
                $stmt->execute();
                $row=$stmt->fetch();
                $Eng_Word = $row['Eng_Word'];
                $As_Meaning = $row['As_Meaning'];
                $Bn_Meaning = $row['Bn_Meaning'];
            ?>
            <div class="form-row">
                <div class="col-sm-4">
                    <label for="Eng_Word">English Word</label>
                    <input type="text"  value="<?php echo $Eng_Word;?>" class="form-control">
                </div>
            </div>
          <div class="form-group">
            <label for="As_textarea">Assamese Meaning</label>
            <input type="text"  value="<?php echo $As_Meaning;?>" class="form-control">
            <hr>
            <hr>
            <textarea class="form-control" name="As_Meaning" id="AssameseMeaning" value="<?php echo $As_Meaning;?>" rows="3"></textarea>
          </div>
          <div class="form-group">
            <label for="Newtextarea">New Meaning</label>
            <textarea class="form-control" name="Bn_Meaning" id="BengaliMeaning" value="<?php echo $Bn_Meaning;?>"  rows="3"></textarea>
          </div>
          <div>
          <button type="button" class="btn btn-primary btn-lg btn-right">Save Now!</button>
          </div>
        </form>
    </div>
</div>

After the execute method you should store the result and bind it to a variable

<?php
    $stmt = "SELECT id FROM bengali";
    $stmt= $conn->prepare( $stmt );
    $stmt->execute();

    $stmt->store_result();
    $std->bind_result( $id );

    while ( $row = $stmt->fetch() ) {
        printf( '<option value="%1$d">%1$d', $id );
    }
?>

Replace This code

echo '<option value=' .$row['id'] . '>' . '</option>';

to

echo "<option value='$row[id]'>$row[id]</option>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM