gpt4 book ai didi

php - 表单中的选项输入未通过

转载 作者:行者123 更新时间:2023-11-29 22:02:44 24 4
gpt4 key购买 nike

我正在尝试使用表单来填写我的数据库。我使用数据库中的表来填写表单中的选项并且它可以工作,但是该值没有被传递,我运行了我的代码,找不到拼写错误,是否有我没有看到的错误?

<?php
require("db.php");
?>
<html>
<head>
<title>Insert a Record in MySQL Database</title>
</head>

<body>
<?php
if(isset($_POST['insert']))
{
$user_added = $_POST['user_added'];
$age_added = $_POST['age_added'];

$insert_query = "INSERT INTO userList ";
$insert_query .= "(user, Age) ";
$insert_query .= "VALUES ";
$insert_query .= "('$user_added', '$age_added')";

$retval = mysqli_query($connection, $insert_query);

if(!$retval )
{
die ("The error is: " . mysqli_error($connection));
}
else
{
echo "Database Updated " . $age_added . " " . $user_added;
}
}
else
{
?>
<form method="post" action="<?php $_PHP_SELF ?>">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="100">User to Add</td>
<td><input name="user_added" type="text" id="user_added"></td>
</tr>
<tr>
<td width="100">Age</td>
<td><select name="age_added" id="age_added">
<?php
while ($selection = mysqli_fetch_assoc($age_query))
{
echo "<option value=\"{$selection['id']}\">{$selection['age']}</option>\n";
}
?>
</select>
</td>
</tr>
<tr>
<td width="100"> </td>
<td>
<input name="insert" type="submit" id="insert" value="Insert">
</td>
</tr>
</table>
</form>
<?php
}
?>
</body>
</html>
<?php
mysqli_close($connection);
?>

如果我在用户中输入Test并在年龄中选择一个数字,则输出不会发送年龄,并且显示没有年龄的Database Updated Test 。我错过了什么?

----- 添加信息 1 -----

这是根据请求创建 $age_query 的查询

<?php
//Perfom DB query
$query = "SELECT * FROM PermittedAges ";
$query .= "ORDER BY Age asc";
$age_query = mysqli_query($connection, $query);

//Test for query error
if (!$age_query) {
die ("Database query failed.");
}
?>

但我不确定这怎么可能是问题,因为这只填充下拉字段中的选项并且它有效

-----添加信息2-----

这是var_dump($_POST)输出

array(3) { ["user_added"]=> string(4) "Test" ["Age_added"]=> string(0) "" ["insert"]=> string(6) "Insert" }

最佳答案

试试这个

<?php
while ($selection = mysqli_fetch_assoc($age_query))
{
?>
<option value="<? echo $selection['id']; ?>"> <? echo $selection['age']; ?> </option>
<?
}
?>

关于php - 表单中的选项输入未通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32464669/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com