我試圖提交一個HTML <form>
並將input
和select
信息輸入到MySQL數據庫表中。我的代碼將<input type="text">
數據輸入到表格中,但它沒有輸入<select>
中的數據。向MySQL數據庫提交一個HTML選擇/下拉表單
這是<form>
我一直在使用:
<form enctype="multipart/form-data" action="send.php" method="post">
<select name="gender">
<option value="Male"> Male</option>
<option value="Female">Female</option>
</select>
<input type="text" name="name">
<input type="submit" value="Add">
</form>
這是我一直在使用輸入形式到表中的PHP:
<?php
$gender=$_POST['gender']; // This is the select/dropdown
$name=$_POST['name']; // This is the text input
mysql_connect("", "", "") or die(mysql_error()) ;
mysql_select_db("") or die(mysql_error()) ;
mysql_query("INSERT INTO `table1` VALUES ('$gender', '$name')") ;
?>
參見:一個媽媽的漏洞(http://xkcd.com/327/)。 –
哈哈,卡通喬聯繫的很棒。任何人,不要將$ _POST(或任何用戶輸入的數據)直接插入到mysql語句中。這打開了一些大的安全漏洞。查看方法[mysql_real_escape_string](http://php.net/manual/en/function.mysql-real-escape-string.php)。這並不完美,但是一開始。除此之外,還需要更多的信息來了解爲什麼你的數據沒有進入表格。 MySQL會拋出任何錯誤?使用[print_r](http://us2.php.net/manual/en/function.print-r.php)確保您正確捕獲表單中的數據。 – jbarreiros