5 Replies - 1438 Views - Last Post: 29 March 2010 - 10:33 AM Rate Topic: -----

#1 eddiehm  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 76
  • Joined: 09-March 10

Problem with $_GET

Posted 26 March 2010 - 09:37 AM

Hey everyone I am having problems with my coding. I am able to get my variable to transfer from my view page to my edit page but when I click my submit button I lose what was in my variable in my url.

	echo $_GET['id'];
	
	if($_POST['submit']){
		
		
		
		$updatesql = "UPDATE reps SET fname = '" . $_POST['fname'] . "', lname = '" . $_POST['lname'] . "', email = '" . $_POST['email'] . "' WHERE repid = '" . $_GET['id'] . "'; ";
		$updateresult = mysql_query($updatesql);
		
		echo $_GET['id'];
		echo $updatesql;
		
		/*if($_POST['fname'] == ''){
			echo ' Oops looks like you deleted the reps name. Please go back and add one in.';
		}else if($_POST['lname'] == ''){
			echo ' Oops looks like you deleted the reps last name. Please go back and add one in.';
		}else if($_POST['email'] == ''){
			echo ' Oops looks like you deleted the reps email. Please go back and add one in.';
		}else{
			header("Location:"  . $config_basedir . "viewreps.php");
		}*/
		
		
	}
	
	require('header.php');
	
	
	$sql = "SELECT * FROM reps WHERE repid =  '" . $_GET['id'] . "';";
	$result = mysql_query($sql);
	$numrows = mysql_num_rows($result);
	
	echo "<form action='$SCRIPT_NAME' id='edit' method='post'>";
	while($reprows = mysql_fetch_assoc($result)){
		echo "First Name<br /><input type='text' name='fname' id='fname' value='" . $reprows['fname'] . "' /><br /> ";
		echo "Last Name<br /><input type='text' name='lname' id='lname' value='" . $reprows['lname'] . "' /><br />";
		echo "Email<br /><input type='text' name='email' id='email' value='" . $reprows['email'] . "' />";
	}
	echo "<br /><input type='submit' name='submit' value='Edit Rep' />";
	echo "</form>";



If anyone could help that would be awesome.
Thanks

This post has been edited by eddiehm: 26 March 2010 - 09:40 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Problem with $_GET

#2 JennaPeterson88  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 39
  • Joined: 17-March 10

Re: Problem with $_GET

Posted 26 March 2010 - 05:19 PM

You're saying that when you hit submit you loose the value of $_GET['id'], correct?
If that's the case, the easiest solution is to include it in your form.
Do it just like your other input boxed, but change the type from text to hidden, and include the value of $_GET['id'] in the value='' part of the tag.

It's not recommended, however, to plug $_GET[''] directly into your queries and other codes. Try declaring a variable set to that value first.
$id = $_GET['id'];

Was This Post Helpful? 1
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,612
  • Joined: 23-August 08

Re: Problem with $_GET

Posted 26 March 2010 - 05:23 PM

I find it sort of curious that you've got $_GET and $_POST in here
echo $_GET['id'];
        
if($_POST['submit']){



Are you getting to this page via GET, or via POST?
Was This Post Helpful? 0
  • +
  • -

#4 eddiehm  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 76
  • Joined: 09-March 10

Re: Problem with $_GET

Posted 28 March 2010 - 07:44 AM

I'm getting to the page via $_GET but I am trying to $_post it to my database. I have the $_GET there because I was trying to figure out where I was losing my url id.
Was This Post Helpful? 0
  • +
  • -

#5 JennaPeterson88  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 39
  • Joined: 17-March 10

Re: Problem with $_GET

Posted 28 March 2010 - 05:25 PM

If the through the process of inputting to/updating your database the page refreshes, that's where you're losing the id value, because it's no longer in the URL. If you would like to keep it as a $_GET instead of then making it into a post (which my previous suggestion would do) you can simply include ?id=$id after the file name in the action declaration of your form tags.
Was This Post Helpful? 1
  • +
  • -

#6 eddiehm  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 76
  • Joined: 09-March 10

Re: Problem with $_GET

Posted 29 March 2010 - 10:33 AM

Thanks for the help I got it working!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1