2016-10-18 10 views
0

私はphpを初めて使い、2つのフォームを作成しました。 1つのフォームはユーザーのファーストネームをとり、2つ目のフォームはユーザーのラストネームをとります。なぜユーザーがフォームにテキストを入力しても何も渡されていないことがわかりません。フォームからの入力が受け付けられない理由

function add_product($category_id, $first_name, $last_name) { 
    global $db; 
    $query = "INSERT INTO products 
       (categoryID, FirstName, LastName) 
       VALUES 
       ('$category_id', '$first_name', '$last_name')"; 
    $db->exec($query); 
} 
?> 
+0

どのようなエラーが表示されますか? – ravisachaniya

+0

@ravisachaniyaは、フォームのユーザー入力を受け入れるはずのPHPでは、入力を検証するif文が空であるか、入力が空である場合を実行しません。 –

+0

ようこそスタックオーバーフロー! [ask]と[mcve]を参照してください。 – Mat

答えて

1

、別の問題がある:

if(isset($_POST['first_name'])){$first_name = $_POST['FirstName'];} 
                 ^^^^^^^^^ 

if(isset($_POST['last_name'])){$last_name = $_POST['LastName'];} 
                ^^^^^^^^ 

それは次のようになります。

if(isset($_POST['first_name'])){$first_name = $_POST['first_name'];} 

if(isset($_POST['last_name'])){$last_name = $_POST['last_name'];} 

また、PHP 7で、の省略形があります現在行っていること:

$first_name = $_POST['first_name'] ?? ""; 

$last_name = $_POST['last_name'] ?? ""; 

これはNull Coalesce Operatorと呼ばれています。

+0

私はそれをフォームのテキストがデータベースに渡されるようにしなければならないと思いました。データベースでは、私は 'FirstName'という値を持っています –

+1

データベースとは何が関係していますか? DBは別の問題です。現在、HTTPデータの受け渡しに問題があります。 '$ _POST ['FirstName']'と '$ _POST ['LastName']'は、これらの変数でPOSTメソッドを送信するPHPファイルがないため、常にnullになります。 –

+0

@Rax Weberアドレッシングに感謝します。私は詳細を使って答えを編集しました。 @KeithCodeあなたの列名が何であっても。 'FirstName'と' LastName'カラムの値として '$ first_name'と' $ last_name'を渡す限り、それは影響しません。 –

0

形態

<?php include '../view/header.php'; ?> 
<div id="main"> 
    <h1>Add Athlete</h1> 
    <form action="index.php" method="post" id="add_product_form"> 
     <input type="hidden" name="action" value="add_product" /> 

     <label>Country:</label> 
     <select name="category_id"> 
     <?php foreach ($categories as $category) : ?> 
      <option value="<?php echo $category['categoryID']; ?>"> 
       <?php echo $category['categoryName']; ?> 
      </option> 
     <?php endforeach; ?> 
    </select> 
    <br /> 

    <label>Code:</label> 
    <input type="input" name="code" /> 
    <br /> 

    <label>First Name:</label> 
    <input type="input" name="first_name" /> 
    <br /> 

    <label>Last Name:</label> 
    <input type="input" name="last_name" /> 
    <br /> 

    <label>&nbsp;</label> 
    <input type="submit" value="Add Athlete" /> 
    <br /> <br /> 
    </form> 
    <p><a href="index.php?action=list_products">View List of Olympic Athletes</a></p> 

</div> 
<?php include '../view/footer.php'; ?> 

フォームからの入力を取り込むPHPは

<?php 
require('../model/database.php'); 
require('../model/product_db.php'); 
require('../model/category_db.php'); 

if (isset($_POST['action'])) { 
    $action = $_POST['action']; 
} else if (isset($_GET['action'])) { 
    $action = $_GET['action']; 
} else { 
    $action = 'list_products'; 
} 

if ($action == 'list_products') { 
    // Get the current category ID 
    $category_id = $_GET['category_id']; 
    if (!isset($category_id)) { 
     $category_id = 1; 
} 

// Get product and category data 
$category_name = get_category_name($category_id); 
$categories = get_categories(); 
$products = get_products_by_category($category_id); 

    // Display the product list 
    include('product_list.php'); 
} else if ($action == 'delete_product') { 
    // Get the IDs 
    $product_id = $_POST['product_id']; 
    $category_id = $_POST['category_id']; 

    // Delete the product 
    delete_product($product_id); 

    // Display the Product List page for the current category 
    header("Location: .?category_id=$category_id"); 
} else if ($action == 'show_add_form') { 
    $categories = get_categories(); 
    include('product_add.php'); 
} else if ($action == 'add_product') { 
    $category_id = $_POST['category_id']; 
    $first_name = ""; 
    if(isset($_POST['first_name'])){$first_name = $_POST['FirstName'];} 
    $last_name = ""; 
    if(isset($_POST['last_name'])){$last_name = $_POST['LastName'];} 



    // Validate the inputs 
    if (empty($first_name) || empty($last_name)) { 
     $error = "Invalid product data. Check all fields and try again."; 
     include('../errors/error.php'); 
    } else { 
     add_product($category_id, $first_name, $last_name); 

     // Display the Product List page for the current category 
     header("Location: .?category_id=$category_id"); 
    } 
} else if ($action == 'list_categories') { 
    $categories = get_categories(); 
    include('category_list.php'); 
} else if ($action == 'add_category') { 
    $first_name = $_POST['FirstName']; 

    // Validate inputs 
    if (empty($name)) { 
     $error = "Invalid category name. Check name and try again."; 
     include('view/error.php'); 
    } else { 
     add_category($name); 
     header('Location: .?action=list_categories'); // display the Category List page 
    } 
} else if ($action == 'delete_category') { 
    $category_id = $_POST['category_id']; 
    delete_category($category_id); 
    header('Location: .?action=list_categories');  // display the Category List page 
} 
?> 

とPHP add_product関数は= textないinputタイプを使用してみてください。 <input type="text" name="last_name" />など。

このコードには問題があります。

if(isset($_POST['first_name'])){$first_name = $_POST['FirstName'];} 
if(isset($_POST['last_name'])){$first_name = $_POST['LastName'];} 

は何であれ、あなたはFIRST_NAMEとして渡すと、あなたの$first_name$last_name変数は空になりますLAST_NAME。 $_POST['FirstName'], $_POST['LastName']を使用してそれぞれの値を設定しているため、実際には存在せず、両方ともNULLを返します。以下のようにこれらの行を書き換え:MahfuzulAlamの答え@ほか

if(isset($_POST['first_name'])){$first_name = $_POST['first_name'];} 
if(isset($_POST['last_name'])){$first_name = $_POST['last_name'];} 
+0

'type'が認識されない場合、' text'のように動作します。 – Barmar

+0

はい。私は後でそれをチェックした。問題は実際には 'type'ではなく、前述のようにコード内にありました。ありがとう。 –

関連する問題