Php 将下拉菜单中的选项存储到数据库中
我的html代码中有以下表单字段:Php 将下拉菜单中的选项存储到数据库中,php,html,forms,Php,Html,Forms,我的html代码中有以下表单字段: <select class="secret_question"> <option value ="1">The name of the city where you were born</option> <option value ="2">The name of your first pet</option>
<select class="secret_question">
<option value ="1">The name of the city where you were born</option>
<option value ="2">The name of your first pet</option>
<option value ="3">What is your mother's maiden name</option>
</select>
我对选择列表尝试了相同的技术,但它给了我
“未定义索引”
错误如果表单元素没有“命名”,您将得到一个未定义索引
错误
您需要按如下方式命名您的
:
<select class="secret_question" name="secret_question">
$firstname = $_REQUEST['firstname'];
$lastname = $_REQUEST['lastname'];
$secret_question = $_REQUEST['secret_question'];
$stmt = $dbh->prepare("INSERT INTO users (firstname,lastname,secret_question)
VALUES ('" . $firstname . "', '" . $lastname . "', '" . $secret_question . "')");
命名数据库列时secret\u-question
使用以下约定更安全:
“$firstname.”
而不是“$firstname”
但是,您可能希望使用输入字段而不是
,因为人们可能自己键入答案,这取决于您的设置方式。将您的
命名为
,然后使用$secret\u question=$\u REQUEST['secret\u question'代码>这里似乎就是这样。然后用它来输入数据库。你没有发布你的完整表单,所以很难100%地说出来,另一个可能的原因是使用值(“$firstname.”、“$lastname.”)
这也是一种更安全的方法。我现在觉得自己很愚蠢。非常感谢。不客气,很高兴我能帮上忙。我在下面贴了一些可能有用的东西,干杯。
$firstname = $_REQUEST['firstname'];
$lastname = $_REQUEST['lastname'];
$secret_question = $_REQUEST['secret_question'];
$stmt = $dbh->prepare("INSERT INTO users (firstname,lastname,secret_question)
VALUES ('" . $firstname . "', '" . $lastname . "', '" . $secret_question . "')");