【问题标题】:Database Connected But Data is not Inserted Into it [duplicate]已连接数据库但未将数据插入其中[重复]
【发布时间】:2018-09-17 04:05:44
【问题描述】:
<?php
//index.php

$connect = new PDO("mysql:host=localhost;dbname=sales", "root", "");

function e_type($connect) {
    $output1 = '';
    $query = "SELECT * FROM elimo_type ORDER BY type ASC";
    $statement = $connect->prepare($query);
    $statement->execute();
    $result = $statement->fetchAll();
    foreach ($result as $row) {
        $output1 .= '<option value="' . $row["type"] . '">' . $row["type"] . '</option>';
    }
    return $output1;
}

function hw_type($connect) {
    $output2 = '';
    $query = "SELECT * FROM hw_version ORDER BY type ASC";
    $statement = $connect->prepare($query);
    $statement->execute();
    $result = $statement->fetchAll();
    foreach ($result as $row) {
        $output2 .= '<option value="' . $row["type"] . '">' . $row["type"] . '</option>';
    }
    return $output2;
}

function sw_type($connect) {
    $output3 = '';
    $query = "SELECT * FROM sw_version ORDER BY type ASC";
    $statement = $connect->prepare($query);
    $statement->execute();
    $result = $statement->fetchAll();
    foreach ($result as $row) {
        $output3 .= '<option value="' . $row["type"] . '">' . $row["type"] . '</option>';
    }
    return $output3;
}
?>
<?php
include 'header.php';
?>
<div class="container">
    <h3 align="center">Purchase</h3>
    <br />
    <h4 align="center">Enter Purchase Details</h4>
    <br />
    <form method="post" id="insert_form">
        <div class="table-repsonsive">
            <span id="error"></span>
            <table class="table table-bordered" id="item_table">
                <tr>
                    <th>Serial No</th>
                    <th>Type</th>
                    <th>Hardware Version</th>
                    <th>Software Version</th>
                    <th>Key</th>
                    <th><button type="button" name="add" class="btn btn-success btn-sm add"><span class="glyphicon glyphicon-plus"></span></button></th>
                </tr>
            </table>
            <div align="center">
                <input type="submit" name="submit" class="btn btn-info" value="Insert" />
            </div>
        </div>
    </form>
</div>
</body>
</html>

<script>
    $(document).ready(function () {

        $(document).on('click', '.add', function () {
            var html = '';
            html += '<tr>';
            html += '<td><input type="text" name="serial_no[]" class="form-control serial_no" /></td>';
            html += '<td><select name="e_type[]" class="form-control e_type"><option value="">Select Type</option><?php echo e_type($connect); ?></select></td>';
            html += '<td><select name="hw_type[]" class="form-control hw_type"><option value="">Select Hardware Version</option><?php echo hw_type($connect); ?></select></td>';
            html += '<td><select name="sw_type[]" class="form-control sw_type"><option value="">Select Software Version</option><?php echo sw_type($connect); ?></select></td>';

            html += '<td><input type="text" name="key[]" class="form-control key" /></td>';
            html += '<td><button type="button" name="remove" class="btn btn-danger btn-sm remove"><span class="glyphicon glyphicon-minus"></span></button></td></tr>';
            $('#item_table').append(html);
        });

        $(document).on('click', '.remove', function () {
            $(this).closest('tr').remove();
        });

        $('#insert_form').on('submit', function (event) {
            event.preventDefault();
            var error = '';
            $('.serial_no').each(function () {
                var count = 1;
                if ($(this).val() == '')
                {
                    error += "<p>Enter Serial no at " + count + " Row</p>";
                    return false;
                }
                count = count + 1;
            });

            $('.e_type').each(function () {
                var count = 1;
                if ($(this).val() == '')
                {
                    error += "<p>Select Type at " + count + " Row</p>";
                    return false;
                }
                count = count + 1;
            });

            $('.hw_type').each(function () {
                var count = 1;
                if ($(this).val() == '')
                {
                    error += "<p>Select Hardware Version  at " + count + " Row</p>";
                    return false;
                }
                count = count + 1;
            });

            $('.sw_type').each(function () {
                var count = 1;
                if ($(this).val() == '')
                {
                    error += "<p>Select Software Version  at " + count + " Row</p>";
                    return false;
                }
                count = count + 1;
            });

            $('.key').each(function () {
                var count = 1;
                if ($(this).val() == '')
                {
                    error += "<p>Enter Key at " + count + " Row</p>";
                    return false;
                }
                count = count + 1;
            });
            var form_data = $(this).serialize();
            if (error == '')
            {
                $.ajax({
                    url: "insert.php",
                    method: "POST",
                    data: form_data,
                    success: function (data)
                    {
                        if (data == 'ok')
                        {
                            $('#item_table').find("tr:gt(0)").remove();
                            $('#error').html('<div class="alert alert-success">Purchase Details Saved</div>');
                        }
                    }
                });
            } else
            {
                $('#error').html('<div class="alert alert-danger">' + error + '</div>');
            }
        });

    });
