atMaarten
atMaarten

Reputation: 93

Inserting name into database?

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)?

Upvotes: 0

Views: 544

Answers (2)

aztechy
aztechy

Reputation: 640

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.

Upvotes: 2

Raspoutine
Raspoutine

Reputation: 82

The placeholder attribut allows in html 5 to put a default value in an element of a form. This default value is remove when the element has focus. But in your case you have forget the attribut name. Try this:

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

Upvotes: 0

Related Questions