Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-18-2011, 02:52 PM   PM User | #1
sotrek
New to the CF scene

 
Join Date: Jan 2011
Posts: 8
Thanks: 2
Thanked 0 Times in 0 Posts
sotrek is an unknown quantity at this point
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..
sotrek is offline   Reply With Quote
Old 01-18-2011, 04:46 PM   PM User | #2
DataTalk
Regular Coder

 
Join Date: Nov 2010
Location: Oregon
Posts: 219
Thanks: 19
Thanked 10 Times in 10 Posts
DataTalk is an unknown quantity at this point
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..
DataTalk is offline   Reply With Quote
Users who have thanked DataTalk for this post:
sotrek (01-18-2011)
Old 01-18-2011, 05:42 PM   PM User | #3
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
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.
__________________
Fumigator is offline   Reply With Quote
Old 01-18-2011, 05:49 PM   PM User | #4
sotrek
New to the CF scene

 
Join Date: Jan 2011
Posts: 8
Thanks: 2
Thanked 0 Times in 0 Posts
sotrek is an unknown quantity at this point
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
sotrek is offline   Reply With Quote
Old 01-18-2011, 05:51 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,647
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
_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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 01-18-2011, 06:02 PM   PM User | #6
sotrek
New to the CF scene

 
Join Date: Jan 2011
Posts: 8
Thanks: 2
Thanked 0 Times in 0 Posts
sotrek is an unknown quantity at this point
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
sotrek is offline   Reply With Quote
Old 01-18-2011, 06:03 PM   PM User | #7
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,647
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
I didn't ask you whether you are using request, I asked you to post your html form.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 01-18-2011, 06:07 PM   PM User | #8
sotrek
New to the CF scene

 
Join Date: Jan 2011
Posts: 8
Thanks: 2
Thanked 0 Times in 0 Posts
sotrek is an unknown quantity at this point
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..
sotrek is offline   Reply With Quote
Old 01-18-2011, 06:16 PM   PM User | #9
venegal
Gütkodierer


 
Join Date: Apr 2009
Posts: 2,127
Thanks: 1
Thanked 426 Times in 424 Posts
venegal has a spectacular aura aboutvenegal has a spectacular aura about
Quote:
<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.
venegal is offline   Reply With Quote
Users who have thanked venegal for this post:
sotrek (01-18-2011)
Old 01-18-2011, 06:19 PM   PM User | #10
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,647
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 01-18-2011, 06:27 PM   PM User | #11
sotrek
New to the CF scene

 
Join Date: Jan 2011
Posts: 8
Thanks: 2
Thanked 0 Times in 0 Posts
sotrek is an unknown quantity at this point
thank you guys..

that was it

stupid of me..
sotrek is offline   Reply With Quote
Reply

Bookmarks

Tags
$_get, empty value, newbee, php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:28 PM.


Advertisement
Log in to turn off these ads.