Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation $_GET not working

    Hi guys,

    i have a bit of a tedious problem.

    i have the following code which performs an update.
    Code:
                           $id = mysql_prep($_GET['subj']);
    			
    			$menu_name = mysql_prep($_POST['menu_name']);
    			$position = mysql_prep($_POST['position']);
    			$visible = mysql_prep($_POST['visible']);
    			$query = "UPDATE subjects SET menu_name = '$menu_name', position = '$position', visible = '$visible' WHERE id = '$id'";
    			$result = mysql_query($query, $connection);
    			print_r($_REQUEST);
    At the point where i am trying to GET the subj from my url : http://blablabal.edit_subject.php?subj=1
    the GET returns no value. it should return 1 in this case..instead its empty.

    anyone would know why?
    The output i get from print_r
    print_r($_REQUEST);
    is: Array ( [subj] => [menu_name] => Produktion [position] => 2 [visible] => 1 [submit] => Edit Subject )

    I have attached a bigger part of the code below for your consideration
    Thanks!

    Code:
    if (isset($_POST['submit'])) {
    		$errors = array();
    	
    		//Form Validation
    		$required_fields = array('menu_name', 'position', 'visible');
    		foreach($required_fields as $fieldname) {
    			if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && $_POST[$fieldname] != 0)) {
    				$errors[] = $fieldname;
    			}
    		}
    	
    		$fields_with_lengths = array('menu_name' => 30);
    		foreach($fields_with_lengths as $fieldname => $maxlength) {
    			if (strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength) {
    				$errors[] = $fieldname; 
    			}
    		}
    	
    		
    		if (empty($errors)) {
    			//Perform Update
    			$id = mysql_prep($_GET['subj']);
    			
    			$menu_name = mysql_prep($_POST['menu_name']);
    			$position = mysql_prep($_POST['position']);
    			$visible = mysql_prep($_POST['visible']);
    			$query = "UPDATE subjects SET menu_name = '$menu_name', position = '$position', visible = '$visible' WHERE id = '$id'";
    			$result = mysql_query($query, $connection);
    			print_r($_REQUEST);
    			
    			if (mysql_affected_rows() == 1) {
    				//Sucess
    				
    			} else {
    				//Failed
    				
    				$message = "The subject update failed." . mysql_error();
    				
    			}	
    		} else {
    			//Errors occured
    			
    		}
    		
    		} //end of: if (isset($_POST['submit']))
    Last edited by sotrek; 01-18-2011 at 05:12 PM.

  • #2
    Regular Coder
    Join Date
    Nov 2010
    Location
    Oregon
    Posts
    243
    Thanks
    23
    Thanked 10 Times in 10 Posts
    your code ->tags should have ending code tags [/CODE]

    Where does it start - Where does it end?
    Last edited by DataTalk; 01-18-2011 at 05:11 PM.

  • Users who have thanked DataTalk for this post:

    sotrek (01-18-2011)

  • #3
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Is there a form element named "subj", so that $_POST['subj'] would be being used as $_REQUEST['subj']? Try print_r on $_GET rather than $_REQUEST and see what values you have.

  • #4
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I am not sure what you mean by 'form element' but subj exists on the link of the page that i submit my form from. (http://xxx.local/~xxx/widget_corp/ed...ect.php?subj=1)

    I have tried print_r ($_GET) and the output i get is Array ( [subj] => )
    which means that the subj is an empty string i guess. right?

    any more ideas?
    thanks

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    _REQUEST is a problem since its GET, POST and COOKIE by default all merged together in that order. If a POST or COOKIE is provided it will be included.
    Post your HTML form.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #6
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I am not using _REQUEST at any point.

    I am just using $_GET and $_POST
    $_POST works..$_GET doesn't

    basically i need to retrieve the value of 1 from 'subj' on my link:
    http://blabla/edit_subject.php?subj=1
    in some way so i can add it to my UPDATE statement.

    $_GET has worked in the past on other parts of the website i m building without a problem..

    thanks

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    I didn't ask you whether you are using request, I asked you to post your html form.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #8
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Sorry i m still a newbee.

    there you go:

    Code:
    <?php require_once("includes/connection.php"); ?>
    <?php require_once("includes/functions.php"); ?>
    
    <?php 
    	
    	
    	if (isset($_POST['submit'])) {
    		$errors = array();
    	
    		//Form Validation
    		$required_fields = array('menu_name', 'position', 'visible');
    		foreach($required_fields as $fieldname) {
    			if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && $_POST[$fieldname] != 0)) {
    				$errors[] = $fieldname;
    			}
    		}
    	
    		$fields_with_lengths = array('menu_name' => 30);
    		foreach($fields_with_lengths as $fieldname => $maxlength) {
    			if (strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength) {
    				$errors[] = $fieldname; 
    			}
    		}
    	
    		
    		if (empty($errors)) {
    			//Perform Update
    			if (isset($_GET['subj'])){
    				$id = mysql_prep($_GET['subj']);
    			}
    			
    			
    			$menu_name = mysql_prep($_POST['menu_name']);
    			$position = mysql_prep($_POST['position']);
    			$visible = mysql_prep($_POST['visible']);
    			$query = "UPDATE subjects SET menu_name = '$menu_name', position = '$position', visible = '$visible' WHERE id = '$id'";
    			$result = mysql_query($query, $connection);
    			
    			
     print_r($_GET);
     if($_GET["subj"] === "") echo "subj is an empty string\n";
     if($_GET["subj"] === false) echo "subj is false\n";
     if($_GET["subj"] === null) echo "subj is null\n";
     if(isset($_GET["subj"])) echo "subj is set\n";
     if(!empty($_GET["subj"])) echo "subj is not empty";
    
    			
    
    	
    	
    			if (mysql_affected_rows() == 1) {
    				//Sucess
    				
    			} else {
    				//Failed
    				
    				$message = "The subject update failed." . mysql_error();
    				
    			}	
    		} else {
    			//Errors occured
    			
    		}
    		
    		} //end of: if (isset($_POST['submit']))
    
    ?>
    <?php find_selected_page();	?>
    <?php include ("includes/header.php"); ?>
    				<table id="structure">
    					<tr>
    						<td id="navigation">
    							<?php echo navigation ($sel_subject, $sel_page); ?>
    						
    						<td id="page">
    							<h2> Edit Subject: <?php echo $sel_subject["menu_name"]; ?></h2>
    							<?php if (!empty($message)) {
    								echo "<p class=\"message\">" . $message . "</p>";
    							}?>
    							<?php 
    							//output a list of the fields that had errors
    							if(!empty($errors)) {
    								echo "<p class=\"errors\">";
    								echo "Please review the following fields:<br />";
    								foreach($errors as $error) {
    									echo " - " . $error . "<br />";
    								}
    							}
    							?>
    							
    							<form action="edit_subject.php?subj=<?php urlencode($sel_subject["id"]); ?>" method="post" >
    								<p>Subject name:
    									<input type="text" name="menu_name" value="<?php echo $sel_subject['menu_name']; ?>" id="menu_name" />
    								</p>
    								<p>Position :
    									<select name="position">
    										<?php 
    											$subject_set = get_all_subjects();
    											$subject_count = mysql_num_rows($subject_set);
    											//$subject_count + 1 because we are adding a subject
    											for($count=1; $count <= $subject_count+1; $count++) {
    												echo "<option value=\"{$count}\"";
    												if($sel_subject['position'] == $count) {
    													echo " selected";
    												}
    												
    												echo ">{$count}</option>";
    											}
    										?>
    									</select>
    								</p>
    								<p>Visible :
    									<input type="radio" name="visible" value="0" <?php if ($sel_subject['visible'] == 0) {echo " checked";} ?> /> No
    									&nbsp;
    									<input type="radio" name="visible" value="1" <?php if ($sel_subject['visible'] == 1) {echo " checked";} ?> /> Yes
    								</p>
    								<input type="submit" name="submit" value="Edit Subject" />
    						</form>
    						<br />
    						<a href="content.php">Cancel</a>
    						</td>
    					</tr>
    				</table>
    <?php require ("includes/footer.php");?>
    Last edited by sotrek; 01-18-2011 at 06:10 PM.

  • #9
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    <form action="edit_subject.php?subj=<?php urlencode($sel_subject["id"]); ?>" method="post" >
    You don't echo anything inside the php tags, that's why there's nothing there.

    A glance at the generated HTML source would have given the problem away instantly.

  • Users who have thanked venegal for this post:

    sotrek (01-18-2011)

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    That will be it. Your GET request is in a branch of your POST. The only way it'll be set is its passed on your form action. Its a set variable, but without the echo its empty.
    Alternatively you can pass it through a hidden input and retrieve from post.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #11
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    thank you guys..

    that was it

    stupid of me..


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •