duansao6776 2013-08-30 22:29
浏览 20
已采纳

将名称插入数据库?

What is the best way to insert a PHP 5 form into a MySQL database?

<form  action="script.php" method="post">
 <fieldset>
  <input id="name" type="text" placeholder="name" />
  <input type="submit" value="Opslaan" />           
 </fieldset>
</form>​    

Do I still have you use all of these?

$name= $_POST['name'];  
$name = stripslashes($name);  
$name = mysql_real_escape_string($name);

mysql_connect($host,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO names VALUES ('','$name')";
mysql_query($query);
mysql_close();

Because when I do this, the script only enters the ID, the name field remains empty..

EDIT: How to use PDO or mysqli in PHP 5 (by the latest standards)?

  • 写回答

2条回答 默认 最新

  • dongshao8566 2013-08-30 22:30
    关注

    You need your name input to have the name attribute="name" ie.

    <input type="text" id="name" name="name" placeholder="Enter your name" />
    

    To fully address your answer.

    • The mysql_* library is deprecated on the latest PHP's and SHOULD NOT be used. Use PDO or MySQLi instead. Thanks to Prix for pointing that out.
    • You'll want to sanitize data that users give. This question has been asked before and a good answer exists here: What's the best method for sanitizing user input with PHP?
    • The reason your _POST parameter was not doing anything on your name input was due to the fact that was mentioned above.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?