我有以下基本HTML表单:
<form action="" method="post">
Name: <input type="text" name="name" /><br><br>
College: <select name = "colleges">
<option> ---Select College---</option>
<option value="option1">DIT</option>
<option value="option2">University College Dublin</option>
</select><br><br>
Email: <input type="text" name="email" /><br><br>
Password: <input type="text" name="password" /><br><br>
Location: <input type="text" name="location" /><br><br>
<button type="submit" name="submit" >Submit</button>
</form>
下面是我很新的PHP部分:
if(isset($_POST["submit"])) {
$name = $_POST["name"];
$college_name = $_POST["college"];
$email = $_POST["email"];
$password = $_POST["password"];
$location = $_POST["location"];
}
$sql = "INSERT INTO user(name, college, email, password, location) VALUES ($name, $college_name, $email, $password, $location)";
?>
我的数据库的大学类型为int,因此数据库中的DIT为1。谁能告诉我该怎么做,以便它发送1作为college_name而不是用户看到的实际名称?
答案 0 :(得分:2)
更改此
College: <select name = "colleges">
<option> ---Select College---</option>
<option value="option1">DIT</option>
<option value="option2">University College Dublin</option>
</select><br><br>
对此
College: <select name = "colleges">
<option> ---Select College---</option>
<option value="1">DIT</option>
<option value="2">University College Dublin</option>
</select><br><br>
还有这个
if(isset($_POST["submit"])) {
$name = $_POST["name"];
$college_name = $_POST["college"];
$email = $_POST["email"];
$password = $_POST["password"];
$location = $_POST["location"];
}
对此
if(isset($_POST["submit"])) {
$name = $_POST["name"];
$college_name = (int) $_POST["colleges"];
$email = $_POST["email"];
$password = $_POST["password"];
$location = $_POST["location"];
}
答案 1 :(得分:1)
只需使用
<option value='1' > DIT </option>
而不是将option1添加为值
答案 2 :(得分:1)
在您的选项中输入数值:
College: <select name="colleges">
<option value="1">DIT</option>
<option value="2">University College Dublin</option>
</select>
然后更正您的php以使其与$ _POST数组中的select名称匹配:
$college_name = (int) $_POST["colleges"];
答案 3 :(得分:1)
为什么不使用option value
?
$_POST["college"]
返回option1
我认为正确的方法是将值定义为1
:
以下内容:
<option value="1">DIT</option>
将返回:1
您还可以使用If else语句
if ($_POST["college"] == 'option1')
{
$college = 1;
}
if ($_POST["college"] == 'option2')
{
$college = 2;
}