Connecting AJAX to MySQL (PHP)

368
November 28, 2016, at 8:37 PM

I have an assignment due which is related to PHP, MySQL, AJAX and JQuery. Currently I am experiencing a problem with a script and I hope someone can help me out. The script is used to update the fields in MySQL. The script doesn't seem to be working. Here is the script:

function updatedata(id) {
    var id = id;
    var name = $('#name' + id).val() ;
    var url = $('#url' + id).val() ;
    var imageurl = $('#imageurl' + id).val() ;
    var description = $('#description' + id).val() ;
    var datas = "name=" + name + "&url=" + url + "&imageurl=" + imageurl + "&description=" + description;
    $.ajax({
        type: "POST",
        url: "update.php?id=" + id;
        data: datas;
    }).done(function (data) {
        $('#info').html(data);
        viewdata();
    });
}

The following is the code that is contained in the 'update.php' file:

<?php
    include ('header.php');
    if(isset($_GET['id'])) {
        $stmt = $connection->prepare("UPDATE news_source SET name=?, url=?, description=?, imageurl=? where id=?");
        $stmt->bind_param('sssss', $full_name, $url_1, $descript_ion, $image_url, $id);
        $full_name = mysqli_real_escape_string($connection, $_POST ['name']);
        $url_1 = mysqli_real_escape_string($connection, $_POST ['url']);
        $descript_ion = mysqli_real_escape_string($connection, $_POST ['description']);
        $image_url = mysqli_real_escape_string($connection, $_POST ['imageurl']);
        $id = $_GET ['id']);
        if($stmt->execute()))
        { ?>
            <div class = "alert alert-success alert-dismissible" role="alert">
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true"> &times; </span>
                </button>
                <strong>Success!</strong> 
                Record has been added.
            </div>
        <?php } else { ?>
            <div class = "alert alert-danger alert-dismissible" role="alert">
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true"> &times; </span>
                </button>
                <strong>Error!</strong> 
                Record failed to add.
            </div>
        <?php }
    include ('footer.php');
?>

EDIT: The script is called in the same php file, 'maintain.php' as:

<div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" onclick="updatedata('<?php echo $rows['id']; ?>')" class="btn btn-primary">Save changes</button>
      </div>

EDIT:

Description of the problem: Alright so, the issue is that I am unable to update the fields in the MySQL database. The main file should allow the user to edit the data that exists in the Bootstrap Modal, and then runs an AJAX to dynamically update the database without refreshing the page. The main file, for now, only shows the data that is available to edit, but I am unable to update the data in the database. Once I click on the "Save Changes" button, nothing happens.

Note: I have tried to use the information that is present in How do I get PHP Errors to display? to display the php error, but unfortunately it displays nothing.

Answer 1

You are binding the params and then escaping them. First get the params and escape, then bind them And you didn't sanitize the ID.

$full_name = mysqli_real_escape_string($connection, $_POST ['name']);
$url_1 = mysqli_real_escape_string($connection, $_POST ['url']);
$descript_ion = mysqli_real_escape_string($connection, $_POST ['description']);
$image_url = mysqli_real_escape_string($connection, $_POST ['imageurl']);
$id = intval($_GET ['id']));
$stmt->bind_param(1, $full_name);
$stmt->bind_param(2, $url_1);
$stmt->bind_param(3, $descript_ion);
$stmt->bind_param(4, $image_url);
$stmt->bind_param(5, $id);
Answer 2

Echo your HTML Div(alert modal). Something like this:

echo '<div class = "alert alert-success alert-dismissible" role="alert">';
            echo '<button type="button" class="close" data-dismiss="alert" aria-label="Close">';
                echo '<span aria-hidden="true"> &times; </span>';
            echo '</button>';
            echo '<strong>Success!</strong>'; 
            echo 'Record has been added.';
        echo '</div>';
READ ALSO
How to access variables of Jquery in FTL?

How to access variables of Jquery in FTL?

I am using FTL very first timeI want to access ancor tagfrom this code in jquery within FTL file and want to give style for table through jQuery

511
Select2 not selecting first item as default

Select2 not selecting first item as default

I am populating a drop down list in an ASPNet page using this code

540