By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,747 Members | 2,011 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,747 IT Pros & Developers. It's quick & easy.

$_POST variable problem

P: n/a
How would I add a variable that I will assign to a list of $_POST
variables that I extract from a form?

My form passes a value for $q. That works fine. What I want to do is run
an if/else on it and assign a new variable based on what was chosen such as

if ($q == "red") {
$q = "tp"; //change $q to this value
$sub = "ak"; //attach this value to the $sub variable
} else {
$q = $q; //don't change the value of $q
$sub = ""; //Leave $sub empty
}

Then both the $q and $sub will get added to a mysql_query for insertion.

mysql_query("INSERT INTO `table` VALUES(\"\",\"$q\",\"$sub\",\"$comments
- $name\",\"$email\",\"$today\",\"\" )") or die(mysql_error());

I can get it to add the $q with no problem but it isn't recognizing the
$sub that wasn't part of the original $_POST variables. Any suggestions?

Jul 17 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
"Jack" <no****@noemail.com> wrote in message
news:KcHDc.162874$3x.32326@attbi_s54...
How would I add a variable that I will assign to a list of $_POST
variables that I extract from a form?

My form passes a value for $q. That works fine. What I want to do is run
an if/else on it and assign a new variable based on what was chosen such as
if ($q == "red") {
$q = "tp"; //change $q to this value
$sub = "ak"; //attach this value to the $sub variable
} else {
$q = $q; //don't change the value of $q
$sub = ""; //Leave $sub empty
}

Then both the $q and $sub will get added to a mysql_query for insertion.

mysql_query("INSERT INTO `table` VALUES(\"\",\"$q\",\"$sub\",\"$comments
- $name\",\"$email\",\"$today\",\"\" )") or die(mysql_error());


It'd help if you wrote your code so you could easily see what was going
wrong. Dying on a database error is extremely user unfriendly.

$sql = "INSERT INTO `table` VALUES(\"\",\"$q\",\"$sub\",\"$comments -
$name\",\"$email\",\"$today\",\"\" )";
$result = mysql_query($sql , $conn);
if(! $result || mysql_error($conn))
{
echo $sql . "<br>\n";
echo mysql_error($conn);
exit;
}

Using this type of structure will allow you to control the fallout of the
error rather than just providing an unintelligable error message.

Write INSERT statements in the format

INSERT INTO tablename (field1 , field2, field3 ...) VALUES
('value1','value2','value3' ..)

Furthermore, mysql and postgresql use ' as the field delimiter, wheres MS
SQL and MS Access use " as the field delimiter.

Jul 17 '05 #2

P: n/a
CJ Llewellyn wrote:
"Jack" <no****@noemail.com> wrote in message
news:KcHDc.162874$3x.32326@attbi_s54...
How would I add a variable that I will assign to a list of $_POST
variables that I extract from a form?

My form passes a value for $q. That works fine. What I want to do is run
an if/else on it and assign a new variable based on what was chosen such


as
if ($q == "red") {
$q = "tp"; //change $q to this value
$sub = "ak"; //attach this value to the $sub variable
} else {
$q = $q; //don't change the value of $q
$sub = ""; //Leave $sub empty
}

Then both the $q and $sub will get added to a mysql_query for insertion.

mysql_query("INSERT INTO `table` VALUES(\"\",\"$q\",\"$sub\",\"$comments
- $name\",\"$email\",\"$today\",\"\" )") or die(mysql_error());

It'd help if you wrote your code so you could easily see what was going
wrong. Dying on a database error is extremely user unfriendly.

$sql = "INSERT INTO `table` VALUES(\"\",\"$q\",\"$sub\",\"$comments -
$name\",\"$email\",\"$today\",\"\" )";
$result = mysql_query($sql , $conn);
if(! $result || mysql_error($conn))
{
echo $sql . "<br>\n";
echo mysql_error($conn);
exit;
}

Using this type of structure will allow you to control the fallout of the
error rather than just providing an unintelligable error message.

Write INSERT statements in the format

INSERT INTO tablename (field1 , field2, field3 ...) VALUES
('value1','value2','value3' ..)

Furthermore, mysql and postgresql use ' as the field delimiter, wheres MS
SQL and MS Access use " as the field delimiter.


Thank you CJ. I changed my mysql code as you recommended. However the
problem remains. As bad as my code was, it worked. It inserted the
information into the database. And there's no error message generated
either with my old code or with your cleaned up code. It just doesn't
see the $sub category at all. Any other ideas?

