-1

What I am trying to do is print the first name and last name of a customer from my database onto a php document and I am getting errors.

These are the errors:

Notice: Undefined index: fName in D:\xampp\htdocs\tech_support\product_register\index.php on line 36

Notice: Undefined index: lName in D:\xampp\htdocs\tech_support\product_register\index.php on line 37

This is a new error and I don't know what that means.

Here is the code that I have made:

index.php:

<?php

// Get your db connection file, be sure it has a new connection to the
// tech support database
require('../model/database.php');

// Get the models needed - work will need to be done in both
require('../model/customer_db.php');
require('../model/product_db.php');
require('../model/registration_db.php');

$action = filter_input(INPUT_POST, 'action');
if ($action == NULL) {
    $action = filter_input(INPUT_POST, 'action');
    if ($action == null) {
        $action = 'product_register';
    }
}

//When the user clicks the first link on the home page, bring them to the login page.
if ($action == 'product_register') {
    include('customer_login.php');
}

//When the user clicks the login button, the system checks for errors in their typing.
//If no errors are present, proceed to product_register.php.
else if ($action == 'login') {
    $email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
    $firstName = filter_input(INPUT_POST, 'fName');
    $lastName = filter_input(INPUT_POST, 'lName');
    if ($email == NULL || $email == FALSE) {
        $error = 'Invalid email. Try again.';
        include('../errors/error.php');
    } else {
        $custEmail = get_email($_POST['email']);
        $fName = get_fname($_POST['fName']);
        $lName = get_email($_POST['lName']);        
        if ($custEmail) {
            $fName = get_fname($firstName);
            $lName = get_lname($lastName);
            $categories = get_products();

            include('product_register.php');
        } else {
            $error = 'Invalid email. Try again.';
            include('../errors/error.php');
        }
    }
}

customers_db.php:

<?php
//Get a customer by their email address and
//check if the data entered in the form is true or false
function get_email($email) {
    global $db;
    $query = 'SELECT * FROM customers WHERE email = :email';    
    $statement = $db->prepare($query);
    $statement->bindValue(':email', $email);
    $statement->execute(); 
    $status = false;
    if($statement->rowCount()){
        $status = true;
    }    
    return $status;
}

//Get customer by their first name
function get_fname($firstName) {
    global $db;
    $query = 'SELECT * FROM customers WHERE firstName = :firstName';    
    $statement = $db->prepare($query);
    $statement->bindValue(':firstName', $firstName);
    $statement->execute();
}

//Get customer by their last name
function get_lname($lastName) {
    global $db;
    $query = 'SELECT * FROM customers WHERE lastName = :lastName';    
    $statement = $db->prepare($query);
    $statement->bindValue(':lastName', $lastName);
    $statement->execute();
}

product_register.php:

<?php include '../view/header.php'; ?>
<?php require('../model/database.php'); ?>
<main>

    <h2>Register Product</h2>
    <?php if (isset($message)) : ?>
        <p><?php echo $message; ?></p>
        <?php
    else:
        $email= filter_input(INPUT_POST, 'email');
        $firstName = filter_input(INPUT_POST, 'firstName');
    $lastName = filter_input(INPUT_POST, 'lastName');
        ?>

    <?php endif; ?>
         <form action="index.php" method="post">
    <label>Customer:</label>
        <?php echo $fName; ?>&nbsp;&nbsp;&nbsp;<?php echo $lName; ?><br>
    <label>Product:</label>
    <select>
        <?php foreach ( $categories as $category ) : ?>
            <option value="<?php echo $cateogry['productCode']; ?>">
                <?php echo $category['name']; ?>
            </option>
        <?php endforeach; ?>
        </select><br>

        <input type="hidden" name="action" value="register_product">
        <input type="submit" value="Register Product">
    </form>
</main>
<?php include '../view/footer.php'; ?>

This is what its supposed to do its supposed to print the name of the customer like this: enter image description here

Any ideas on how to fix this problem?

tereško
  • 58,060
  • 25
  • 98
  • 150
