简体   繁体   中英

Simple Crud Using AJAX/JQUERY

Im trying to add another field name "hospital_name". Every time I add some fields it and try to add new record, it always failed and doesnt appear on my list.

Here's the code for the add Record

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

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

    $query = "INSERT INTO users(first_name, last_name, email, work) VALUES('$first_name', '$last_name', '$email', '$work', '$hospital_name')";
    if (!$result = mysql_query($query)) {
        exit(mysql_error());
    }
    echo "1 Record Added!";
}

?>

Updateuserdetail code

<?php
// include Database connection file
include("db_connection.php");

// check request
if(isset($_POST))
{
    // get values
    $id = $_POST['id'];
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $email = $_POST['email'];
    $work = $_POST['work'];
    $hospital_name = $_POST['hospital_name'];
    // Updaste User details
    $query = "UPDATE users SET first_name = '$first_name', last_name = '$last_name', email = '$email', work = '$work', hospital_name = '$hospital_name'
     WHERE id = '$id'";
    if (!$result = mysql_query($query)) {
        exit(mysql_error());
    }
}

my scriptjs

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

        // read records again
        readRecords();

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

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


function DeleteUser(id) {
    var conf = confirm("Are you sure, do you really want to delete User?");
    if (conf == true) {
        $.post("ajax/deleteUser.php", {
                id: id
            },
            function (data, status) {
                // reload Users by using readRecords();
                readRecords();
            }
        );
    }
}

function GetUserDetails(id) {
    // Add User ID to the hidden field for furture usage
    $("#hidden_user_id").val(id);
    $.post("ajax/readUserDetails.php", {
            id: id
        },
        function (data, status) {
            // PARSE json data
            var user = JSON.parse(data);
            // Assing existing values to the modal popup fields
            $("#update_first_name").val(user.first_name);
            $("#update_last_name").val(user.last_name);
            $("#update_email").val(user.email);
            $("#update_work").val(user.work);
            $("#update_hospital_name").val(user.hospital_name);
        }
    );
    // Open modal popup
    $("#update_user_modal").modal("show");
}

function UpdateUserDetails() {
    // get values
    var first_name = $("#update_first_name").val();
    var last_name = $("#update_last_name").val();
    var email = $("#update_email").val();
    var work = $("#update_work").val();
    var work = $("#update_hospital_name").val();

    // get hidden field value
    var id = $("#hidden_user_id").val();

    // Update the details by requesting to the server using ajax
    $.post("ajax/updateUserDetails.php", {
            id: id,
            first_name: first_name,
            last_name: last_name,
            email: email,
            work: work,
            update_hospital_name
        },
        function (data, status) {
            // hide modal popup
            $("#update_user_modal").modal("hide");
            // reload Users by using readRecords();
            readRecords();
        }
    );
}

$(document).ready(function () {
    // READ recods on page load
    readRecords(); // calling function
});

Add row modal

<div class="form-group">
                <label for="hospital_name">hospital name</label>
                <input type="text" id="hospital_name" placeholder="hospital name" class="form-control"/>
            </div>

update user modal

<div class="form-group">
                    <label for="update_hospital_name">Hospital Name</label>
                    <input type="text" id="update_hospital_name" placeholder="Hospital Name" class="form-control"/>
                </div>

You MySQL query doesn't have hospital column. Below query should have hospital name column

$query = "INSERT INTO users(first_name, last_name, email, work, MISSING_COLUMN) VALUES('$first_name', '$last_name', '$email', '$work', '$hospital_name')";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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