如何从mysql中检索数据并将其显示在文本字段中

I am trying to create an html form with 4 columns item , qty , cost and amount...all in a single php file.

The item column is a select tag that collects data from mysql table , which works fine

Now based on that value of item1 the cost has to display in cost text tag.

the following is my code :

</tr>


        <tr>  <td align="left">
               <?php
$user_name = "won";
$password = "won@123";
$database = "won";
$server = "127.0.0.1";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);

if ($db_found) {
$result = mysql_query("SELECT item FROM test ");

echo "<select>";
echo "<option value=''>Select Item</option>";



while($row = mysql_fetch_array($result))

  {

echo "<option value='item1' id='item1' name='item1'>"  .$row['item']. "</option>";
  }
  echo "</select>";

}
mysql_close($db_handle);
?>


              </td> 

              <td align="left"><input type="text" id="qty1" name="qty1"> </td>

              <td align="left"><input type = "text" onclick = "myFunction1()" id="row['cost']"  > </td>
              <td align="left"><input type="text" id="amount1" name="amount1" value=0> </td> 


</tr> 


*************************************************************************
The following is the function :

function myFunction1()
{
<?php
$user_name = "won";
$password = "won@123";
$database = "won";
$server = "127.0.0.1";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);

if ($db_found) {
$result = mysql_query("SELECT cost FROM test where item='$item1' ");

$row = mysql_fetch_array($result);



}
mysql_close($db_handle);
?>

}

You can't fire php events onclick, php is SERVER SIDE, you would need javascript if you want to display them asynchronous

What you want is not much clear.

But as i understand You should use Ajax.

First prepare your item select tag is right in your code.

Then handle change event of select tag and pass that value to ajax call to find cost of that item and store that result in your cost tag on success of ajax.

As you prepared your function myfuncton use this in another php file and call that file from ajax.

code you can write like:

<script src="jquery-1.7.1.min.js"></script>
<script>

    $(document).ready(function(){

        $( "#item1" ).change(function() {

        var item = $("#item1 option:selected").text();

        $.ajax({
                    url: 'getcost.php',
                    type: "POST",
                    data: {
                        'item' : item,                   
                    },
                    success : function(response) { 
                      $('#cost').val(response);
                    },
                    error : function() {                                                
                    },
                    complete : function() {
                    }

                });
         });
    });
    </script>

in your current code give id to your select tag:

<select id="item1">

write your myfunction() code in getcost.php file:

just edit that code. Add before your query.

$item1 = $_POST['item'];

and after

$row = mysql_fetch_array($result);
echo $row['cost'];

write at last

exit;

and in your input tag change id of cost to cost & remove onclick.

this may help you. I just writing here. i had not tested it so some spell or variable related mistake may there.