Jul 17 '05 #3

P: n/a
"Jack" <no****@noemail.com> wrote in message
news:uzIDc.125698$Sw.55866@attbi_s51...
-snip-
Thank you CJ. I changed my mysql code as you recommended. However the
problem remains. As bad as my code was, it worked. It inserted the
information into the database. And there's no error message generated
either with my old code or with your cleaned up code. It just doesn't
see the $sub category at all. Any other ideas?


Remove the row from the table and add this before the mysql_query line:-

echo "[" & $q & "]<br>\n";
if(! isset($sub))
echo '$sub is not set!<br>';

Jul 17 '05 #4

P: n/a
CJ Llewellyn wrote:
"Jack" <no****@noemail.com> wrote in message
news:uzIDc.125698$Sw.55866@attbi_s51...
-snip-
Thank you CJ. I changed my mysql code as you recommended. However the
problem remains. As bad as my code was, it worked. It inserted the
information into the database. And there's no error message generated
either with my old code or with your cleaned up code. It just doesn't
see the $sub category at all. Any other ideas?

Remove the row from the table and add this before the mysql_query line:-

echo "[" & $q & "]<br>\n";
if(! isset($sub))
echo '$sub is not set!<br>';


As one would expect, it returns $sub is not set!

Now back to my original question:

if ($q == "red") {
$q = "tp"; //change $q to this value
$sub = "ak"; //attach this value to the $sub variable
} else {
$q = $q; //don't change the value of $q
$sub = ""; //Leave $sub empty
}

I can get it to recognize the $q from the $_POST array with no problem
but it isn't recognizing the $sub that wasn't part of the original
$_POST variables. Is there some problem with my trying to set the $sub
variable with the above if statement? If so, what is the correct way to
set that variable manually?

Jul 17 '05 #5

P: n/a
Regarding this well-known quote, often attributed to Jack's famous "Mon, 28
Jun 2004 00:35:08 GMT" speech:
CJ Llewellyn wrote:
"Jack" <no****@noemail.com> wrote in message
news:uzIDc.125698$Sw.55866@attbi_s51...
-snip-
Thank you CJ. I changed my mysql code as you recommended. However the
problem remains. As bad as my code was, it worked. It inserted the
information into the database. And there's no error message generated
either with my old code or with your cleaned up code. It just doesn't
see the $sub category at all. Any other ideas?

Remove the row from the table and add this before the mysql_query line:-

echo "[" & $q & "]<br>\n";
if(! isset($sub))
echo '$sub is not set!<br>';


As one would expect, it returns $sub is not set!

Now back to my original question:

if ($q == "red") {
$q = "tp"; //change $q to this value
$sub = "ak"; //attach this value to the $sub variable
} else {
$q = $q; //don't change the value of $q
$sub = ""; //Leave $sub empty
}

I can get it to recognize the $q from the $_POST array with no problem
but it isn't recognizing the $sub that wasn't part of the original
$_POST variables. Is there some problem with my trying to set the $sub
variable with the above if statement? If so, what is the correct way to
set that variable manually?


First off, your code would be easier to sort out, as well as more portable
and secure, using the $_POST superglobal, instead of just the
register_globals automation. So, with that:

<?php
if ($_POST['q'] == "red") {
$q = "tp"; //change $q to this value
$sub = "ak"; //attach this value to the $sub variable
} else {
$q = $_POST['q']; // assign $q to the POSTed value
$sub = ""; // Leave $sub empty
}
?>

That said, have you tried some rough debugging?

<?php
echo "POST value of q = '" . $_POST['q'] . "'<br>\n";

if ($_POST['q'] == "red") {
$q = "tp"; //change $q to this value
$sub = "ak"; //attach this value to the $sub variable
echo "Posted q was 'red'. Now \$q is $q and \$sub is $sub<br>\n";
} else {
$q = $_POST['q']; // assign $q to the POSTed value
$sub = ""; // Leave $sub empty
echo "Posted q was NOT 'red'. Now \$q is $q and \$sub is $sub<br>\n";
}

echo "FINAL RESULT: \$q = '$q', \$sub = $sub";
?>

See what you come up with there. If you find that it's not detecting the
"redness", there might be extraneous characters getting passed in the POST.
Try perhaps using a strpos() search, a preg_match() search, or even just
trim()-ming and rtrim()-ming the posted value before you check it.

