2017-05-28 109 views
1

我试图添加另一个字段名称“hospital_name”。每当我添加一些字段并尝试添加新记录时,它总是失败并且不会出现在我的列表中。简单Crud使用AJAX/JQUERY

下面是添加记录的代码

<?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代码

<?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()); 
    } 
} 

我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 
}); 

添加行模式

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

更新用户模式

<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> 
+0

我试图手动添加一些数据在我的数据库和我的应用程序检索它。 – lenewb

回答

0

您MySQL查询没有医院列。下面的查询应该有医院名称列

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

非常感谢!霍里! 5小时后疼痛哈哈 – lenewb

+0

哈哈.. 5小时?真的吗?!?很抱歉地说,但是很愚蠢的错误。总之,祝你好运。 :) 您可以勾选,投票并关闭此问题。 –

+0

试图转发我的js代码。 ahaha!非常感谢! – lenewb