ユーザーが製品をデータベースに挿入できるフォームを作成しようとしています。入力する必要がある項目の1つにカテゴリがあります。これは選択可能なカテゴリから選択するための入力フォームを使用しています(PHPとMySQLは利用可能なすべてのカテゴリを表示します)。 select要素内のオプションはPHPで与えられます。しかし何らかの理由で、私がアプリケーションを実行すると、カテゴリは表示されません。エラーや何もなく、データベースへの接続が保証されています。コードは以下の通りです。それは機能を持つ任意のMy Selectフォームにデータベース項目が表示されない
ファイルを助け場合、私は、MVCパターンを使用しています(category_db.php)ファイル(index.phpを)
<?php
require ('../models/database.php');
require ('../models/product_db.php');
require ('../models/category_db.php');
$action = filter_input(INPUT_POST, 'action');
if($action == NULL || $action==FALSE){
$action = 'list_products';
}
if ($action == 'list_products'){
$categories = get_categories();
$category_id = filter_input(INPUT_GET, 'category_id');
if ($category_id == NULL || $category_id == FALSE){
$category_id = 1;
}
$product_item = get_product_list($category_id);
include ('product_list.php');
} else if ($action = 'delete_product') {
$product_id = filter_input(INPUT_POST, 'product_id');
$category_id = filter_input(INPUT_POST, 'category_id');
delete_product($product_id);
header("Location: .?category_id=$category_id");
}
else if($action == 'add_product'){
$categories = get_categories();
include('add_product_form.php');
}
そして用
<?php
function get_categories() {
global $db;
$query = "SELECT * FROM categories ORDER BY categoryID";
$statement= $db->prepare($query);
$statement->execute();
return $statement;
}
コントローラ最後に、フォームを表示するページ(add_product_form.php)
<?php include '../includes/header.php'; ?>
<main>
<h3>Add Product Form</h3>
<form method="post" action=".">
<label for="category">Category</label>
<select name="category">
<?php foreach ($categories as $category) : ?>
<option value="<?php echo $category['categoryID']; ?>">
<?php echo $category['categoryName']; ?>
</option>
<?php endforeach; ?>
</select>
<br>
<label for="product_code">Product Code</label>
<input type="text" name="product_code"><br>
<label for="product_name">Product Name</label>
<input type="text" name="product_name"><br>
<label for="list_price">List Price</label>
<input type="text" name="list_price"><br>
<input type="submit" value="Add Product">
</form>
</main>
<?php include '../includes/footer.php'; ?>
私はコードを3回チェックしましたが、私は問題を見つけることができません。これについての助けに感謝します。
function get_categories() {
global $db;
$query = "SELECT * FROM categories ORDER BY categoryID";
$statement= $db->prepare($query);
$statement->execute();
$categories = $statement->fetchAll(PDO::FETCH_ASSOC);
return $categories;
}
その後のことができますループ配列throught:それはどんな
あなたは 'index.php'に' product_add.php'を含めていません。 – urfusion