简体   繁体   English

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

[英]Need help inserting data to database using php,ajax,javascript

Recently, i take some template from the internet and trying to understand the code. 最近,我从互联网上获取了一些模板,并试图理解代码。 But i got stuck when trying to insert data to the database and the error message wont show. 但是,当我尝试向数据库中插入数据时,我被卡住了,错误消息不会显示。 Im sorry for my bad English 对不起我的英语不好

This is my piece of, master_menu.php 这是我的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>

And this is my piece of, function_script_master.js 这是我的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);
    });
}

And this is my piece of, addRecord.php 这是我的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!";
    }
?>

Try below code hope this helps, i just replaced alert with echo. 尝试下面的代码希望对您有帮助,我只是将echo替换为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!";
    }
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM