gpt4 book ai didi

php - 无法在 C :\wamp\www\zr\core\functions\users. php 中重新分配自动全局变量 _POST

转载 作者:行者123 更新时间:2023-12-01 00:42:19 24 4
gpt4 key购买 nike

我正在使用 PHP 和 MySQL。我正在尝试使用 PHP 函数将网页中的表单数据插入到 MySQL 数据库中。

这是用户表单页面的代码:

<?php 
include 'core/int.php';
include 'includes/overall/header.php';


if(!empty($_POST)){
$add_status = $insert->add_status($user_data['user_id'], $_POST);
}
?>


<form action = "" method = "POST">
<ul>
<li>
<input name = "question_time" type = "hidden" value = "<?php echo time()?>" />
</li>
<li>
<h2>Post your Question <?php echo $user_data['username'];?></h2>
<textarea name = "question"></textarea>
</li>
<li>
<p><input type = "submit" value = "submit"></p>
</li>
</ul>
</form>

这是我在另一个页面上定义的函数“add_status”..

function add_status($user_id, $_POST){

mysqli_query("INSERT into `post` (user_id, status_time, status_content) VALUES($user_id, '$_POST[question_time]', '$_POST[question]')");
}

当它在本地主机上运行时会抛出一个错误:

Cannot re-assign auto-global variable _POST in C:\wamp\www\zr\core\functions\users.php on line 97

我看到第 97 行是函数 add_status()

错误是什么意思,我该如何解决?

最佳答案

你的问题是这个

function add_status($user_id, $_POST){

您将 $_POST 声明为局部参数变量。根据定义,$_POST 是超全局的(适用于所有范围)。因此,您要么需要删除参数并让它正常传播,要么需要将其更改为局部变量,如 $post 并改用它。

function add_status($user_id, $post){

我会推荐后者。

一些其他注释:mysql_query 和相关参数 are deprecated and will be removed .请考虑使用 mysqli

您正在将 $_POST 直接传递到 SQL 中。那剩下你wide open to SQL injection .你需要解决这个问题。

关于php - 无法在 C :\wamp\www\zr\core\functions\users. php 中重新分配自动全局变量 _POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29306603/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com