过去几天我一直坚持这个问题,我似乎无法解决 .
我在PHP文件中有以下两个 if statements :
This statement gets the post ID from the previous page.
if(isset($_GET['post'])){
$post_id = $_GET['post'];
}
This statement inserts user inputs into a DB_Table after a button is clicked. Amongst the data inserted into the DB_Table, I want the $post_id from the (isset($_GET['post'])) condition to be one of them.
if(isset($_POST['submit'])){
$insert_member = "insert into hcg_members (post_id, member_id) values ('$post_id','$match_member_id')";
$run_event = mysqli_query($con,$insert_member);
}
唯一的问题是,我似乎找不到保留$ post_id数据的方法 . 我试图将$ post_id设置为$ Globals变量,但这不起作用 .
作为一种查找问题的方法,我已成功从if(isset($ _ GET []))语句之外回显$ post_id但是一旦我尝试从if(isset($ _ POST [])运行它,echo语句就会停止工作))声明 .
任何帮助将不胜感激,谢谢你提前 .
2 回答
好的,所以只是澄清那些发现我的问题不清楚的人 . 我试图对我的脚本进行两次不同的调用 .
第一个是GET请求 .
第二个是POST请求 .
我正在尝试执行上述操作,同时保留在POST请求中使用它时位于GET请求中的$ post_id的值 .
正如 dan08 & Crayon Violent 所建议的那样,当尝试将变量的值从一个HTTP请求方法传递给另一个时,无法保留变量的值 . 正如 Crayon Violent 所指出的,http是一种无状态协议,它使PHP无法记住前一个请求中的任何内容 .
因此,我使用 Crayon Violent 的解决方案之一来使用会话,以便将变量从Get Request方法转移到POST Request方法,并且它有效! :)
There's the solution:
GET请求方法:
POST请求方法:
Thanks again to dan08 and Crayon Violent's support! :)
例如$ _GET ['post'] = 5时,此代码会生成以下查询,这对我来说很好 . 你能详细说明出了什么问题吗?此代码是否以某种方式驻留在不同的函数中,因此可能是变量无法访问的情况?
插入hcg_members(post_id,member_id)值('5','')