skip to Main Content

This is update_action.php

/*this is for update_action.php (button name btnUpdateFaculty) */

if(isset($_REQUEST['btnUpdateFaculty']))
{
    $id = trim($_POST['id']);
    $date = trim($_POST['date']);
    $role = trim($_POST['role']);
    $description = trim($_POST['description']);
    $venue = trim($_POST['venue']);
    
    $sql = "UPDATE admin_faculty
    SET date='$date', role='$role', description='$description', venue='$venue'
    WHERE id='$id'";
            
    if (mysqli_query($conn, $sql)) {
        echo "Data saved: Record updated successfully";
    } else {
        echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }
} else {
    echo "Date not saved";
}



/*this is for update_action.php (button name btnUpdateUniversity)*/

 if(isset($_REQUEST['btnUpdateUni']))
 {
    $id = trim($_POST['id']);
    $date = trim($_POST['date']);
    $role = trim($_POST['role']);
    $description = trim($_POST['description']);
    $venue = trim($_POST['venue']);
    
    $sql = "UPDATE admin_university
    SET date='$date', role='$role', description='$description', venue='$venue'
    WHERE id='$id'";
            
    if (mysqli_query($conn, $sql)) {
        echo "Data saved: Record updated successfully";
    } else {
        echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }
} else {
    echo "Date not saved";
}

This is my Ajax/Jquery code

 /* This is for ModalFaculty */
$('.btnUpdateFaculty').on('click', function(){
    $('#ModalFaculty').modal('show');
        $tr = $(this).closest('tr');

        var data = $tr.children('td').map(function(){
            return $(this).text();
        }).get();

    console.log(data);

    $('#Edit_id').val(data[0]);
    $('#Edit_date').val(data[1]);
    $('#Edit_role').val(data[2]);
    $('#Edit_description').val(data[3]);
    $('#Edit_venue').val(data[4]);
});

$('#FormFaculty').on('submit', function(e){
    e.preventDefault();

    $.ajax({
        type: 'POST',
        url: 'update_action.php',
        data: $('#FormFaculty').serialize(),
        success: function(response){
            console.log(response);
            $('#ModalFaculty').modal('hide');
            alert(response);

            location.reload();
        },
        error: function(error){
            console.log(error);
            alert('Data Not Saved');
        }
    });
});


/* This is for ModalUniversity */
$('.btnUpdateUni').on('click', function(){
    $('#ModalUni').modal('show');
        $tr = $(this).closest('tr');

        var data = $tr.children('td').map(function(){
            return $(this).text();
        }).get();

    console.log(data);

    $('#Edit_idUni').val(data[0]);
    $('#Edit_dateUni').val(data[1]);
    $('#Edit_roleUni').val(data[2]);
    $('#Edit_descriptionUni').val(data[3]);
    $('#Edit_venueUni').val(data[4]);
});

$('#FormUni').on('submit', function(e){
    e.preventDefault();

    $.ajax({
        type: 'POST',
        url: 'update_action.php',
        data: $('#FormUni').serialize(),
        success: function(response){
            console.log(response);
            $('#ModalUni').modal('hide');
            alert(response);

            location.reload();
        },
        error: function(error){
            console.log(error);
            alert('Data Not Saved');
        }
    });
});

this is my html modal bootstrap file

<!-- Update ModalFaculty -->
<div id="ModalFaculty" class="modal fade">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <!-- Modal Header -->
            <div class="modal-header">
                <h4 class="modal-title">Edit Details</h4>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>
            <!-- Modal body -->
            <div class="modal-body">
                <form id="FormFaculty" class="form-group modal1">
                    <input id="Edit_id" name="id"  value="<?php echo $row['id']; ?>" hidden><br>
                    <div class="container"><div class="row">
                        <label for="date">Previous date: &nbsp;</label>
                        <input id="Edit_date" type="text" name="date" class="form-control" style="width: 35% !important" value="" readonly required>
                        <label for="date" style="margin-left: 40px">New date: &nbsp;</label>
                        <input id="Edit_date" type="date" name="date" class="form-control" style="width: 35% !important" required><br>
                    </div></div>
                    <label for="role">Role:</label><br>
                    <input id="Edit_role" type="text" name="role" class="form-control" value="<?php echo $row['role']; ?>" required><br>
                    <label for="description">Description:</label><br>
                    <input id="Edit_description" type="text" name="description" class="form-control" value="<?php echo $row['description']; ?>" required><br>
                    <label for="venue">Venue:</label><br>
                    <input id="Edit_venue" type="text" name="venue" class="form-control" value="<?php echo $row['venue']; ?>" required><br>
                    <button type="submit" name="btnUpdateFaculty" class="btn btn-success">Update</button>
                </form>
            </div>
            <!-- Modal footer -->
            <div class="modal-footer">
                <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>
