为什么表单中的输入没有被传递

时间:2016-10-18 06:02:54

标签: php forms

我是php新手并创建了两个表单。一个表单接受用户的名字,第二个表单接收用户的姓氏。我不确定为什么当用户在表单中输入文本时,没有传递任何内容。

带有表单的php

<?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
}
?>

add_product功能

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

2 个答案:

答案 0 :(得分:1)

除了@ MahfuzulAlam的回答,另一个问题是:

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

答案 1 :(得分:0)

尝试使用type = 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和last_name,您的$first_name$last_name变量都将为空。因为您使用$_POST['FirstName'], $_POST['LastName']分别设置它们的值,所以这并不存在,并且都返回NULL。重写这些行如下:

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