0

I am new to php and I am currently creating a form which sends data to a local mySQL server. I have a database which stores the users first name, last name, email and address. So the form takes the data and stores it in the database. I'm stuck because I get an error 'Notice: Undefined index: id'

This is where i'm stuck:

<div class="text-center">
              <form class="form-inline" action="tables.php" method="post">
                <div class="form-group">
                  <label for="first name">Add user:</label>
                  <input type="text" name="user" id="first name" class="form-control" value="<?=$_POST['id'];?>">
                <div>
              </form>
          </div>
Brad
  • 159,648
  • 54
  • 349
  • 530
  • `$_POST` data comes from the HTTP POST request. If you didn't have one of those, there isn't going to be anything in `$_POST`... You'll have to default this to empty or something. Also, be sure to use `htmlspecialchars()` with any arbitrary data injected into the context of HTML. – Brad Apr 19 '18 at 22:28
  • Once you submit the form, the webserver and PHP will automatically define that for you. Note that the index on your array is `user` in this case. so `$_POST['user'];` should work. Remember to escape the value before you save it in the database. – Ibu Apr 19 '18 at 22:33
  • I tried having user as the index but I still get the same error. Do i get the value for the index from the database? – Siya Skosana Apr 19 '18 at 22:45

1 Answers1

0

$_POST['id'] returns user submitted post data id, but here there's no id to show, that why causing the error message

if you want to load user-submitted value for the specific field, you should call the post data with the field name. like below

<input type="text" name="user" id="first name" class="form-control" value="<?=$_POST['user'];?>">

here $_POST['user'], user is the name of input field.

however, if you use $_POST['user'] in the form field, Notice: Undefined index: user will be appeared, the reason for the Notice is code PHP cannot detect $_POST['user'] value before user submits the form. so, it will give notice. These notices are not issues, to prevent this, normally we use bypass code at the top of the script like below

error_reporting(E_ALL & ~E_NOTICE);

it means PHP will report all errors expect NOTICE error.

Anfath Hifans
  • 1,588
  • 1
  • 11
  • 20