表单无法正确提交 - PHP / MYSQL

时间:2016-05-12 02:05:45

标签: php mysql forms

我的表单没有正确提交。提交表单,并正确检查必填字段但是当它到达我的if语句检查没有错误时,它应该将表单信息输入到mysql并重定向到另一个页面,但它只是清除表单并保持在同一页面上。

对此肯定有一个简单的答案,但我找不到它。我似乎无法通过搜索找到同一问题的另一个实例,但我可能只是在错误的地方搜索。如果您需要任何包含,我可以提供它们,但我觉得这个页面上有问题。

manage_inventory.php

<?php require_once("../includes/session.php"); ?>
<?php require_once("../includes/db_connection.php"); ?>
<?php require_once("../includes/functions.php"); ?>
<?php require_once("../includes/validation_functions.php"); ?>
<?php confirm_logged_in(); ?>

<?php
  $admin_set = find_all_admins();
?>

<?php
if (isset($_POST['submit'])) {
  // Process the form

  // validations
  $required_fields = array("type", "part_number");
  validate_presences($required_fields);

  if (empty($errors)) {
    // Perform Create

    $type = mysql_prep($_POST["type"]);
    $part_number = mysql_prep($_POST["part_number"]);
    $cat = mysql_prep($_POST["cat"]);
    $desc = mysql_prep($_POST["desc"]);
    $sales_price = mysql_prep($_POST["sales_price"]);
    $tax = $_POST["tax"];
    $purchace_price = mysql_prep($_POST["purchace_price"]);

    $query  = "INSERT INTO inventory (";
    $query .= "type, part_number, cat, desc, sales_price, tax, purchace_price";
    $query .= ") VALUES (";
    $query .= "'{$type}', '{$part_number}', '{$cat}', '{$desc}', '{$sales_price}', '{$tax}', '{$purchace_price}'";
    $query .= ")";
    $result = mysqli_query($connection, $query);

    if ($result) {
      // Success
      $_SESSION["message"] = "Inventory item created.";
      redirect_to("inventory.php");
    } else {
      // Failure
      $_SESSION["message"] = "Inventory item creation failed.";
    }
  } 
} else {

}
?>

<?php $layout_context = "admin"; ?>

<?php include("../includes/layout/header.php"); ?>

<div id="nav">&nbsp;</div>
<div id="heading">
    <h1>Inventory</h1>
</div>
<div id="sidebar">
<a href="admin.php">&laquo; Main menu</a>
<br />
<a href="inventory.php">&laquo; Back</a>
</div>
<div id="page"> 

    <?php  message(); ?>
    <?php echo form_errors($errors); ?>
    <br />
    <form action="manage_inventory.php" method="post">
        <p>Type
            <select name="type">
                <?php
                    $type_set = find_all_types();
                    while ($type = mysqli_fetch_assoc($type_set)){ 
                ?>
                <option value= "<?php echo $type['type'] ?>"><?php echo $type ['type'] ?></option>
                <?php } ?>
            </select>
        </p>
        <p>Part Number
            <input type="text" name="part_number" value="" />
        </p>
        <p>Category
            <select name="cat">
                <?php
                    $cat_set = find_all_cats();
                    while ($cat = mysqli_fetch_assoc($cat_set)){ 
                ?>
                <option value= "<?php echo $cat ['category'] ?>"><?php echo $cat ['category'] ?></option>
                <?php } ?>
            </select>
        </p>
        <p>Description
            <input type="text" name="desc" value="" />
        </p>
        <p>Sales Price
            <input type="text" name="sales_price" value="" />
        </p>
        <p>Taxable?
            <input type="radio" name="tax" value="0" /> No
                &nbsp;
            <input type="radio" name="tax" value="1" /> Yes
        </p>
        <p>Purchace Price
            <input type="text" name="purchace_price" value="" />
        </p>
        <input type="submit" name="submit" value="Save" />
    </form>
    <br />
    <a href="inventory.php">Cancel</a>
</div>

../包括/ validation_functions.php

这就是创建$ errors的原因,这个相同的代码适用于使用相同代码的其他页面。

<?php

$errors = array();

function fieldname_as_text($fieldname) {
  $fieldname = str_replace("_", " ", $fieldname);
  $fieldname = ucfirst($fieldname);
  return $fieldname;
}

// * presence
// use trim() so empty spaces don't count
// use === to avoid false positives
// empty() would consider "0" to be empty
function has_presence($value) {
    return isset($value) && $value !== "";
}

function validate_presences($required_fields) {
  global $errors;
  foreach($required_fields as $field) {
    $value = trim($_POST[$field]);
    if (!has_presence($value)) {
        $errors[$field] = fieldname_as_text($field) . " can't be blank";
    }
  }
}

// * string length
// max length
function has_max_length($value, $max) {
    return strlen($value) <= $max;
}

function validate_max_lengths($fields_with_max_lengths) {
    global $errors;
    // Expects an assoc. array
    foreach($fields_with_max_lengths as $field => $max) {
        $value = trim($_POST[$field]);
      if (!has_max_length($value, $max)) {
        $errors[$field] = fieldname_as_text($field) . " is too long";
      }
    }
}

// * inclusion in a set
function has_inclusion_in($value, $set) {
    return in_array($value, $set);
}

?>

2 个答案:

答案 0 :(得分:0)

如果您的查询在此处更改,则某些列名称为mysql reserved keyword

$query .= "`type`, `part_number`, `cat`, `desc`, `sales_price`, `tax`, `purchace_price`";

答案 1 :(得分:0)

在将我的变量更改为更具描述性并避免保留关键字时,我发现列名与我的数据库不同,因此出错。谢谢大家为我看过这个,感谢你的帮助。我将考虑这些意见。