首页 > 解决方案 > 无法从表单将数据插入数据库?

问题描述

我正在创建一个可以创建新产品的小型网上商店,最近它根本不工作,它应该像这样工作 -> 我插入名称,价格,然后我选择三个类别之一(书籍,硬驱动器和家具)然后根据它我选择一个新的插入物,例如书籍出现重量插入物出现家具三个插入物出现宽度长度和高度,它只有在我插入一个类别的所有值时才起作用(比如我还必须插入宽度长度和高度,另一个是大小,然后它会插入数据库,现在它根本不起作用,我不知道我的错误在哪里)

这是create_product.php表单(我使用 display none 然后 jquery 来显示哪些类别插入出现(也许这不是正确的方法?))

<?php
// include database and object files
include_once 'config/database.php';
include_once 'objects/product.php';
include_once 'objects/category.php';

// get database connection
$database = new Database();
$db = $database->getConnection();

// pass connection to objects
$product = new Product($db);
$category = new Category($db);

// set page headers
$page_title = "Create Product";
include_once "layout_header.php";

echo "<div class='right-button-margin'>";
    echo "<a href='index.php' class='btn btn-default pull-right'>Read Products</a>";
echo "</div>";

?>
<?php 
// if the form was submitted 
if($_POST){

    // set product property values
    $product->name = $_POST['name'];
    $product->price = $_POST['price'];
    $product->category_id = $_POST['category_id'];
    $product->size = $_POST['size'];
    $product->weight = $_POST['weight'];
    $product->lenght = $_POST['lenght'];
    $product->width = $_POST['width'];
    $product->height = $_POST['height'];


    // create the product
    if($product->create()){
        echo "<div class='alert alert-success'>Product was created.</div>";
    }

    // if unable to create the product, tell the user
    else{
        echo "<div class='alert alert-danger'>Unable to create product.</div>";
    }
}
?>

<!-- HTML form for creating a product -->


<script>
    // script for when you change the categories so that the appropriate input field appears
$(document).ready(function(){
    $('.form-control').on('change', function() {


      if ( this.value == 1)
      {
        $("#weight").show();
        $("#size").hide();
        $("#height").hide();
      }


      if ( this.value == 2)
      {
        $("#weight").hide();
        $("#size").show();
        $("#height").hide();

      }
      if ( this.value == 3)
      {
        $("#weight").hide();
        $("#size").hide();
        $("#height").show();

      }


    });
});

 </script>

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">

    <table class='table table-hover table-responsive table-bordered'>

        <tr>
            <td>Name</td>
            <td><input type='text' name='name' class='form-control' /></td>
        </tr>

        <tr>
            <td>Price</td>
            <td><input type='text' name='price' class='form-control' /></td>
        </tr>

    <div >
        <tr id="weight" style="display:none">

            <td >Weight</td>
            <td><input type="text" name='weight' class='form-control'></input></td>
        </tr>
    </div>


        <tr id="size" style="display:none">

            <td>Size</td>
            <td><input name='size' class='form-control'></input></td>
        </tr>

        <tr id="height" style="display:none">

            <td>Height</td>
            <td><input name='height' class='form-control'></input></td>
            <td>Length</td>
            <td><input name='lenght' class='form-control'></input></td>
            <td>Width</td>
            <td><input name='width' class='form-control'></input></td>
        </tr>

        <tr>
            <td>Category</td>
            <td>
            <?php
// read the product categories from the database
$stmt = $category->read();

// put them in a select drop-down
echo "<select class='form-control' name='category_id'>";
    echo "<option>Select category...</option>";

    while ($row_category = $stmt->fetch(PDO::FETCH_ASSOC)){
        extract($row_category);
        echo "<option value='{$id}'>{$name}</option>";
    }

echo "</select>";
?>
            </td>
        </tr>

        <tr>
            <td></td>
            <td>
                <button type="submit" class="btn btn-primary">Create</button>
            </td>
        </tr>

    </table>
</form>

<?php

// footer
include_once "layout_footer.php";
?>

这是product.php查询发生的地方

<?php
class Product{

    // database connection and table name
    private $conn;
    private $table_name = "products";

    // object properties
    public $id;
    public $name;
    public $price;
    public $category_id;
    public $size;
    public $weight;
    public $lenght;
    public $width;
    public $height;

    public function __construct($db){
        $this->conn = $db;
    }

    function create(){

        //write query
        $this->size = !empty($this->size) ? $this->size: null;
        $this->weight= !empty($this->weight) ? $this->weight : null;
        $this->lenght= !empty($this->lenght) ? $this->lenght : null;
        $this->width= !empty($this->width) ? $this->width : null;
        $this->height= !empty($this->height) ? $this->height : null;

        $query = "INSERT INTO
                        " . $this->table_name . "
                    VALUES
                        name=:name, price=:price,  category_id=:category_id, size=:size, weight=:weight, lenght=:lenght, width=:width,height=:height";

        $stmt = $this->conn->prepare($query);

        // bind values 
        $stmt->bindParam(":name", $this->name);
        $stmt->bindParam(":price", $this->price);
        $stmt->bindParam(":category_id", $this->category_id);
        $stmt->bindParam(":size", $this->size);
        $stmt->bindParam(":weight", $this->weight);
        $stmt->bindParam(":lenght", $this->lenght);
        $stmt->bindParam(":width", $this->width);
        $stmt->bindParam(":height", $this->height);

        if(!empty($stmt->execute())){
            return true;
        }else{
            return false;
        }

    }

}

?>

在我的数据库中,所有属性(大小、重量等)都设置为可以设置,NULL 因此不应该成为问题

标签: phpmysql

解决方案


推荐阅读