0

我有这段代码,我正在尝试将选定的状态放在子目录表中。到目前为止,它返回一个空值。我不确定这是否清楚,但我想要的只是:从选择选项中选择一个状态并提交它。我想将选定的州名放入我的表子目录中。

enter <?php
include("connect.php");
$state = $row['states']; //Select name
if (isset($_POST[submit])){
$query = "INSERT INTO subcat (sub_name) VALUES ('$state')";
mysql_query($query) or die(mysql_error());
}
?>
<form action="" method="post" name="form">
<?php
$sql = mysql_query("SELECT * FROM   state");
echo "<select name='states'>
<option value=''>Select a state</option>";
while ($row = mysql_fetch_assoc($sql)) {
echo "<option value='$row[id]'>$row[name]</option>";
}
echo "</select>";
?>
<input type="submit" name="submit" value="Continue" />
</form> here

谢谢

4

2 回答 2

1

更改$state = $row['states']$state = $_POST['states']

<?php

include("connect.php");    
if (isset($_POST[submit]))
{
   $state = $_POST['states']; //Select name
   $query = "INSERT INTO subcat (sub_name) VALUES ('$state')";
   mysql_query($query) or die(mysql_error());
}

?>
<form action="" method="post" name="form">
<?php
$sql = mysql_query("SELECT * FROM   state");
echo "<select name='states'>
<option value=''>Select a state</option>";
while ($row = mysql_fetch_assoc($sql)) {
echo "<option value='$row[id]'>$row[name]</option>"; // if you want to 
//get the name into table, then use like this 
//echo "<option value='$row[name]'>$row[name]</option>";  or  
//echo "<option>$row[name]</option>";
}
echo "</select>";
?>
<input type="submit" name="submit" value="Continue" />
</form>
于 2012-05-19T04:03:06.083 回答
0

试试这个:

enter <?php
include("connect.php");
if (isset($_POST[submit])){
$state = $_POST['states'];
$query = "INSERT INTO subcat (sub_name) VALUES ('".mysql_real_escape_string($state)."')";
mysql_query($query) or die(mysql_error());
}
?>
<form action="" method="post" name="form">
<?php
$sql = mysql_query("SELECT * FROM   state");
echo '<select name="states" id="states">
<option value="">Select a state</option>';
while ($row = mysql_fetch_assoc($sql)) {
echo '<option value="'.$row['name'].'">'.$row['name'].'</option>';
}
echo '</select>';
?>
<input type="submit" name="submit" value="Continue" />
</form> here

不要忘记使用 mysql_real_escape_string 来防止 SQL 注入。我已经替换了 $state = $row['states']; $state = $_POST['states'];

我不知道你从哪里得到 $row...

以上将状态名称插入数据库。

于 2012-05-19T04:09:31.257 回答