--
-- Rudy Fleminger
-- sp@mmers.and.evil.ones.will.bow-down-to.us
(put "Hey!" in the Subject line for priority processing!)
-- http://www.pixelsaredead.com
Jul 17 '05 #6

P: n/a
Regarding this well-known quote, often attributed to Jack's famous "Mon, 28
Jun 2004 00:35:08 GMT" speech:
CJ Llewellyn wrote:
"Jack" <no****@noemail.com> wrote in message
news:uzIDc.125698$Sw.55866@attbi_s51...
-snip-
Thank you CJ. I changed my mysql code as you recommended. However the
problem remains. As bad as my code was, it worked. It inserted the
information into the database. And there's no error message generated
either with my old code or with your cleaned up code. It just doesn't
see the $sub category at all. Any other ideas?

Remove the row from the table and add this before the mysql_query line:-

echo "[" & $q & "]<br>\n";
if(! isset($sub))
echo '$sub is not set!<br>';


As one would expect, it returns $sub is not set!

Now back to my original question:

if ($q == "red") {
$q = "tp"; //change $q to this value
$sub = "ak"; //attach this value to the $sub variable
} else {
$q = $q; //don't change the value of $q
$sub = ""; //Leave $sub empty
}

I can get it to recognize the $q from the $_POST array with no problem
but it isn't recognizing the $sub that wasn't part of the original
$_POST variables. Is there some problem with my trying to set the $sub
variable with the above if statement? If so, what is the correct way to
set that variable manually?


First off, your code would be easier to sort out, as well as more portable
and secure, using the $_POST superglobal, instead of just the
register_globals automation. So, with that:

<?php
if ($_POST['q'] == "red") {
$q = "tp"; //change $q to this value
$sub = "ak"; //attach this value to the $sub variable
} else {
$q = $_POST['q']; // assign $q to the POSTed value
$sub = ""; // Leave $sub empty
}
?>

That said, have you tried some rough debugging?

<?php
echo "POST value of q = '" . $_POST['q'] . "'<br>\n";

if ($_POST['q'] == "red") {
$q = "tp"; //change $q to this value
$sub = "ak"; //attach this value to the $sub variable
echo "Posted q was 'red'. Now \$q is $q and \$sub is $sub<br>\n";
} else {
$q = $_POST['q']; // assign $q to the POSTed value
$sub = ""; // Leave $sub empty
echo "Posted q was NOT 'red'. Now \$q is $q and \$sub is $sub<br>\n";
}

echo "FINAL RESULT: \$q = '$q', \$sub = $sub";
?>

See what you come up with there. If you find that it's not detecting the
"redness", there might be extraneous characters getting passed in the POST.
Try perhaps using a strpos() search, a preg_match() search, or even just
trim()-ming and rtrim()-ming the posted value before you check it.

--
-- Rudy Fleminger
-- sp@mmers.and.evil.ones.will.bow-down-to.us
(put "Hey!" in the Subject line for priority processing!)
-- http://www.pixelsaredead.com
Jul 17 '05 #7

P: n/a
"Jack" <no****@noemail.com> wrote in message
news:0JJDc.163535$3x.4031@attbi_s54...
CJ Llewellyn wrote:
"Jack" <no****@noemail.com> wrote in message
news:uzIDc.125698$Sw.55866@attbi_s51...
-snip-
Thank you CJ. I changed my mysql code as you recommended. However the
problem remains. As bad as my code was, it worked. It inserted the
information into the database. And there's no error message generated
either with my old code or with your cleaned up code. It just doesn't
see the $sub category at all. Any other ideas?

Remove the row from the table and add this before the mysql_query line:-

echo "[" & $q & "]<br>\n";
if(! isset($sub))
echo '$sub is not set!<br>';


As one would expect, it returns $sub is not set!

Now back to my original question:

if ($q == "red") {
$q = "tp"; //change $q to this value
$sub = "ak"; //attach this value to the $sub variable
} else {
$q = $q; //don't change the value of $q
$sub = ""; //Leave $sub empty
}

I can get it to recognize the $q from the $_POST array with no problem
but it isn't recognizing the $sub that wasn't part of the original
$_POST variables. Is there some problem with my trying to set the $sub
variable with the above if statement? If so, what is the correct way to
set that variable manually?


$sub = "ak"; as per your statement. You didn't say what $q contained.
Furthmore your else condition is redundent.

Try :-

$sub = '';
if($q == "red")
{
echo "q was red";
$q = "tp";
$sub = "ak";
}
Jul 17 '05 #8

