2013-09-10 42 views
0

我有一些問題試圖將$ _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被張貼在數據庫表中。 我嘗試將獲取變量分配給新變量,如

$ listNAME = $ _GET ['id'];

,但即便如此我還是最終在我的桌子空字段

我甚至嘗試使用$ _GET變量在MySQL的INSERT查詢,仍然沒有運氣

誰能幫助我,並給我一些建議,說明腳本運行時我能做些什麼來解決空白字段。

<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> 
+0

當你打印出get變量時,你看到了什麼? –

+0

@MrD我看到通過URL – mattchambers

+0

傳遞的變量的信息,您的列設置爲正確的數據類型? varchar或文本或>>? –

回答

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) 
+0

謝謝@ moskito-X我用這個和管理,使一切工作 – mattchambers

4

你說你有$ _GET變量,但是你想找回他們爲$ _ POST變量:

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

是不是問題?你也可以試試這個,看看有什麼在正在添加兩個數組:

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

或者,你可以在其接收或者$ _GET或$ _POST變量使用$ _REQUEST。