</script>
<?php
//insert.php;

if (isset($_POST["serial_no"])) {
    $connect = new PDO("mysql:host=localhost;dbname=sales", "root", "");
    $id = uniqid();
    for ($count = 0; $count < count($_POST["serial_no"]); $count++) {
        $query = "INSERT INTO elimo_purchase 
      (id,serial_no, e_type, hw_type, sw_type,key) 
      VALUES (:id,:serial_no, :e_type, :hw_type, :sw_type,:key)";
        $statement = $connect->prepare($query);
        $statement->execute(
                array(
                    ':id' => $id,
                    ':serial_no' => $_POST["serial_no"][$count],
                    ':e_type' => $_POST["e_type"][$count],
                    ':hw_type' => $_POST["hw_type"][$count],
                    ':sw_type' => $_POST["sw_type"][$count],
                    ':key' => $_POST["key"][$count]
                )
        );
    }
    $result = $statement->fetchAll();
    if (isset($result)) {
        echo 'ok';
    }
}
?>

我在保存购买详细信息时得到输出,但结果未存储到数据库中。

【问题讨论】:

  • fetchAll() 不能用于INSERT 语句,它只能与SELECT 一起使用。
  • 您不需要在循环内准备语句。准备一次,然后用不同的参数数组循环调用execute()
  • 判断INSERT是否成功,检查$statement-&gt;execute()的返回值
  • 请通过代码说明
  • 函数包含选择语句

标签: php jquery pdo


【解决方案1】:

您需要按照以下方式进行调试,

  1. 检查您在插入查询中传递的所有值 print all 并检查值。
  2. 然后打印查询并手动触发相同的查询并检查它是否已插入

【讨论】:

    【解决方案2】:

    当您执行INSERT 查询时,您不能使用fetch 函数,因为它不返回任何数据。这只能与SELECT 一起使用。你需要检查$statement-&gt;execute()的结果。

    if(isset($_POST["serial_no"]))
    {
        $connect = new PDO("mysql:host=localhost;dbname=sales", "root", "");
        $id = uniqid();
        $query = "INSERT INTO elimo_purchase (id,serial_no, e_type, hw_type, sw_type,key) 
                  VALUES (:id,:serial_no, :e_type, :hw_type, :sw_type,:key)";
        $statement = $connect->prepare($query);
        for($count = 0; $count < count($_POST["serial_no"]); $count++)
        {  
            if (!$statement->execute(
                    array(
                        ':id'   => $id,
                        ':serial_no'   => $_POST["serial_no"][$count],
                        ':e_type'  => $_POST["e_type"][$count], 
                        ':hw_type' => $_POST["hw_type"][$count], 
                        ':sw_type' => $_POST["sw_type"][$count], 
                        ':key'  => $_POST["key"][$count]
                        )
                    )) {
                die('not ok');
            }
        }
        echo 'ok';
    }
    

    【讨论】:

    • 不,现在它根本不起作用。
    • e_type,hw_type 和 sw_type 是另一个表的数据
    • 我以为它们来自index.php的表格。
    猜你喜欢
    • 2018-09-15
    • 2014-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-03
    • 2020-04-17
    • 2013-04-15
    • 2012-12-06
    相关资源
    最近更新 更多