将数据插入mysql数据库的PHP代码不起作用

时间:2016-11-22 07:46:47

标签: php mysql ajax

我试图在不使用ajax和php刷新页面的情况下插入数据,但数据没有插入到数据库中。

这是我的代码

<?php
require 'validation_class.php';
$obj_menufacture = new Validation_Class();

if (isset($_POST['btn'])) {
    $menufacture = $obj_menufacture->menufacture_add($_POST);
}
?>
<!DOCTYPE html>
<html>
    <head>
        <script src='js/jquery-3.0.0.js'></script>
    </head>
    <body>
        <div id='form_id'>
            <form action='' method='post'>
                <input type='text' class='m_name' name='m_name' id='m_name' onchange="names('m_name')" onkeyup="names('m_name')"/>
                <span style='color:red;' id='m_name_error'></span>
                <input type="submit" name='btn' value="submit" id='save'/>
            </form>
        </div>
        <div id='cool'></div>
        <style>
            .border_input{
                border-color:red;
            }
            .border_input1{
                border-color:green;
            }
        </style>
        <script>
            $(document).ready(function () {
                $('#save').on('click', function (e) {
                    e.preventDefault();

                    var m_name = $.trim($('#m_name').val());
                    var dataString = 'name='+ m_name;
                    if (m_name === '') {
                        if (m_name == '') {
                            $('#m_name_error').html('Please enter your name');
                            $('#m_name').addClass('border_input');

                        }
                    } else {
                        $.ajax({
                            url: 'validation.php',
                            method: 'post',
                            data:dataString,
                            success: function () {
                                $("#form_id").slideUp("slow");

                            }
                        });
                    }

                });

            });

            function names(id) {
                var val = $.trim($('#' + id).val());
                if (val === '') {
                    $('#' + id + '_error').html('');
                    $('#' + id).addClass('border_input');
                } else {
                    $('#' + id + '_error').html('');
                    $('#' + id).addClass('border_input1');
                }
            }

        </script>
    </body>
</html>

这是我的插入代码

<?php

class Validation_Class{

    public $link;

    public function __construct() {
        $HOST = "localhost";
        $USER = "root";
        $PASS = "";
        $DATABASE = "store";
        $this->link = mysqli_connect($HOST, $USER, $PASS, $DATABASE);
        if (!$this->link) {
            die('database query problem' . mysqli_error($this->link));
        }
    }

    public function menufacture_add($data) {
        $sql = "INSERT INTO menufacture(m_name)VALUES('$data[m_name]')";
        if (mysqli_query($this->link, $sql)) {
            $menufacture = "Menufacture insert successfully";
            return $menufacture;
        } else {
            die('menufacruere query problem' . mysqli_error($this->link));
        }
    }


}

请帮我解决这个问题

2 个答案:

答案 0 :(得分:0)

  <form  name="signup" id="reg" action="" method="post"enctype="multipart/form-data">
    <input type='text' class='m_name' name='m_name' id='m_name' onchange="names('m_name')" onkeyup="names('m_name')"/>
    <span style='color:red;' id='m_name_error'></span>
    <input type="submit" name='btn' value="submit" id='save'/>
</form>
            <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
            <script>
                $("#save").submit(function () {
                    var form_data = $('#reg').serialize();
                    $.ajax({
                        type: "POST",
                        url: 'setting.php',
                        data: $('#reg').serialize(),
                        success: function (data) {
                            alert('Data save Successfully');
                        }, error: function () {
                            alert('Some Internal Error.');
                        }
                    });
                });`
            </script>

<强> settting.php

            <?php

            include("db_connection.php"); // include connection file
            if(isset($_POST)) {
            $fname = $_POST['m_name'];
            $insert = "INSERT INTO `table_name`(`column_name`)VALUES('".$fname."')";
            $insertion = mysqli_query($conn, $insert); 
            }

            ?>

答案 1 :(得分:0)

              $("#save").on("click", function () {
              var form_data = $('#reg').serialize();
            $.ajax({
                type:"POST",

                 url:'setting.php',
                data: $('#reg').serialize(),

                success: function(data){

                  alert("Data save Successfully");  

                },error: function () {
                alert('Some Internal Error.');
            }
          });

            });