1

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.

Community
  • 1
  • 1
  • 1
    You should add a description on what exactly the error/problem is. – チーズパン Nov 23 '15 at 10:54
  • I have added a description of the exact problem. I have a feeling that there may be something wrong with the script, I'm not too sure to be honest because everything looks fine, but still doesn't work. Help please. – Prasad Acharya Nov 23 '15 at 11:34

2 Answers2

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);
user5542121
  • 1,051
  • 12
  • 28
  • I tried it, but it still doesn't work. I have edited the post and added the button where it is called. Is there something wrong with the calling of the script? Or is there something else that I'm missing? – Prasad Acharya Nov 23 '15 at 10:49
  • not 100% sure, but according to this http://php.net/manual/de/pdostatement.bindparam.php you can't bind multiple ones in one go. I updated the solution. – user5542121 Nov 23 '15 at 10:56
  • Unfortunately that still doesn't work. Anything else I could possibly try to make the update work? – Prasad Acharya Nov 23 '15 at 11:11
  • http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display You should enable error displaying, that might help narrow the error down. – user5542121 Nov 23 '15 at 11:11
  • Unfortunately, enabling error displaying, shows no error. – Prasad Acharya Nov 23 '15 at 11:43
  • are you 100% sure errors are being displayed when they happen? E.g. did you create an error and check if it gets displayed? – user5542121 Nov 23 '15 at 12:28
0

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>';