PHP在表字段中发布$ _GET变量

时间:2013-09-10 22:28:46

标签: php mysql

我在尝试将$ _GET变量发布到表中时遇到了一些问题。

这是我的剧本:

include 'connect.php';

if(isset($_POST['client_name'])) {

$_GET['list']; //these are variables passed through from another page and I want these to post in the same table this page is suppose to post in.
$_GET['list_id'];


$Cname = $_POST['client_name'];
$Cnumber = $_POST['client_number'];
$listid = $_POST['list_id'];
$listname = $_POST['list'];


if(!empty($Cname) && !empty($Cnumber)){

    $query = "INSERT INTO clients (id, client_name, client_number, list_name, date_registered, list_id) VALUES ('$userid','$Cname', '$Cnumber', '$listname', now(), '$listid')";

    mysql_query($query);

echo '<br />
 <br />
        You successfully added a new clients to your list <a href="http://somewebsite.com/clients.php">View Update</a>';


    mysql_close();
    } 

    else {

        echo '<script type="text/javascript">alert("Both fields are required");</script>';
        }

每当我运行脚本时,除了listname和list_id之外的所有其他内容都会发布在数据库表中。 我尝试将get变量分配给新变量,例如

$ listNAME = $ _GET ['id'];

但即便如此,我仍然会在表格中找到空白字段

我甚至尝试在mysql INSERT查询中使用$ _GET变量,但仍然没有运气

任何人都可以帮助我,并就脚本运行时我可以做些什么来解决空字段给我一些建议。

<form action="addclient.php" method="POST">

Name of Client: <input type="text" name="client_name">

Client's Number: <input type="text" name="client_number" placeholder="1-876-xxx-xxx">

 <input type="submit" >


</form>

2 个答案:

答案 0 :(得分:4)

你说你有$ _GET变量,但你试图将它们检索为$ _POST变量:

$listid = $_POST['list_id'];
$listname = $_POST['list'];

这不是问题吗?您也可以尝试这样来查看两个阵列中的内容:

print_r($_GET);
print_r($_POST);

或者,你可以使用$ _REQUEST,因为它收到$ _GET或$ _POST变量。

答案 1 :(得分:1)

我只想注意。

请使用PDO或mysqli


如果你打电话给你的addclient.php

http://localhost/addclient.php?list_id=100&list=mylistname

比你必须在addclient.php

中捕获两个变量
if (isset($_GET['list_id'])) {

 $listid = $_GET['list_id'];
 $listname = $_GET['list'];

}

和你的表格

<form action="addclient.php" method="POST">
    <input type="hidden" name="list_id" value="$listid">
    <input type="hidden" name="list" value="$listname">
Name of Client: <input type="text" name="client_name">
Client's Number: <input type="text" name="client_number" placeholder="1-876-xxx-xxx">
 <input type="submit" >
</form>

并在提交后

if(isset($_POST['client_name'])) {

$Cname = $_POST['client_name'];
$Cnumber = $_POST['client_number'];
$listid = $_POST['list_id'];
$listname = $_POST['list'];
....
}

并在您的插入

VALUES ('$userid','$Cname', '$Cnumber', '$listname', now(), '$listid')

$ listid没有引号它是int(11)

VALUES ('$userid','$Cname', '$Cnumber', '$listname', now(), $listid)