首页 文章

在函数内使用A $ _GET变量进行数据库插入?

提问于
浏览
0

希望有人可以解释我遇到的问题 .

我有一个表单,用于向页面添加注释;

<?php if(app('current_user')->role != 'user'): ?>
        <div class="leave-comment">
            <div class="control-group form-group">
                <h5><?= trans('leave_comment'); ?></h5>
                <div class="controls">
                    <textarea class="form-control" id="comment-text"></textarea>
                </div>
            </div>
            <div class="control-group form-group">
                 <div class="controls">
                    <button class="btn btn-success" id="comment">
                        <i class="fa fa-comment"></i>
                        <?= trans('comment'); ?>
                    </button>
                </div>
            </div>
        </div>
    <?php else: ?>
        <p><?= trans('you_cant_post'); ?></p>
    <?php endif; ?>

这使用ajax加载以下情况;

case "postComment":
    echo app('comment')->insertComment(ASSession::get("user_id"), $_POST['comment']);
    break;

哪个负载;

public function insertComment($userId, $comment)
{
    $userInfo = $this->users->getInfo($userId);
    $datetime = date("Y-m-d H:i:s");

    $this->db->insert("as_comments", array(
        "posted_by" => $userId,
        "posted_by_name" => $userInfo['username'],
        "comment" => strip_tags($comment),
        "post_time" => $datetime,
        "FK_DappID" => 1
    ));

    return json_encode(array(
        "user" => $userInfo['username'],
        "comment" => stripslashes(strip_tags($comment)),
        "postTime" => $datetime
    ));
}

这些注释用在一个名为view.php的页面上,我有一个变量,它是从名为$ _GET ['ID']的htis页面的URL设置的 .

SO..

如何将要插入数据库的数组中的“FK_DappID”=> 1更改为$ _GET ['ID']的值?

我尝试过使用“FK_DappID”=> $ _GET ['ID']但它不起作用..

任何帮助都会非常有用 .

谢谢,B .

编辑:Index.js

$(document).ready(function () {

//comment button click
$("#comment").click(function () {
    //remove all error messages
    asengine.removeErrorMessages();

    var comment = $("#comment-text"),
         btn    = $(this);

    //validate comment
    if($.trim(comment.val()) == "") {
        asengine.displayErrorMessage(comment, $_lang.field_required);
        return;
    }

    //set button to posting state
    asengine.loadingButton(btn, $_lang.posting);

     $.ajax({
        url: "ASEngine/ASAjax.php",
        type: "POST",
        data: {
            action : "postComment",
            comment: comment.val()
        },
        success: function (result) {
            //return button to normal state
            asengine.removeLoadingButton(btn);
            try {
               //try to parse result to JSON
               var res = JSON.parse(result);

               //generate comment html and display it
               var html  = "<blockquote>";
                    html += "<p>"+res.comment+"</p>";
                    html += "<small>"+res.user+" <em> "+ $_lang.at +res.postTime+"</em></small>";
                    html += "</blockquote>";
                if( $(".comments-comments blockquote").length >= 7 )
                    $(".comments-comments blockquote").last().remove();
                $(".comments-comments").prepend($(html));
                comment.val("");
            }
            catch(e){
               //parsing error, display error message
               asengine.displayErrorMessage(comment, $_lang.error_writing_to_db);
            }
        }
    });
});

});

编辑:Ajax.php

include_once'AS.php';

$ action = $ _POST ['action'];

switch($ action){case'checkLogin':app('login') - > userLogin($ _ POST ['username'],$ _ POST ['password']);打破;

case "registerUser":
    app('register')->register($_POST['user']);
    break;

case "resetPassword":
    app('register')->resetPassword($_POST['newPass'], $_POST['key']);
    break;

case "forgotPassword":
    $result = app('register')->forgotPassword($_POST['email']);
    if ($result !== true) {
        echo $result;
    }
    break;

case "postComment":
    echo app('comment')->insertComment(ASSession::get("user_id"), $_POST['comment']);
    break;

case "updatePassword":
    app('user')->updatePassword(
        ASSession::get("user_id"),
        $_POST['oldpass'],
        $_POST['newpass']
    );
    break;

case "updateDetails":
    app('user')->updateDetails(ASSession::get("user_id"), $_POST['details']);
    break;

case "changeRole":
    onlyAdmin();

    $result = app('user')->changeRole($_POST['userId'], $_POST['role']);
    echo ucfirst($result);
    break;

case "deleteUser":
    onlyAdmin();

    $userId = (int) $_POST['userId'];
    $users = app('user');

    if (! $users->isAdmin($userId)) {
        $users->deleteUser($userId);
    }
    break;

case "getUserDetails":
    onlyAdmin();

    respond(
        app('user')->getAll($_POST['userId'])
    );
    break;

case "addRole":
    onlyAdmin();

    respond(
        app('role')->add($_POST['role'])
    );
    break;

case "deleteRole":
    onlyAdmin();

    app('role')->delete($_POST['roleId']);
    break;


case "addUser":
    onlyAdmin();

    respond(
        app('user')->add($_POST)
    );
    break;

case "updateUser":
    onlyAdmin();

    app('user')->updateUser($_POST['userId'], $_POST);
    break;

case "banUser":
    onlyAdmin();

    app('user')->updateInfo($_POST['userId'], array('banned' => 'Y'));
    break;

case "unbanUser":
    onlyAdmin();

    app('user')->updateInfo($_POST['userId'], array('banned' => 'N'));
    break;

case "getUser":
    onlyAdmin();

    respond(
        app('user')->getAll($_POST['userId'])
    );
    break;

default:
    break;
}

function onlyAdmin()
{
if (! (app('login')->isLoggedIn() && app('current_user')->is_admin)) {
    exit();
}
}

1 回答

  • 1

    您正在使用 type: "POST" 提交ajax,因此数据将在您的PHP中的 $_POST 变量中提供 .

    因为看起来你正在添加注释这是正确的请求动词,所以在你的插入方法中更改为 "FK_DappID" =>$_POST['ID'] 并确保将 ID 添加到 data 你在你的AJAX中进行POST:

    您应该创建一个隐藏的表单输入 <input type="hidden" id="post_id" name="post_id" value="<?= $_GET['ID'] ?>" /> ,然后从此输入中获取值以发送您的AJAX POST请求 .

    data: {
      action : "postComment",
      comment: comment.val(),
      ID: $("#post_id").val()
    }
    

相关问题