//This is my php to view dropdown box <?php
include('connect.php');
//for retriving data from DB and show it in drop down box
$query="SELECT cname FROM country";
$result = mysqli_query ($con, "$query");
echo "<select name=country value=''>";
while($r=mysqli_fetch_row($result))
{
echo "<option value='$r[0]'> $r[0] </option>";
}
echo "</select>";
?>
但是当我存储在数据库中时,我的country_id总是 0。
您没有从 SQL 查询获取国家/地区 ID
改变
$query="SELECT cname FROM country";
自
$query="SELECT cid, cname FROM country"; // Update cid with your country id field
因此,$r[0]
正在获取空白值。
使用此代码作为参考
//This is my php to view dropdown box
<?php
include('connect.php');
//for retriving data from DB and show it in drop down box
$query="SELECT countryid,cname FROM country";
$result = mysqli_query ($con, "$query");
echo "<select name='country'>";
while($r=mysqli_fetch_row($result))
{
echo "<option value='$r[countryid]'> $r[cname] </option>";
}
echo "</select>";
?>
我将使用学生的答案将查询更改为
$query="SELECT cid, cname FROM country"; // Update cid with your country id field
然后更改此
echo "<select name=country value=''>";
自
echo "<select name=country>"; //remove value=''
编辑:
请检查您的列类型可能是"INT",当您尝试存储它存储 0 的文本时。