在combobox php中显示mysql值

时间:2015-04-02 12:16:56

标签: php mysql combobox

我试图在一个组合框中显示一个名为milk的MySQL数据库表中的一系列数字(表字段名=“标签号”。它在输入表内。但我不确定是什么问题是。这是我的代码:

 <form name="test_form" id="test_form" method="post" action="InputBase.php">

  <tbody>
    <?php

        //set up mysql connection
        mysql_connect("localhost", "root", "") or die(mysql_error());
        //select database
        mysql_select_db("dairy herd system") or die(mysql_error());
        // Retrieve all the data from the "tblstudent" table
        $result = mysql_query("SELECT * FROM cow") or die(mysql_error());
        // store the record of the "tblstudent" table into $row


        while ($row = mysql_fetch_array($result)) {
            // Print out the contents of the entry 
            echo '<tr>'

        //$results = mysql_query("SELECT tag_number FROM milk") or die(mysql_error());
        $query = ("SELECT tag_number FROM milk") or die(mysql_error());
        $results = mysql_query($query);
        while($row=mysql_fetch_array($results)) 
        {                             
        echo '<td>  
        <option value='.$row['tag_number'].'></option>
        </td>';

        }



            echo '<td><input type="text" size = "3" name= "input1"></td>';
            echo '<td><input type="text" size = "3" name= "input2"></td>';
            echo '<td><input type="text" size = "3" name= "input3"></td>';
    echo '<td><input type="text" size = "3" name= "input4"></td>';
            echo '<td><input type="text" id="datepicker"></td>';'</tr>';


        }
        ?>      

  </tbody>

我得到的错误如下: 解析错误:语法错误,意外的'$ query'(T_VARIABLE),期待','或';'在第66行的G:\ xampp \ htdocs \ bootstraptable \ input1.php

这是查询数据库以从牛奶

中选择tag_number的行

how it currently looks

2 个答案:

答案 0 :(得分:0)

你的代码中有一颗小星星。这将导致您的脚本失败。

有了这个,你需要使用PHP htmlspecialchars函数正确地将HTML中显示的任何值转义给用户。这是为了防止Cross-site scripting攻击,这会使您的用户面临各种安全风险和攻击。

while($row=mysql_fetch_array($results)) 
{                             
    echo '<td>  
    <option value='.htmlentities($row['tag_number']).'></option>
    </td>';
}* <---- REMOVE THIS STAR

答案 1 :(得分:0)

你会从中得到一些想法,它只是一个选择框,它将显示数据库中的所有条目(tag_numbers)

  

$hostname_localhost ="localhost";
$database_localhost ="dairy herd system";
$username_localhost ="root";
$password_localhost ="";
$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)
or
trigger_error(mysql_error(),E_USER_ERROR);

mysql_select_db($database_localhost, $localhost);



$query ="SELECT tag_number FROM milk";
 $result = mysql_query($query); 


?>
      <select  name="tag_number" >
      <?php while ($line = mysql_fetch_array($result)){ ?> 

      <option value="<?php echo $line['tag_number'];?>"> <?php echo $line['tag_number'];?>

  </option>   <?php } ?> </select>