-1

I just learned PHP month ago, so now I trapped in a little few days but can't solve it. I have a database with 1 table, 2 column. I want to bring column one value to drop-down in PHP (and I did it!), but I also want to show value of colum two same with drop-down value selected.

Example: If drop-down list value current is 1011, the textbox auto get value "News". But my code never return the textbox's value for the firt value of drop-down !!! And after return value in textbox, it always return to first value in drop-down, not hold back to values I want to choose. Someone can review and help me for this confuse, please? Thank you!

<?php
    $con=mysqli_connect("localhost","root","","my_database_exam"); //connect to my database 
    mysqli_set_charset($con,"utf8"); //set UTF8 for my database

    if(isset($_POST["id_type"])) { //check for the action of select value
        $id_theloai=$_POST["get_id_type"];      
        $ten="SELECT * FROM theloai WHERE ID_TheLoai='".$get_id_type."'";
        $kqten=mysqli_query($con,$ten);
        while($rowten=mysqli_fetch_array($kqten)) {
            $tenresult=$rowten[1];
        }
    }
?>
<form method="POST" action="park.php">
    <label>TYPE OF NEWS</label>
    <select onchange="this.form.submit()" name="id_type">
        <?php
            $sl="SELECT * FROM theloai";
            $result=mysqli_query($con,$sl);
            while ($row=mysqli_fetch_array($result)) {
                echo '<option value="'.$row[0].'">'.$row[0].'</option>';
            }
        ?>
    </select>

    <br>
    <input type="text" name="" value="<?php if(isset($_POST["id_type"])) echo $tenresult; ?>">

</form>


<?php mysqli_close($con);?>
Anvh
  • 11
  • 3
  • **WARNING**: When using `mysqli` you should be using [parameterized queries](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and [`bind_param`](http://php.net/manual/en/mysqli-stmt.bind-param.php) to add user data to your query. **DO NOT** use string interpolation or concatenation to accomplish this because you have created a severe [SQL injection bug](http://bobby-tables.com/). **NEVER** put `$_POST`, `$_GET` or **any** user data directly into a query, it can be very harmful if someone seeks to exploit your mistake. – tadman Mar 16 '17 at 02:08
  • Where's `$get_id_type` defined? Why is `$id_theloai` defined but not used? – Pang Mar 16 '17 at 02:12
  • sounds like undefined index/variables to me – Funk Forty Niner Mar 16 '17 at 02:19

1 Answers1

0

(sorry for bad english)

if(isset($_POST["id_type"])) { //check for the action of select value
        $id_theloai=$_POST["get_id_type"];      
        $ten="SELECT * FROM theloai WHERE ID_TheLoai='".$get_id_type."'";
        $kqten=mysqli_query($con,$ten);
        while($rowten=mysqli_fetch_array($kqten)) {
            $tenresult=$rowten[1];
        }

you asign your post value to $id_theloai=$_POST["get_id_type"]; but you use $get_id_type in your query... it should be:

$get_id_type=$_POST["get_id_type"];
Carlos Gonzalez
  • 389
  • 1
  • 11
  • Yes, it's my fault. But after fix all this, it stil not work properly. It's still not hold the value in drop-down when I select and not show the first value in textbox, too! – Anvh Mar 16 '17 at 02:27
  • (sorry for bad english) You checks `isset($_POST["id_type"])` and store `$_POST["get_id_type"]`, you should use same key you set as NAME in select: ` – Carlos Gonzalez Mar 16 '17 at 11:41