2016-10-18 30 views
0

我是新来的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 
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); 
} 
?> 
+0

你会得到什么错误? – ravisachaniya

+0

@ravisachaniya在应该在窗体的用户输入中使用的php中,验证输入的if语句为空或者不运行输入为空的情况。 –

+0

欢迎来到Stack Overflow!请参阅[问]和[mcve]。 – Mat

回答

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

+0

我想我必须这样做,以便表单中的文本将传递到数据库中。在数据库中,我有一个名为'FirstName'的值 –

+1

它与数据库有什么关系?数据库是一个单独的问题。您目前在通过HTTP数据时遇到问题。 '$ _POST ['FirstName']'和'$ _POST ['LastName']'将始终为空,因为没有任何PHP文件向这些变量发送POST方法。 –

+0

@Rax Weber感谢您的处理。我还详细地编辑了我的答案。 @KeithCode无论你的列名是什么。只要您将'$ first_name'和'$ last_name'作为'FirstName'和'LastName'列的值传递,它就不会受到影响。 –

0

尝试使用类型= textinput PHP的。 <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'];} 
+0

如果'type'没有被识别,它就像'text'一样。 – Barmar

+0

是的。我稍后再检查。问题实际上并不在'type'中,它是在代码中提到的。谢谢。 –

相关问题