0

我已经从数据库中创建了带有值名称的下拉列表。当我从下拉列表中选择值时,其他数据将出现在基于数据库的其他文本字段中。提交过程做得很好,除非我在列表中检查。下拉列表值未出现在列表中,但其他数据出现了。

这是我的添加形式:

<tr><td width="116">Medicine name</td><td width="221">
<center>:
<select name="name" id="name" >
<option>--- Choose Medicine ---</option>
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("arie");
$sql = mysql_query("SELECT * FROM tabelmedicine ORDER BY name ASC ");
if(mysql_num_rows($sql) != 0){
    while($row = mysql_fetch_assoc($sql)){
        $option_value = $row['priceperunit'] . ',' . $row['stock'];
        echo '<option value="'.$option_value.'">'.$row['name'].'</option>';
    }
}
?>
</select ></center>

这是一个脚本,用于在选择下拉列表时在其他文本字段中显示其他数据库值:

<script>
var select = document.getElementById('name');
var priceperunit = document.getElementById('priceperunit');
var stock = document.getElementById('stock');
select.onchange = function()
{
var priceperunit_stock = select.value.split(',');
priceperunit.value = priceperunit_stock[0];
stock.value = priceperunit_stock[1];
}
</script>

这是我在数据库过程中插入的数据:

<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "arie";
$connect = mysql_connect($host, $user, $pass) or die ('Failed to connect! ');
mysql_select_db($db);

$name=$_POST['name'];
if ($name === "")
{
echo "Please fill all the data";
}

else
{
$query="INSERT INTO `tabelout`(`name`)
VALUES ('$name');";

$result = mysql_query($query) OR die (mysql_error());

echo "You have successfully added new medicine to the database.";
}
?>

这是我的列表页面,其中没有显示名称:

<?php
$con=mysqli_connect("localhost","root","","arie");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM tabelout");
echo "<table border='1'>
<th>name</th>";

while($row = mysqli_fetch_array($result))
{
echo "<td><center>" . $row['name'] . "</center></td>";
}
echo "</table>";
mysqli_close($con);
?>
4

1 回答 1

-1

确保您的数据库表有记录,如果有记录,则更改表结构,在需要的地方添加 tr 标签。

echo "<table border='1'>
<tr><th>name</th></tr>";

while($row = mysqli_fetch_array($result))
{
echo "<tr><td><center>" . $row['name'] . "</center></td></tr>";
}
echo "</table>";
于 2013-09-18T16:20:29.730 回答