如何在表单列表中显示数据库值

时间:2013-09-18 16:06:59

标签: php database mysqli row

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

这是我的添加表单:

<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);
?>

1 个答案:

答案 0 :(得分:-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>";