HawkBlade124
  • 107
  • 1
  • 9
  • "This is a new error and I don't know what that means." - http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php – Paul Spiegel Feb 26 '17 at 03:55

2 Answers2

0

you are just displaying variable between form section in product_register.php so when user click on submit button than there is no fname & lname in POST array ,you can check through var_dump($_POST) on index.php . if you want to send $fname or $lname through you to set in hidden input type like this in form

<input type="hidden" name="fname" value="<?php echo $fname ; ?>">

<input type="hidden" name="fname" value="<?php echo $fname ; ?>"> 

so user click on submit button than you can sue $_POST['fname'] or $_POST['lname'] through product_register.php .

gaurav
  • 1,281
  • 1
  • 13
  • 25
0

I'd like to help you get some resolution on this question. I found it hard to read your product_register.php code because of all of the toggling of php and html, so I've done a full rewrite of it and added what gaurav was trying to say into your actual code.

I also added a name attribute to your Product <select> so that the value was POSTed on submit.

product_register.php:

include '../view/header.php';
require('../model/database.php');
echo "<main>";
    echo "<h2>Register Product</h2>";
    if(isset($message)){
        echo "<p>$message</p>";
    }else{
        $email=filter_input(INPUT_POST,'email');
        $firstName=filter_input(INPUT_POST,'firstName');
        $lastName = filter_input(INPUT_POST,'lastName');
    }
    echo "<form action=\"index.php\" method=\"post\">";
        echo "<label>Customer: </label>$fName $lName<br>";
            echo "<input type=\"hidden\" name=\"email\" value=\"$email\">"; // POST this value
            echo "<input type=\"hidden\" name=\"fName\" value=\"$firstName\">"; // POST this value
            echo "<input type=\"hidden\" name=\"lName\" value=\"$lastName\">"; // POST this value
        echo "<label>Product: </label>";
        echo "<select name=\"category\">"; // declare name attr so that this value is POSTed
            foreach($categories as $category){
                echo "<option value=\"{$cateogry['productCode']}\">{$category['name']}</option>";
            }
        echo "</select><br>";
        echo "<input type=\"hidden\" name=\"action\" value=\"register_product\">";
        echo "<input type=\"submit\" value=\"Register Product\">";
    echo "</form>";
echo "</main>";
include '../view/footer.php';

Just a couple added considerations... index.php:

change:

$action = filter_input(INPUT_POST, 'action');
if ($action == NULL) {
    $action = filter_input(INPUT_POST, 'action');
    if ($action == null) {
        $action = 'product_register';
    }
}

to:

if(isset($_POST['action'])){
    $action=filter_input(INPUT_POST,'action')
}else{
    $action="product_register";
}

It seems like your code could benefit from the use of $_SESSION storage for user identification, but creating a solution for that would be too involved for posting on SO, so I'll just recommend that you research this on your own.

//When the user clicks the first link on the home page, bring them to the login page.
if ($action == 'product_register') {
    include('customer_login.php');
}

seems doomed to be forever directing to customer_login.php. Perhaps you want to add a check to see if they are already successfully logged in:

//Bring user to the login page if not successfully logged in.
if($action=='product_register'){
    if(!isset($_POST['email']) || isset($_POST['fName']) || isset($_POST['lName'])){
        $error='Invalid email. Try again.';
        include('customer_login.php');
    }else{
        // this process (from what I can tell) does not check actually verify that the user's three login details exist in the same row of the database, and so is not effective.
        $custEmail = get_email(filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL));
        if($custEmail){
            $fName = get_fname(filter_input(INPUT_POST,'fName'));
            $lName = get_email(filter_input(INPUT_POST,'lName'));
            $categories = get_products();
            include('product_register.php');
        }else{
            $error = 'Invalid email. Try again.';
            include('customer_login.php');
        }
    }
}

Your code is not designed in a way that I would use professionally. Perhaps there is more to this process that you have presented, but from the outside looking in I recommend that you research a solid login process and work up from there to permit access to product_register.php for only legitimate users.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136