473,404 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

$_POST variable problem

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
10 2639
"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
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
"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
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
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
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
"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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Dan | last post by:
I was trying to troubleshoot a login page that doesn't work - it keeps saying the login/password is missing - when my tracing discovered this peculiar behavior. register_globals is off, so at...
10
by: arun.kumar.varma | last post by:
Hi, I'm learning PHP and prepared a simple login form. validate.php does the validation and it was behaving erratically. So, I did var_dump of $_POST variable and it's NULL. Did I miss anything...
1
by: Gloriajw | last post by:
This is an odd problem. My _POST variable is always set. But it is not visible to this function until I do a print_r($_POST); No matter where I do this in the file, even if it's within the 'if'...
12
by: Todd Michels | last post by:
Hi all, I am trying to send data from a form and insert it into a MSSQL DB. When I submit the data I get: Warning: mssql_query() : message: The name "Todd" is not permitted in this context....
9
by: shreedhan | last post by:
Hi I'm recenlty studying PHP myself and I have got a problem in passing variables to one php script from another i have following code as index.php <?php $name=HREEDN ?> <html> <head>
2
by: keeps21 | last post by:
I have a script that recieves an id number via the address bar when a link is clicked. ie . index.php?id=1 if the link was for the story whose ID is 1. My script checks if a user is logged in,...
4
by: mtuller | last post by:
I have a page that submits data to a database, and I want to make it so that if the page is refreshed, it doesn't submit the information again. I am trying to use unset the variables so that if the...
10
by: Mason Barge | last post by:
I have a standard POST form consisting of two types of input: text input and textarea. The form downloads current settings from a mysql database. The user can update the information by modifying...
0
by: Gilles Ganault | last post by:
Hello I use a POST variable named "status" to show different screens while keeping everything in the same script. The problem is that in the Print screen, I need to empty this variable so the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.