P: n/a
CJ Llewellyn wrote:
"Jack" <no****@noemail.com> wrote in message
news:0JJDc.163535$3x.4031@attbi_s54...
CJ Llewellyn wrote:

"Jack" <no****@noemail.com> wrote in message
news:uzIDc.125698$Sw.55866@attbi_s51...
-snip-

Remove the row from the table and add this before the mysql_query line:-

echo "[" & $q & "]<br>\n";
if(! isset($sub))
echo '$sub is not set!<br>';

As one would expect, it returns $sub is not set!

Now back to my original question:

if ($q == "red") {
$q = "tp"; //change $q to this value
$sub = "ak"; //attach this value to the $sub variable
} else {
$q = $q; //don't change the value of $q
$sub = ""; //Leave $sub empty
}

I can get it to recognize the $q from the $_POST array with no problem
but it isn't recognizing the $sub that wasn't part of the original
$_POST variables. Is there some problem with my trying to set the $sub
variable with the above if statement? If so, what is the correct way to
set that variable manually?

$sub = "ak"; as per your statement. You didn't say what $q contained.
Furthmore your else condition is redundent.


And therin lied my problem. There was another if/else a little further
on that was resetting the $sub to ''. Removing the else statements
solved the problem.
Try :-

$sub = '';
if($q == "red")
{
echo "q was red";
$q = "tp";
$sub = "ak";
}

It was a variation of your suggestion here that helped me find the
problem. By adding several of these in different parts of the script and
exiting before the mysql query, I narrowed down where the problem was
coming from.

It now works as intended. Thank you so much for your patience and
assitsance.

Jul 17 '05 #9

P: n/a
FLEB wrote:
Regarding this well-known quote, often attributed to Jack's famous "Mon, 28
Jun 2004 00:35:08 GMT" speech:

if ($q == "red") {
$q = "tp"; //change $q to this value
$sub = "ak"; //attach this value to the $sub variable
} else {
$q = $q; //don't change the value of $q
$sub = ""; //Leave $sub empty
}

I can get it to recognize the $q from the $_POST array with no problem
but it isn't recognizing the $sub that wasn't part of the original
$_POST variables. Is there some problem with my trying to set the $sub
variable with the above if statement? If so, what is the correct way to
set that variable manually?

First off, your code would be easier to sort out, as well as more portable
and secure, using the $_POST superglobal, instead of just the
register_globals automation. So, with that:

<?php
if ($_POST['q'] == "red") {
$q = "tp"; //change $q to this value
$sub = "ak"; //attach this value to the $sub variable
} else {
$q = $_POST['q']; // assign $q to the POSTed value
$sub = ""; // Leave $sub empty
}
?>


I had previously extracted the $_POST value of $q before running the if
statement. The value red was being properly passed all along. I
understand what your saying with your example and would definitely use
it had the $q value not already been extracted.

That said, have you tried some rough debugging?

<?php
echo "POST value of q = '" . $_POST['q'] . "'<br>\n";

if ($_POST['q'] == "red") {
$q = "tp"; //change $q to this value
$sub = "ak"; //attach this value to the $sub variable
echo "Posted q was 'red'. Now \$q is $q and \$sub is $sub<br>\n";
} else {
$q = $_POST['q']; // assign $q to the POSTed value
$sub = ""; // Leave $sub empty
echo "Posted q was NOT 'red'. Now \$q is $q and \$sub is $sub<br>\n";
}

echo "FINAL RESULT: \$q = '$q', \$sub = $sub";
?>

See what you come up with there. If you find that it's not detecting the
"redness", there might be extraneous characters getting passed in the POST.
Try perhaps using a strpos() search, a preg_match() search, or even just
trim()-ming and rtrim()-ming the posted value before you check it.


It was a variation of the code such as you posted that allowed me to
find that the problem was being caused by a second if/else statement
that was resetting the $sub value to ''. Eliminating both else portions
rectified the problem.

Thank you for your suggestions. Much obliged.

Jul 17 '05 #10

P: n/a
"Jack" <no****@noemail.com> wrote in message
news:WxUDc.123748$HG.47924@attbi_s53...
And therin lied my problem. There was another if/else a little further
on that was resetting the $sub to ''. Removing the else statements
solved the problem.


And this is why you (and all others ) are encouraged to post small, complete
examples demonstrating any perceived problem. Often, the simple act of
preparing the example will lead you to the source of the bug.

- Virgil
Jul 17 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.