weixin_33713503 2017-06-20 14:01 采纳率: 0%
浏览 5

使用AJAX更新DB ROW

i am troubleshooting with update row using AJAX. When i click submit button, the value of poke increases +1 and i need it to update in DB also. Everything needs to work with AJAX. Currently my code works only +1 to poke input with ajax but DB row still not updating. HTML:

 <?php
                    $result = $mysqli->query("SELECT * FROM users") or die($mysqli->error);
                    while ($users = $result->fetch_assoc()) {
                        ?>
                        <form class="table" name="table" id="table" method="post" enctype="multipart/form-data" autocomplete="off" data-counter="<?php echo $users['id']?>">
                            <div class="Table-row" id="table">
                                <div class="Table-row-item" data-header="Header1"><input class="clear" type="text" name="first_name" id="first_name" value="<?php echo $users['first_name'] ?>"></div>
                                <div class="Table-row-item" data-header="Header2"><input class="clear" type="text" name="last_name" id="last_name" value="<?php echo $users['last_name'] ?>"></div>
                                <div class="Table-row-item" data-header="Header3"><input class="clear" type="text" name="email" id="email_<?php echo $users['email']?>" value="<?php echo $users['email'] ?>"></div>
                                <div class="Table-row-item" data-header="Header4"><input class="clear" type="text" name="poke" id="poke_<?php echo $users['id']?>" value="<?php echo $users['poke']?>"></div>
                                <div class="Table-row-item" data-header="Header5"><input class="poke" type="submit" value="Poke" id="submit" name="submit"></div>
                                <input class="clear" type="hidden" id="hidden" name="hidden" value="<?php echo $users['email']?>">
                            </div>
                        </form>
                                        <?php 
                                      } ?>

PHP:

<?php
require 'db.php';
if (isset($_POST['submit'])) {
$poke = $mysqli->escape_string($_POST['poke']);
    $email = $mysqli->escape_string($_POST['email']);
    $mysqli->query("UPDATE users SET poke='$poke' WHERE email='$email'") or die($mysqli->error);
}

AJAX:

$(document).ready(function () {
    $('form').on('submit', function (e) {
        var id = $(this).attr('data-counter');
        e.preventDefault();
        $.ajax({
            type: "post",
            data: $(this).serialize(),
            url: "update.php",
            success: function () {
                var counter = parseInt($("#poke_"+id).val()); // Use form's inner #poke
                counter++;
                $("#poke_"+id).val(counter);
                alert("form was submited on: " + id);
            }
        });
        return false;
    });
});
  • 写回答

1条回答 默认 最新

  • MAO-EYE 2017-06-20 14:14
    关注

    Not enough info. You need to do more diagnostics.

    E.g: Dump the SQL statement to the output so you can check it is what you expect it to be.

    And run the SQL in, e.g. phpAdmin to check if it updates.

    You may have a problem in the email string, which could be caused by, say, different character encodings between AJAX and the DB. Probably both should use UTF8.

    评论

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