<!-- Update ModalUni -->
<div id="ModalUni" class="modal fade">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <!-- Modal Header -->
            <div class="modal-header">
                <h4 class="modal-title">Edit Details</h4>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>
            <!-- Modal body -->
            <div class="modal-body">
                <form id="FormUni" class="form-group">
                    <input id="Edit_idUni" name="id"  value="<?php echo $row['id']; ?>" hidden><br>
                    <div class="container"><div class="row">
                        <label for="date">Previous date: &nbsp;</label>
                        <input id="Edit_dateUni" type="text" name="date" class="form-control" style="width: 35% !important" value="" readonly required>
                        <label for="date" style="margin-left: 40px">New date: &nbsp;</label>
                        <input id="Edit_dateUni" type="date" name="date" class="form-control" style="width: 35% !important" required><br>
                    </div></div>
                    <label for="role">Role:</label><br>
                    <input id="Edit_roleUni" type="text" name="role" class="form-control" value="<?php echo $row['role']; ?>" required><br>
                    <label for="description">Description:</label><br>
                    <input id="Edit_descriptionUni" type="text" name="description" class="form-control" value="<?php echo $row['description']; ?>" required><br>
                    <label for="venue">Venue:</label><br>
                    <input id="Edit_venueUni" type="text" name="venue" class="form-control" value="<?php echo $row['venue']; ?>" required><br>
                    <button type="submit" name="submit" class="btn btn-success">Update</button>
                </form>
            </div>
            <!-- Modal footer -->
            <div class="modal-footer">
                <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

At first, the update is success. When in update_action.php only has update statement for admin_faculty, but when I try to put another update statement in update_action.php, the process failed. I think maybe I got stuck at $_REQUEST / $_POST in update_action.php, but I am not sure which part is it. I got error "No data saved." Can someone help me which code I wrote it wrong?

This is the screenshot of error I got

This is the screenshot of error I got

2

Answers


  1. Hey can you change this line

                 <button type="submit" name="submit" class="btn btn-success">Update</button>
    

    to

                    <button type="submit" name="btnUpdateUni" class="btn btn-success">Update</button>
    

    name is not appear correctly in post array that is the issue.

    Login or Signup to reply.
  2. HTML

    The values in the ID attributes must be unique through the whole page. This is why your value assignment in jQuery does not work properly. You could change the ids or use classes like class="edit-date" instead of duplicate IDs like id="Edit_Date". The same applies to input elements and the name attribute in a single form

    <div class="container">
      <div class="row">
        <label for="date">Previous date: &nbsp;</label>
        <input id="edit-previous-date" type="text" name="date" class="form-control" value="" readonly required>
        <label for="date" style="margin-left: 40px">New date: &nbsp;</label>
        <input id="edit-new-date" type="date" name="date" class="form-control" required><br>
      </div>
    </div>
    

    jQuery

    Also, the value of your submit button "btnUpdateFaculty" is not transmitted by serialize(). Just use an input element with the attribute type="hidden". More Informations here, if you want to keep the button as the value holder Ajax post serialize() does not include button name and value

    <input type="hidden" name="btnUpdateFaculty" value="" />
    <button type="submit" class="btn btn-success">Update</button>
    

    PHP

    And don’t forget to check your variables in php before putting it directly in the sql query because of sql injections. How can I prevent SQL injection in PHP?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search