Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Losing $_GET variable inside if clause

Posted on 2009-07-08
9
Medium Priority
?
261 Views
Last Modified: 2013-12-13
Hi Experts!

the variable $_GET['subtask'] is not being posted to the database.

If I remove "isset($_POST['task'] from the if statement, it will work. But it has to be there.

Note that the other variables are being posted correctly to the database in both cases.

Thank you!
<? include 'inc/func.php'; ?>
<? include 'inc/dbc.php'; ?>
 
<? include 'inc/head.php'; ?>
 
<?
if(isset($_GET['subtask']) && isset($_POST['task'])){
	$q = "INSERT INTO tasks (task, parent_id, added) VALUES ('{$_POST['task']}', '{$_GET['subtask']}', NOW())";
	$r = mysql_query($q);
	redirect("index.php");    
}
?>
 
<? include 'inc/head.php'; ?>
 
<form action="new.php" method="post">
	<label>Task: </label><input name="task" id="task" type="text" />
    <br />
    <br />
    <input name="submit" id="submit" type="submit" value="Submit" />
</form>
 
<? include 'inc/foot.php'; ?>

Open in new window

0
Comment
Question by:AphX
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 24804028
Try it like this:
(as indicated in the other question, this may need to be $_GET['task'] as well)
<? include 'inc/func.php'; ?>
<? include 'inc/dbc.php'; ?>
 
<? include 'inc/head.php'; ?>
 
<?
if(isset($_GET['subtask']) && isset($_POST['task'])){
        $q = "INSERT INTO tasks (task, parent_id, added) VALUES ('".$_POST['task']."', '".$_GET['subtask']."', NOW())";
        $r = mysql_query($q);
        redirect("index.php");    
}
?>
 
<? include 'inc/head.php'; ?>
 
<form action="new.php" method="post">
        <label>Task: </label><input name="task" id="task" type="text" />
    <br />
    <br />
    <input name="submit" id="submit" type="submit" value="Submit" />
</form>
 
<? include 'inc/foot.php'; ?>

Open in new window

0
 
LVL 2

Author Comment

by:AphX
ID: 24804114
Hi and thanks for fast response!

It didn't work. I can´t even echo $_GET['subtask'] inside the if clause. The exact same echo works outside the if clause.

As I pointed out above: if I use this if statement instead, it works 100%

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

Something turns wrong when using $_POST['task'] in the if statement...
0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 1800 total points
ID: 24804121
Oh, yeah and you have to ensure that you are passing on the 'subtask' query parameter, either in the post URL or as another value along side task maybe with a hidden variable.

URL method:
<form action="new.php?<?php echo $_SERVER['QUERY_STRING']; ?>" method="post">

Hidden field method:
<input name="subtask" id="subtask" type="hidden" value="<?php echo $_GET['subtask'] ?>" />
(And then change the if to use $_POST['subtask'] instead)
0
Flash Sale! Good things come in big bundles

Save over 50% on our fully managed dedicated server bundle for Labor Day. Plus FREE Guardian Backups, FREE Advanced DDoS Protection and FREE Plesk Onyx Web Pro Edition.

 
LVL 60

Expert Comment

by:Kevin Cross
ID: 24804148
Remember that the $_POST['task'] is only set after the form has been posted; therefore, you have to test that way.  Otherwise, the IF condition will fail as $_POST['task'] will not be set.
0
 
LVL 27

Assisted Solution

by:Cornelia Yoder
Cornelia Yoder earned 200 total points
ID: 24804471
By the way, NEVER use $_GET or $_POST variables directly in a MySQL query!!   It leaves you wide open to SQL Injection hacking!

Always pass the inputs through a safety function such as mysql_real_escape_string().
0
 
LVL 2

Author Closing Comment

by:AphX
ID: 31601123
Thank you!

Now I see!
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 24804556
Great point, yodercm.  Same on me for not mentioning as I presented a security discussion on cross-site scripting (XSS) on the very subject.  Definitely a no no, especially going to a database as it becomes the gift that keeps on giving. ;)
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 24804560
*Shame NOT same. :)
0
 
LVL 2

Author Comment

by:AphX
ID: 24805484
Thank you! :)
0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question