weixin_33690963 2017-05-28 11:03 采纳率: 0%
浏览 69

使用AJAX / JQUERY的简单Crud

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>
  • 写回答

1条回答 默认 最新

  • weixin_33736649 2017-05-28 11:10
    关注

    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')";
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器