表格不会在数据库中提交

时间:2018-05-21 09:16:03

标签: php html mysqli web-development-server

对我来说一切似乎都很好,但表格不会在我的数据库中提交。我不知道还能做什么,我没有得到任何错误或任何东西,所以我真的很困惑。我错过了什么?这让我非常着迷

这是我的插入功能:

function insertDb($table, $data, $validation = null) {
    global $link;

    foreach($data as $key => $value) {
        $data[$key] = trim($value);
    }

    if(!empty($validation)) {
        foreach($validation as $field => $rules) {
            foreach($rules as $rule_type => $rule_value) {
                if($rule_type == 'unique') {
                    $check_exists = find('first', $table, array(array('field' => $field, 'operator' => '=', 'value' => $data[$field])));
                    if(!empty($check_exists)) {
                        $errors['error'][$field][$rule_type] = $field.' must be unique';
                    }
                } elseif($rule_type == 'min_lenght') {
                    if(strlen($data[$field]) < $rule_value) {
                        $errors['error'][$field][$rule_type] = $field.' requires at least ' . $rule_value . ' characters';
                    }
                }
            }
        }
        if(isset($errors)) {
            return $errors;
        }
    }

    foreach($data as $key => $value) {
        $insert_fields[] = $key;
        $insert_values[] = '"' . $value . '"';
    }

    $query = 'INSERT INTO ' . $table. ' ('.implode(',', $insert_fields).') VALUES ('.implode(',', $insert_values).')';
    mysqli_query($link, $query);

    return true;
}

所以这是主要代码。表格对我来说很好,我不知道这里出了什么问题

 <?php
    include "configuration.php";

    if(isset($_POST['submit'])) {
        $result = insertDb('products',
            array('category' => $_POST['name'],
                'name' => $_POST['name'],
                'price' => $_POST['price'],'active' => $_POST['active'],
                'condition' => $_POST['condition']));

        if($result === true) {
            header("Location: products.php");
        }
    }
    ?>


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <head>
        <style type="text/css"><?php include 'style.css'; ?></style>
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <style type="text/css">
            <?php include 'style.css'; ?>
        </style>
    </head>
    <body>
    <?php include "header.php";?>
    <div class="container">
        <form action="" method="post">
            <h3>Products:</h3>

            <div class="form-group">
                <label for="category">Category: *</label>
                <select name="category">

               <?php
               $res = mysqli_query($link,"select name from categories");
                while (  $row=mysqli_fetch_assoc($res)) {

                    echo "<option>";
                    echo $row["name"];
                    echo "</option>";
                } ?>

                </select>
            </div>

            <div class="form-group">
                <label for="name">Name: *</label>
                <input type="text" name="name" id="name" value="<?php echo @$_POST['name']; ?>" />
            <br/>
            </div>

            <div class="form-group">
                <label for="price">Price: *</label>
                <input type="text" name="price" id="price" value="<?php echo @$_POST['price']; ?>" />
            </div>

            <div class = "form-group">
                <label for="active">Active: *</label>
                <input type="checkbox" name="active" id="active" value="active" />
            </div>

            <div class = "form-group">
                <label for = "condition">Condition: *</label>
                <input type = "radio" name="condition" id="used" value="used" /><label for="used">Used</label>
                <input type = "radio" name="condition" id="new" value="new" /><label for="new">New</label>
            </div>

            <input type="submit" name="submit" value="Add">

        </form>
    </div>
    </body>
    </html>

我的产品表包含:

CREATE TABLE `products` (
                  `id` int(11) NOT NULL AUTO_INCREMENT,
                  `category` varchar(255) DEFAULT NULL,
                  `name` varchar(255) DEFAULT NULL,
                  `price` int(11) DEFAULT NULL,
                  `active` varchar(2) DEFAULT NULL,
                 `condition` varchar(2) DEFAULT NULL,
                  PRIMARY KEY (`id`)
                )

1 个答案:

答案 0 :(得分:0)

请检查数据库表的列大小。您已为活动列定义列大小,条件仅为2长度。但是当运行查询时,然后传递相应字段的值大小不止于此。另一个问题字段名称condition更改为任何其他名称。你不应该使用&#34; &#34;对于整数类型字段也是。