0

I am getting this error:

undefined index:name why its not responding.

<?php
$url="localhost";
$user="root";
$password="";
$db="sms";
$connection=new mysqli($url,$user,$password,$db);
$username=$_POST["name"];
$query="INSERT INTO student(name)VALUES('".$username."')";
$connection->query($query);   
?>
goto
  • 7,908
  • 10
  • 48
  • 58

2 Answers2

0

When you get Username $username = $_POST["name"]; Value for $username variable come from global $_POST variable. If you start your script without POST, you cant read key 'name' from $_POST array. This key didn't exist and you get this error. SOLUTION for this problem. Just check for $_POST value with isset() and if() statments.

if (isset($_POST["name"])) {
    $username=$_POST["name"];
    $query="INSERT INTO student(name)VALUES('".$username."')";
    $connection->query($query);   
}
plamen
  • 304
  • 1
  • 2
  • 11
0

Make sure that the <input type="blablabla" name="name" ...>

Also make sure to wrap the PHP code in:

if(!empty($_POST["name"])){
    // the code you posted ...
}
Hossam
  • 1,126
  • 8
  • 19