weixin_33726318 2015-11-23 10:21 采纳率: 0%
浏览 10

将AJAX连接到MySQL(PHP)

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.

  • 写回答

2条回答

      报告相同问题?

      悬赏问题

      • ¥15 GEO下载数据的处理报错 :函数‘Meta’标签‘"data.frame"’找不到继承方法,如何解决?
      • ¥15 DLNM模型是否可以用二分类变量作为y变量
      • ¥15 android object box 一个实体多个表怎么写
      • ¥15 temux 启用docker 服务失败
      • ¥15 Flask 使用celery发送邮件出现‘目标计算机积极拒绝‘
      • ¥60 老人用的sd卡在手机里面不知道操作了什么,导致图片和视频变成了文件,取下sd卡连接电脑就是图中的样子,后缀改为.jpg才可以,需要用系统的画图软件才能打开,文件属性还是文件,有没有批量操作的解决办法
      • ¥15 超时跳出方法代码的返回值问题
      • ¥15 汇编语言程序设计设计,ascii码求数,再求数的BCD码
      • ¥30 Mask rcnn训练自己的数据集出现问题!
      • ¥20 研究人工智能时的几个问题