需要帮助使用php,ajax,javascript将数据插入数据库

时间:2017-02-15 13:25:27

标签: javascript php jquery mysql ajax

最近,我从互联网上拿了一些模板并试图理解代码。 但是当我尝试将数据插入数据库时​​,我遇到了问题,并且错误消息不会显示。 对不起,我的英文不好

这是我的,master_menu.php

<div class="form-group">
<label for="first_name">Nama Menu</label>
<input type="text" id="first_name" placeholder="Contoh : Ayam Goreng" class="form-control"/>
</div>

<div class="form-group">
<label for="last_name">Harga Pokok</label>
                                                <input type="text" id="last_name" placeholder="Contoh : 15000" class="form-control"/>
</div>

<div class="form-group">
<label for="email">Harga Jual</label>
<input type="text" id="email" placeholder="Contoh : 15000" class="form-control"/>
</div>

</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Batal</button>
<button type="button" class="btn btn-primary" onclick="addRecord()">Tambahkan Menu</button>
</div>

这是我的函数function_script_master.js

function addRecord() {
    // get values
    var first_name = $("#first_name").val();
    var last_name = $("#last_name").val();
    var email = $("#email").val();
    // Add record
    $.post("ajax/addRecord.php", {
        first_name: first_name,
        last_name: last_name,
        email: email
    }, function (data, status) {
        // close the popup
        $("#add_new_record_modal").modal("hide");

        //reload
        readRecords();

        // clear fields from the popup
        $("#first_name").val("");
        $("#last_name").val("");
        $("#email").val("");
    });
}

// READ records
function readRecords() {
    $.get("ajax/readRecords.php", {}, function (data, status) {
        $(".records_content").html(data);
    });
}

这是我的,addRecord.php

<?php
    if(isset($_POST['first_name']) && isset($_POST['last_name']) && isset($_POST['email'])){
        // include Database connection file 
        include("function_connection.php");
        alert('clcicked');
        // get values 
        $first_name = $_POST['first_name'];
        $last_name = $_POST['last_name'];
        $email = $_POST['email'];

        $query = "INSERT INTO MENUS(NAMA_MENU, HARGA_POKOK, HARGA_JUAL, STATUS) VALUES('$first_name', '$last_name', '$email', 'aktif')";
        if ($conn->query($query) === TRUE) {
            alert("Registrasi Sukses!");
        } else {
            alert("Username Yang Anda Inginkan Sudah Terpakai");
        }
        $conn->close();
        echo "1 Record Added!";
    }
?>

1 个答案:

答案 0 :(得分:1)

尝试下面的代码希望这会有所帮助,我只是用echo替换了警告。

<?php
    if(isset($_POST['first_name']) && isset($_POST['last_name']) && isset($_POST['email'])){
        // include Database connection file 
        include("function_connection.php");

        // get values 
        $first_name = $_POST['first_name'];
        $last_name = $_POST['last_name'];
        $email = $_POST['email'];

        $query = "INSERT INTO MENUS(NAMA_MENU, HARGA_POKOK, HARGA_JUAL, STATUS) VALUES('$first_name', '$last_name', '$email', 'aktif')";
        if ($conn->query($query) === TRUE) {
            echo "Registrasi Sukses!";
        } else {
            echo "Username Yang Anda Inginkan Sudah Terpakai";
        }
        $conn->close();
        echo "1 Record Added!";
    }
?>