0

i'm getting the message "Undefined index: name..." in an sql insert query.

$sql="INSERT INTO user (name) VALUES ('".$_POST['name']."')";

could someone tell me what is wrong?

ok, here is the (additional) code:

$name=(isset($_POST['name']));
$sql="INSERT INTO user (name) VALUES ('".$_POST['name']."')";

<form action="index.php" method="post" id="enterForm">
<p>Enter username:</p>
<input type="text" name="name" id="name" />
<input type="submit" name="enter" id="enter" value="OK" />
</form>
hausinho
  • 549
  • 1
  • 4
  • 8
  • There's no `name` field in `$_POST` when this statement is called. – andrewsi Oct 28 '13 at 13:19
  • There is nothing in that $_POST,before the query use a if isset(... – Mihai Oct 28 '13 at 13:19
  • can you show ur HTML code ??? ,, the error means that your form is not posting the the value name – Leo Bali Oct 28 '13 at 13:19
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Hanky Panky Oct 28 '13 at 13:23

2 Answers2

2

You are not sending field with name name so you can't get it from $_POST.

Also, never do something like that! You can't trust any user input. Filter it first or, better, use prepared statements.

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
Elon Than
  • 9,603
  • 4
  • 27
  • 37
0

Try like this

if(isset($_POST['name']))
{
//Sanitize $_POST['name'] before directly inserting it to the table
$sql="INSERT INTO user (name) VALUES ('".$_POST['name']."')";
}
else
{
echo "No data";
}
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126