Php 在函数中使用$\u GET变量进行数据库插入?
希望有人能对我遇到的问题有所帮助 我有一个表单,用于在页面上添加评论Php 在函数中使用$\u GET变量进行数据库插入?,php,arrays,get,insert,Php,Arrays,Get,Insert,希望有人能对我遇到的问题有所帮助 我有一个表单,用于在页面上添加评论 <?php if(app('current_user')->role != 'user'): ?> <div class="leave-comment"> <div class="control-group form-group"> <h5><?= trans('leave_comment
<?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; ?>
哪种负载
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的页面,我有一个变量,它是从htis页面的URL设置的,名为$_GET['ID']
所以
如何将要插入数据库的数组中的FK_DappID=>1更改为$_GET['ID']的值
我尝试过使用FK_DappID=>$\u 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);
}
}
});
});
});
您正在使用type:POST提交ajax,因此数据将在PHP中的$\u POST变量中可用 因为看起来您正在添加注释,所以这是正确的请求动词,所以在插入方法中更改为FK_DappID=>$\u POST['ID'],并确保将ID添加到您在AJAX中发布的数据中:
你应该创建一个隐藏的表单,显示你的输入ajax@RyanTuostoadddhi,我同样传递变量ID,然后使用$PK_DappID=$\u GET['ID']在视图页面上获取它;我可以使用POST而不是get获取变量吗??谢谢你的意见!当然,只需使用$_POST['ID'],您还需要将其添加到通过ajaxHi Ryan发送的数据中,非常感谢您的帮助,我希望您能原谅我缺乏编码知识。如何使用POST而不是GET将变量传递到新页面?我认为GET是从另一个页面带来价值的唯一方法。如果我在我的查看页面上使用$PK\u DappID=$\u POST['ID'],它不会从另一个页面带来值。谢谢,布拉德。别担心,我想这是一个常见的困惑点。您确实希望使用GET将数据传递到后期查看页面。然后,要向该帖子添加评论,您需要使用post请求并传递ID将评论添加到正确的帖子中。交换这些数据的一种常见方式是通过一个隐藏的表单元素,在该元素中,您回显$\u GET值,然后在javascript中检索它,以便在ajax中通过POST请求发回。我已经更新了我的答案以显示这一点。我已经将隐藏的输入添加到页面中,&ID:$post_ID.val添加到ajax请求中。我还在insert方法中设置了FK_DappID=>$\u POST['ID']。到目前为止还不走运,我注意到我忘了在代码中添加ASAjax页面,我现在将添加它,是否还需要添加一个额外的参数?再次感谢。
$(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);
}
}
});
});
});
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();
}
}
data: {
action : "postComment",
comment: comment.val(),
ID: $("#post_id").val()
}