0

I'm having some problems trying to post $_GET variables into a table.

Here is my script:

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>';
        }

Whenever I run the script everything else but the listname and list_id is posted in the database table. I tried assigning the get variables to new variable such as

$listNAME = $_GET['id'];

but even with that I still end up with empty fields in my table

I even tried to use the $_GET variable in the mysql INSERT query and still no luck

Can anyone help me out and give me some advice as to what I can do to solve the empty fields when the script runs.

<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>
12
  • when you print out the get variable what do you see? Commented Sep 10, 2013 at 22:30
  • @MrD I see the info from the variables passed in the URL Commented Sep 10, 2013 at 22:33
  • your columns are set to the proper data type? varchar or text or>>? Commented Sep 10, 2013 at 22:37
  • 1
    Why you are mixing $_POST and $_GET request. You are saying ending up with empty fields. How could that happen when you testing if(!empty($Cname) && !empty($Cnumber)){ So your fields $Cname and $Cnumbercan not be emtpy. Commented Sep 10, 2013 at 22:46
  • 1
    Where you setting list_name and list_id fields ? If you do it with a <form> and a submit your method mustbe method="post" . As long we cannot see your <form> we can't see what's going on. Commented Sep 10, 2013 at 22:56

2 Answers 2

4

You say you have $_GET variables, but you are trying to retrieve them as $_POST variables:

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

Isn't it the issue? You could also try this to see what's comming in both arrays:

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

Alternatively, you could use $_REQUEST as it receives either $_GET or $_POST variables.

Sign up to request clarification or add additional context in comments.

Comments

1

I say it only to notice .

Please use PDO or mysqli


if you are calling your addclient.php like

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

than you must catch both variables in addclient.php

if (isset($_GET['list_id'])) {

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

}

and your form

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

and after submit

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

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

and in your insert

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

$listid without quotes it's a int(11) .

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

1 Comment

thank you @moskito-x I used this and managed to make everything work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.