2

I have a simple web form, which I want the user to fill out and when they click the submit button the information from the form should be inserted into the correct tables in the mySQL database for later use. I have a database called foundation_fitness with a table called Client.

The problem I am facing is this error message upon clicking submit:

Notice: Undefined index: exampleInputEmail1 in C:\xampp\htdocs\ffitness\dist\new_client.php on line 19

Notice: Undefined index: forname1 in C:\xampp\htdocs\ffitness\dist\new_client.php on line 20

Notice: Undefined index: surname1 in C:\xampp\htdocs\ffitness\dist\new_client.php on line 21

Notice: Undefined index: height1 in C:\xampp\htdocs\ffitness\dist\new_client.php on line 22

Notice: Undefined index: weight1 in C:\xampp\htdocs\ffitness\dist\new_client.php on line 23

Notice: Undefined index: bodyfat1 in C:\xampp\htdocs\ffitness\dist\new_client.php on line 24

Below is my code for new_client.php. EDIT: Changed Foundation_fitness to Client for the INSERT INTO

<?php

define('DB_NAME', 'foundation_fitness');
define('DB_USER', 'root');
define('DB_HOST', 'localhost');

$link = mysql_connect(DB_HOST, DB_USER);

if (!$link) {
     die('Could not connect: ' . mysql_error());
     }

     $db_selected = mysql_select_db(DB_NAME, $link);

     if (!$db_selected) {
     die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
     }

     $value2 = $_POST['exampleInputEmail1'];
     $value3 = $_POST['forname1'];
     $value4 = $_POST['surname1'];
     $value5 = $_POST['height1'];
     $value6 = $_POST['weight1'];
     $value7 = $_POST['bodyfat1'];

     $sql = "INSERT INTO client (Client_email, Client_forename, Client_surname, Height, Weight, Body_fat) VALUES ('$value2', 
     '$value3', '$value4', '$value5', '$value6')";
    
    $result = mysql_query($sql);

     mysql_close();

And the code for my html form is as follows: EDIT - Changed method mistake and id to name

<form action="new_client.php" method="POST">
        <div class="form-group">
          <label for="exampleInputEmail1">Email address</label>
          <input type="email" class="form-control" name="exampleInputEmail1" placeholder="Enter email">
        </div>
        <div class="form-group">
          <label for="forename1">Forename</label>
          <input type="text" class="form-control" name="forname1" placeholder="Enter First Name">
        </div>
        <div class="form-group">
          <label for="surname1">Surname</label>
          <input type="text" class="form-control" name="surname1" placeholder="Enter Surname">
        </div>
        <div class="form-group">
          <label for="height1">Height (cm)</label>
          <input type="text" class="form-control" name="height1" placeholder="Enter Height">
        </div>
        <div class="form-group">
          <label for="weight1">Weight (kgs)</label>
          <input type="text" class="form-control" name="weight1" placeholder="Enter Weight">
        </div>
        <div class="form-group">
          <label for="bodyfat1">Body Fat %</label>
          <input type="text" class="form-control" name="bodyfat1" placeholder="Enter Body Fat Percentage">
        </div>
        <button type="submit" class="btn btn-default">Submit</button>
      </form>

Any help with the error and getting the php script working would be much appreciated!

EDIT: I am now not getting the error message as I changed the method mistake and id to name in the HTML form. I am still not able to get the information into the table on the mySQL database even with the additional change of the table name which should have been client as I do not have a table called foundation_fitness that is the database name.

Community
  • 1
  • 1
engage_roll
  • 143
  • 1
  • 13
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – andrewsi Jan 22 '15 at 00:21

3 Answers3

3

It means that there are no such keys in the $_POST array. You should use name attribute (not id) for the form elements if you want to use them later as keys in the $_POST array. Also, you have a typo : mehtod should be method attribute in your form element.

potashin
  • 44,205
  • 11
  • 83
  • 107
  • Thanks for this. I have a changed the `id` attribute to `name` although I am still receiving the same error? – engage_roll Jan 21 '15 at 20:10
  • Great it is now working - well I have not got an error message. Although no data has been inserted into the client table when I look at the mySQL database. Do you know why this would be? – engage_roll Jan 21 '15 at 20:16
1

Your INSERT statement uses foundation_fitness as the table name, which is the same as your DB name. Your answer to @notulysses comment suggests the table name should be client

INSERT INTO client ...

Can you confirm that you have a foundation_fitness table inside your foundation_fitness database.

EDIT:

If your table name is Client, your query might have to be INSERT INTO Client, since table names could be case sensitive, depending on the underlying file system. Also, the error seems to be happening at the mysql level, so you can change your code this way to actually see the error :

$result = mysql_query($sql);
if ($result !== false) {
    // everything is fine, proceed with script
} else {
    // Something went wrong :
    die(mysql_error($link)); // to see the error from MySQL
}

The error output from MySQL should help you a lot, if not, post it here so we can further help you.

As a side note, mysql_connect is deprecated as of PHP 5.5, so perhaps looking into PDO would be a good start to refactoring your code.

Another thing (very important) : your query is exposed to SQL Injection, you are taking user input without validation and/or sanitization. If that is your production code, you absolutely need to fix it. You should use prepared statements to shield yourself from SQL injection.

Cedric
  • 61
  • 5
0

You are getting these notices because of post request to same page. Once the page load it read your php code but couldn't find any post data. As you are using single php file i.e form's 'action' is redirecting the page to itself. So once you fill the form and click submit these notices disappear, as data is sent via post. You can hide these notices by usingerror_reporting(E_ALL ^ E_NOTICES). It hides all the notice error you are getting. But I would recommend you to use a different page for form action redirect to insert data into database and then redirect that page back to you main page using header('Location:newclient.php').

Moid
  • 1,447
  • 1
  • 13
  • 24