sending session variables in a form
Moderator: General Moderators
-
adityadennis
- Forum Newbie
- Posts: 5
- Joined: Wed Jul 30, 2008 12:06 pm
sending session variables in a form
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
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?
Post definitely works in PHP 5. What's the code you're having a problem with?
-
adityadennis
- Forum Newbie
- Posts: 5
- Joined: Wed Jul 30, 2008 12:06 pm
Re: sending session variables in a form
I've got this:
and this is add.php:
any ideas?
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)");
Re: sending session variables in a form
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."')");-
adityadennis
- Forum Newbie
- Posts: 5
- Joined: Wed Jul 30, 2008 12:06 pm
Re: sending session variables in a form
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.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."')");
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
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.
At the start of add.php, print_r($_POST) and post the results here.
-
adityadennis
- Forum Newbie
- Posts: 5
- Joined: Wed Jul 30, 2008 12:06 pm
Re: sending session variables in a form
Code: Select all
Array ( [added_by] => jkjhkjh [keywords] => kjhkj [submit] => INSERT )Re: sending session variables in a form
So, that tells us for sure that POST is working.
Now, try something like this:
For each variable.
If that works, build up your sql query like this:
And echo the query itself to ensure that it's correct.
If it's correct, query like so:
By the time you've done that, you should have a very solid handle on what the issue is, if there is any.
Now, try something like this:
Code: Select all
$added = $_POST['added_by'];
echo $added;
If that works, build up your sql query like this:
Code: Select all
$sql = "INSERT INTO basics (added_by, keywords) VALUES ('".$added_by."','".$keywords."')";
If it's correct, query like so:
Code: Select all
$r = mysql_query($sql) or die(mysql_error());
-
adityadennis
- Forum Newbie
- Posts: 5
- Joined: Wed Jul 30, 2008 12:06 pm
Re: sending session variables in a form
holy cow, it worked! thank you!