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条回答 默认 最新

  • weixin_33720078 2015-11-23 10:30
    关注

    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);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 u盘问题:盘符不显示 无媒体
  • ¥50 R语言读取nc按月均值转为tif
  • ¥30 智能车串级pid调参
  • ¥15 visual studio code翻译老是错误
  • ¥20 卫星测高数据的高程转换
  • ¥15 爬取招聘网站数据信息
  • ¥15 安装完tensorflow,import tensorflow as tf后报错,如何解决?
  • ¥15 ultralytics库导出onnx模型,模型失去预测能力
  • ¥15 linux下点对点协议连接2个USB串口的硬件流量控制问题
  • ¥15 SQL数据自动生成问题