【问题标题】:Inserting a new record: Row gets created in database but values do not get submitted插入新记录:在数据库中创建行但未提交值
【发布时间】:2016-03-26 00:34:47
【问题描述】:

所以在我单击提交按钮后,我的数据库中会使用用户 ID 创建一个新行,但用户名不会提交到数据库。目前我提交的两个 loginInfo 表有 3 列:用户 ID(自动生成和递增)、用户名和密码。我相信我的错误出现在 newuser.php 文件中,我不确定在其中做什么。

<td><a href="#" class="myeditable editable editable-click editable-empty" id="new_username" data-type="text" data-name="name" data-original-title="Enter username">Empty</a></td>

也在脚本下的同一个文件中

$.fn.editable.defaults.mode = 'inline';

//init editables
$('.myeditable').editable({
    url: 'post.php' //this url will not be used for creating new user, it is only for update
});

//make username required
$('#new_username').editable('option', 'validate', function(v) {
    if(!v) return 'Required field name!';
});

//automatically show next editable
$('.myeditable').on('save.newuser', function(){
    var that = this;
    setTimeout(function() {
        $(that).closest('tr').next().find('.myeditable').editable('show');
    }, 200);
});

$('#save-btn').click(function() {
    $('.myeditable').editable('submit', {
        url: 'newuser.php',
        ajaxOptions: {
            type: 'POST',
            dataType: 'json' //assuming json response
        },
        success: function(data, config) {
            if(data && data.id) {  //record created, response like {"id": 2}
                //set pk
                $(this).editable('option', 'pk', data.id);
                //remove unsaved class
                $(this).removeClass('editable-unsaved');
                //show messages
                var msg = 'New user created! Now editables submit individually.';
                $('#msg').addClass('alert-success').removeClass('alert-error').html(msg).show();
                $('#save-btn').hide();
                $(this).off('save.newuser');
            } else if(data && data.errors){
                //server-side validation error, response like {"errors": {"username": "username already exist"} }
                config.error.call(this, data.errors);
            }
        },
        error: function(errors) {
            var msg = '';
            if(errors && errors.responseText) { //ajax error, errors = xhr object
                msg = errors.responseText;
            } else { //validation error (client-side or server-side)
                $.each(errors, function(k, v) { msg += k+": "+v+"<br>"; });
            }
            $('#msg').removeClass('alert-success').addClass('alert-error').html(msg).show();
        }
    });
});

Post.php

include('connect.php'); 
sleep(1);
$pk = $_post['pk'];
$un=$_POST['name'];
$value1 = $_POST['value'];

if(!empty($value1)) {
    $sql =("UPDATE loginInfo
            SET $un=$value1
            WHERE userID='$pk'");
    mysqli_query($con, $sql);

    //here, for debug reason we just return dump of $_POST, you will see result in browser console
    print_r($_POST);
}
else {
    header('HTTP/1.0 400 Bad Request', true, 400);
    echo "This field is required!";
}

newuser.php

$pk = $_post['pk'];
$un=$_POST['name'];
$value1 = $_POST['value'];

try {

    $sql =("INSERT INTO loginInfo(username,password)
            VALUES ('a','b')");   
    mysqli_query($con, $sql);


} catch (Exception $e) {
    print($e->__toString());
}

?>

在我的 net 控制台下的 firefox 中,我看到了这个输出,这是我在名称文本框中输入的值

Parameters
name    
testing

Source
name=testing

来自网站的文档:https://vitalets.github.io/x-editable/docs.html#newrecord

【问题讨论】:

    标签: jquery json ajax x-editable


    【解决方案1】:

    您需要在用户名周围加上引号,因为它是一个字符串。

    $sql =("UPDATE loginInfo
            SET $un='$value1'
            WHERE userID='$pk'");
    

    如果您使用准备好的查询会更好,以防止 SQL 注入。

    $stmt = mysqli_prepare($con, 
                            "UPDATE loginInfo
                            SET $un = ?
                            WHERE userID = ?");
    mysqli_bind_param($stmt, "ss", $value1, $pk);
    mysqli_execute($stmt);
    

    由于无法对列名进行参数化,因此您应该在查询中使用 $un 之前对其进行验证。或者,如果这是用户可以更新的唯一列,您可以只对列名进行硬编码,而不是使用 POST 参数。

    【讨论】:

    • 感谢我更改了代码,但仍然无法正常工作。我认为我的 post.php 和 newuser.php 页面设置不正确。我无法对列名进行硬编码,因为用户可以更新许多列
    • 查询是否出错? echo mysqli_error($con); 显示什么?
    • 我刚刚注意到我遗漏了mysqli_prepare$con 参数,因此请尝试更正的语法。
    猜你喜欢
    • 1970-01-01
    • 2019-02-11
    • 1970-01-01
    • 2011-03-21
    • 1970-01-01
    • 2019-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多