drnbotxo449747 2018-05-21 09:16
浏览 49
已采纳

表格不会在数据库中提交

everything seems fine for me, but the form won't submit in my database. I don't know what else to do, i don't get any errors or anything, so i am really confused. I am missing something? This is getting me really obsessed

Here's my insert function:

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

So here's the the main code. The form looks fine for me, i don't know what went wrong in here

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

My products table contains:

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条回答 默认 最新

  • dsla94915 2018-05-21 09:40
    关注

    Please check your DB table column size. You have define the column size for active and condition is just 2 length. But when run the query then passing value size for the corresponding field is more than that. Another issue field name condition change to any other name. You should not use " " for the integer type field also.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示