2017-02-26 13 views
0

このドロップダウンメニューを動作させるためには、それは前に働いていましたが、私がいくつかの修正を加えたとき、それはもはや働いていませんでした。PHPのドロップダウンが以前に動作していましたが、今はそうではありません

Notice: Undefined variable: categories in D:\xampp\htdocs\tech_support\product_register\product_register.php on line 19 

しかし、私はそれは私のindex.phpの中で定義されている:表示されたエラーメッセージがある

<?php 

    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); 
     if ($email == NULL || $email == FALSE) { 
      $error = 'Invalid email. Try again.'; 
      include('../errors/error.php'); 
     } else { 
      $custEmail = get_email($_POST['email']); 
      if ($custEmail) { 
       $category_name = get_product_name($productName); 
       $categories = get_products(); 
       $products = get_products_by_name($name); 
       header("Location: product_register.php"); 
      } else { 
       $error = 'Invalid email. Try again.'; 
       include('../errors/error.php'); 
      } 
     } 
    } 
?> 

と私はそれが私の機能

<?php 

// Get all the products for the registration dropdown list 

function get_products() { 
    global $db; 
    $query = 'SELECT * FROM products 
       ORDER BY productCode'; 
    $statement = $db->prepare($query); 
    $statement->execute(); 
    return $statement;  
} 

function get_product_name($productName) { 
    global $db; 
    $query = 'SELECT * FROM products 
       WHERE productCode = :product_code';  
    $statement = $db->prepare($query); 
    $statement->bindValue(':product_name', $productName); 
    $statement->execute();  
    $product = $statement->fetch(); 
    $statement->closeCursor();  
    $product_name = $product['name']; 
    return $product_name; 
} 

function get_products_by_name($name) { 
    global $db; 
    $query = 'SELECT * FROM products 
       WHERE products.name = :name 
       ORDER BY productCode'; 
    $statement = $db->prepare($query); 
    $statement->bindValue(":name", $name); 
    $statement->execute(); 
    $products = $statement->fetchAll(); 
    $statement->closeCursor(); 
    return $products; 
} 

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

    <?php endif; ?> 
     <form action="index.php" method="post"> 
    <label>Customer:</label><br> 
<?php echo $email; ?> 
    <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'; ?> 

私は何ですか間違っている?

+0

'' 'product_register.php''の内容を表示 – Wolen

+0

私は' product_register.php'の '19'行を見ています。スコープにない変数への参照があると思います。 – spencer7593

+1

この行は 'header(" Location:product_register.php ");'インクルード前?今あなたはリダイレクトしていますので、 '$ categories'は"失われます "。 – Jeff

答えて

2

の操作を行います。

header("Location: product_register.php"); 

0への参照が失われます。

include('product_register.php')に戻すか、@ Wolenの解決方法に従ってください。

0

これは私が それを掲示したときに、私はそれはまだコードの流れを追跡し、それを理解する必要性を示していると思うように私は、削除するつもりはないが、それは思った正解

ではありません - OP文がデバッグを続けることができるように、if文を見たところで停止しました - 私はheader()文をその中に入れませんでした。

if ($action == 'login') { 
    if ($email == NULL || $email == FALSE) { 
    } else { 
     if ($custEmail) { 
      $categories = get_products(); 

だから、次のいずれか:

  • $action'login'
  • または$emailが同じでない変数が定義されているが

    は、それが3 /場合はelse文の中にネストされますNULLまたはFALSE

  • または$custEmailが空の場合、NULLFALSEまたは他の多くの真でないステートメント。
+0

彼は '' 'header()' ''を使いますが、とにかく動作しません。 – Wolen

+0

@ウォーレンあなたは答えとしてそれを提出しているはずです - 私は他の誰かがちょうどやったのを見ます – Theo

+1

@テオはい、私は同じようにコメントし、OPが私に応答を与えた後、答えとしてそれを投稿しました。 1。 – Jeff

2

あなたは$categories変数を宣言していなかったproduct_register.phpindex.phpからリダイレクトやっています。 header()を使用して別のページにユーザーをリダイレクトし、データを渡す必要がある場合は、セッションを使用することをお勧めします。ファイルの先頭にあるsession_start()を使用してセッションを開始する必要があります。この場合、両方のファイルでこの関数を呼び出す必要があります。

次に、header()のインデックス.phpのようにセッションに保存するだけです。

$_SESSION['categories'] = get_products(); 

次に、product_registerにあります。PHP 代わり

<?php foreach ($categories as $category) : ?> 

あなたはここにリダイレクトに含めるを変更したので

<?php foreach ($_SESSION['categories'] as $category) : ?> 

または

<?php 
$categories = $_SESSION['categories']; 
foreach ($categories as $category): 
?> 
+0

'header'-redirectを使いこなすときは非常に良い答えです!がんばり続ける! – Jeff

関連する問題