2017-02-26 27 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

この問題を解決する方法はありますか?あなただけproduct_register.phpでフォームのセクション間の変数を表示している

+0

"これは新しいエラーで、その意味がわかりません。" - http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php –

答えて

0

私はあなたにこの質問を解決するのを手伝いたいと考えています。 phpとhtmlの切り替えのためにproduct_register.phpコードを読みにくかったので、私は完全に書き直してgauravがあなたの実際のコードに何を言おうとしているかを加えました。

また、値に名前属性を追加して、送信時に値が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にアクセスできるようにすることをお勧めします。

0

ので、POST配列にはfname & lnameがないよりも、送信ボタン上のときに、ユーザーがクリックすると、あなたはindex.phpvar_dump($_POST)を通じて確認することができます。フォームで、次のように隠された入力タイプに

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

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

を設定することができて$fname$lnameを送信する場合ので、ユーザは、あなたがproduct_register.phpを通じて$_POST['fname']または$_POST['lname']を訴えることができるより送信ボタンをクリックします。

関連する問題