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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料