Page 1 of 1

sending session variables in a form

Posted: Wed Jul 30, 2008 12:08 pm
by adityadennis
I usually use "post" for sending variables from a form, but post doesn't seem to work in php 5 and I dont want to use get...is there a way to send session variables from a form instead?

Re: sending session variables in a form

Posted: Wed Jul 30, 2008 12:39 pm
by mabwi
If you want to get data from a form to your script, GET or POST are your choices.

Post definitely works in PHP 5. What's the code you're having a problem with?

Re: sending session variables in a form

Posted: Wed Jul 30, 2008 12:58 pm
by adityadennis
I've got this:

Code: Select all

<form action="add.php" method="post">
<p>BASICS</p>
your name:  <input type="text" name="added_by"  /><br />
keywords:   <input type="text" name="keywords"  /><br /><br />
<input type="submit" name="submit" value="INSERT"  />
</form>

and this is add.php:

Code: Select all

$dbc = mysql_connect("localhost", "username","password");
mysql_select_db("database", $dbc);
$added_by = $_POST['added_by'];
$keywords = $_POST['keywords'];
 
$r = mysql_query("INSERT INTO basics (added_by, keywords) VALUES ($added_by,$keywords)");
 
any ideas?

Re: sending session variables in a form

Posted: Wed Jul 30, 2008 1:05 pm
by Dynamis
Your code seems to be correct. Have you tried echoing the variables $added_by and $keywords in add.php to see if they are being set? If not I would do this. Not sure if you were just not getting a working query and thats why you asked. Also, I think in your query you need ' ' around the value values. So the query code should be:

Code: Select all

$r = mysql_query("INSERT INTO basics (added_by, keywords) VALUES ('".$added_by."','".$keywords."')");

Re: sending session variables in a form

Posted: Wed Jul 30, 2008 1:09 pm
by adityadennis
Dynamis wrote:Your code seems to be correct. Have you tried echoing the variables $added_by and $keywords in add.php to see if they are being set? If not I would do this. Not sure if you were just not getting a working query and thats why you asked. Also, I think in your query you need ' ' around the value values. So the query code should be:

Code: Select all

$r = mysql_query("INSERT INTO basics (added_by, keywords) VALUES ('".$added_by."','".$keywords."')");
Those variables haven't been set in add.php, I tried the echo. The query works fine if I use GET or just try putting in hard-coded data, but since this is eventually going to be an elaborate form, GET won't work.

So I guess what I'm asking is: is there a way to do exactly what post does but with something else. I googled around and it seems that a lot of people are having problems with post in php 5 :(

Re: sending session variables in a form

Posted: Wed Jul 30, 2008 2:41 pm
by mabwi
POST works just fine in PHP 5. Let's focus on your code, instead of blaming it on the system.

At the start of add.php, print_r($_POST) and post the results here.

Re: sending session variables in a form

Posted: Wed Jul 30, 2008 3:06 pm
by adityadennis

Code: Select all

Array ( [added_by] => jkjhkjh [keywords] => kjhkj [submit] => INSERT )

Re: sending session variables in a form

Posted: Wed Jul 30, 2008 3:14 pm
by mabwi
So, that tells us for sure that POST is working.

Now, try something like this:

Code: Select all

 
$added = $_POST['added_by'];
echo $added;
 
For each variable.

If that works, build up your sql query like this:

Code: Select all

 
$sql = "INSERT INTO basics (added_by, keywords) VALUES ('".$added_by."','".$keywords."')";
 
And echo the query itself to ensure that it's correct.

If it's correct, query like so:

Code: Select all

 
$r = mysql_query($sql) or die(mysql_error());
 
By the time you've done that, you should have a very solid handle on what the issue is, if there is any.

Re: sending session variables in a form

Posted: Wed Jul 30, 2008 3:52 pm
by adityadennis
holy cow, it worked! thank you!