2017-02-26 35 views
-1

我想要做的是從我的數據庫打印客戶的名字和姓氏到一個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'; ?> 

這是它應該做的o是否應該打印這樣的客戶名稱: enter image description here

有關如何解決此問題的任何想法?

+0

「這是一個新的錯誤,我不知道這意味着什麼。」 - http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php –

回答

0

我想幫助你解決這個問題。 我發現很難閱讀您的product_register.php代碼,因爲所有的php和html切換,所以我已經完全重寫了它,並添加了什麼gaurav試圖說到你的實際代碼。

我還爲您的產品<select>添加了名稱屬性,以便值在提交時發佈。

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存儲用戶識別受益,但創造了一個解決辦法是太涉及了這麼張貼,所以我就建議你研究這對你擁有。

//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。

0

你只是顯示形式部分之間的變量product_register.php所以當上比存在POST數組沒有fname & 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 ; ?>"> 

等用戶點擊提交按鈕,你可以通過product_register.php起訴$_POST['fname']$_POST['lname']

相關問題