试图在程序中打印名字和姓氏,得到错误

时间:2017-02-26 03:17:27

标签: php mysql

我要做的是将数据库中客户的名字和姓氏打印到php文档上,我收到错误。

这些是错误:

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

这是一个新错误,我不知道这意味着什么。

以下是我所做的代码:

的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'; ?>

这应该是它应该打印的客户名称,如下所示: enter image description here

有关如何解决此问题的任何想法?

2 个答案:

答案 0 :(得分:0)

您只是在product_register.php中的表单部分之间显示变量,因此当用户点击提交按钮时,没有fname&amp;在POST数组中lname,您可以在var_dump($_POST)上查看index.php。如果你想通过你发送$fname$lname来设置隐藏的输入类型,如表格

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

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

所以用户点击“提交”按钮比起$_POST['fname']$_POST['lname']通过product_register.php起诉。

答案 1 :(得分:0)

我想帮你解决一下这个问题。 我发现很难读懂你的product_register.php代码因为所有的php和html的切换,所以我完全重写了它并添加了gaurav试图对你的实际代码说的内容。

我还为您的商品<select>添加了一个名称属性,以便在提交时将值设置为POST。

<强> 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';

只是添加了一些注意事项...... 的的index.php

变化:

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

为:

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

似乎您的代码可以从使用$ _SESSION存储区获得用户身份识别中受益,但为此创建解决方案太过于涉及在SO上发布,所以我建议您在您的网站上进行研究自己的。

//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');
}

似乎注定要永远指向customer_login.php。也许您想添加一个检查以查看它们是否已成功登录:

//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');
        }
    }
}

您的代码的设计方式不是我专业使用的方式。也许你已经介绍了这个过程,但是从外面看,我建议你研究一个可靠的登录过程并从那里开始工作以允许仅为合法用户访问product_register.php。