11 Replies - 358 Views - Last Post: 12 October 2017 - 02:11 PM Rate Topic: -----

#1 hackеr  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 31
  • Joined: 18-February 16

$_GET['id'] not working

Posted 12 October 2017 - 07:52 AM

Hey,
for some reason my $_GET['id'] is not getting the id, I have no idea why. When I try to echo the $PostId, nothing comes up meaning that it is empty, but I don't get why. Can you guys take a look and see if you find the problem?

Here is my code:
<?php require_once("include/db.php"); ?>
<?php require_once("include/sessions.php"); ?>
<?php require_once("include/functions.php"); ?>
<?php  
    if(isset($_POST["Submit"])){
        $Name=mysqli_real_escape_string($Connection, $_POST["Name"]);
        $Email=mysqli_real_escape_string($Connection, $_POST["Email"]);
        $Comment=mysqli_real_escape_string($Connection, $_POST["Comment"]);
        date_default_timezone_set("America/Denver");
        $CurrentTime=time();
        $DateTime=strftime("%m-%d-%Y %H:%M:%S", $CurrentTime);
        $DateTime;
        $PostId=$_GET['id'];
        if(empty($Name) || empty($Email) || empty($Comment)){
            $_SESSION["ErrorMessage"]="All fields are required!";  
        } else if(strlen($Email) < 5){
            $_SESSION["ErrorMessage"]="Please enter a valid email!";
        } else if(strlen($$Comment) > 1000){
            $_SESSION["ErrorMessage"]="Comment should not be more than 1000 characters!";
        } else {
            global $Connection;
            $Query="INSERT INTO comments (datetime, name, email, comment, status)
                VALUES('$DateTime','$Name' ,'$Email','$Comment', 'OFF');";
            $Execute=mysqli_query($Connection,$Query);
            if($Execute){
                $_SESSION["SuccessMessage"]="Comment Submitted Successfully!";
                redirect_to("fullPost.php?id={$PostId}");
                echo $PostId;
            } else {
                $_SESSION["ErrorMessage"]="Comment Failed to Submit!";
                redirect_to("fullPost.php?id={$PostId}");
            }
        }
    }
?>


db.php has the $Connection; which connects to the database.

sessions.php starts the session and has two functions, one for the ErrorMessage and another for the SuccessMessage.

functions.php has:
<?php
   function redirect_to($New_Location){
      header("location:".$New_Location);
      exit();
   }
?>


Is This A Good Question/Topic? 0
  • +

Replies To: $_GET['id'] not working

#2 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6549
  • View blog
  • Posts: 30,681
  • Joined: 10-May 07

Re: $_GET['id'] not working

Posted 12 October 2017 - 08:03 AM

Is the value of method from your input form GET or POST?
Was This Post Helpful? 0
  • +
  • -

#3 hackеr  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 31
  • Joined: 18-February 16

Re: $_GET['id'] not working

Posted 12 October 2017 - 08:07 AM

The method of the form is POST.
<form action="fullPost.php?id=<?php echo $PostID; ?>" method="POST" enctype="multipart/form-data">
   <fieldset>
      <div class="form-group">
         <label for="name"><span class="fieldInfo">Name:</span></label>
	 <input class="form-control" type="text" name="Name" id="name" placeholder="Name">
      </div>
      <div class="form-group">
         <label for="email"><span class="fieldInfo">Email:</span></label>
	 <input class="form-control" type="text" name="Email" id="email" placeholder="Email">
      </div>
      <div class="form-group">
         <label for="commentarea"><span class="fieldInfo">Comment:</span></label>
	 <textarea class="form-control" name="Comment" id="commentarea" placeholder="Please enter your comment here.."></textarea>
      </div>
      <br>
      <input class="btn btn-success" type="Submit" name="Submit" value="Add New Comment">
   </fieldset>
</form>

Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3714
  • View blog
  • Posts: 13,469
  • Joined: 08-August 08

Re: $_GET['id'] not working

Posted 12 October 2017 - 08:13 AM

Deprecated MySQL functions are simpler to use (not counting all the sanitizing you need to add) than PDO or MySQLi prepared statements, but far less secure. That's why they've been deprecated. If you're just learning PHP you should start with
http://www.dreaminco...duction-to-pdo/
or
http://php.net/manua...-statements.php
When dealing with old code that uses deprecated MySQL functions you should replace them if possible. If it's not possible, your experience with PDO or MySQLi will benefit you when dealing with MySQL functions.
Was This Post Helpful? 0
  • +
  • -

#5 hackеr  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 31
  • Joined: 18-February 16

Re: $_GET['id'] not working

Posted 12 October 2017 - 08:18 AM

Thank you, I'll make sure to read the information on those two links.
I've been doing this online course and I guess all the information is outdated.
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4135
  • View blog
  • Posts: 13,056
  • Joined: 08-June 10

Re: $_GET['id'] not working

Posted 12 October 2017 - 08:26 AM

for debugging you should find out, what's actually in your $_POST/$_GET.
Was This Post Helpful? 0
  • +
  • -

#7 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1829
  • View blog
  • Posts: 5,758
  • Joined: 15-January 14

Re: $_GET['id'] not working

Posted 12 October 2017 - 10:42 AM

Is the problem that you're not printing the ID the the form action URL? Is it blank there? Because if it's blank there, it's still going to be blank when you submit the form.
Was This Post Helpful? 0
  • +
  • -

#8 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6549
  • View blog
  • Posts: 30,681
  • Joined: 10-May 07

Re: $_GET['id'] not working

Posted 12 October 2017 - 10:53 AM

View Posthackеr, on 12 October 2017 - 11:07 AM, said:

	 <input class="form-control" type="text" name="Name" id="name" placeholder="Name">
	 <input class="form-control" type="text" name="Email" id="email" placeholder="Email">
	 <textarea class="form-control" name="Comment" id="commentarea" placeholder="Please enter your comment here.."></textarea>
      <input class="btn btn-success" type="Submit" name="Submit" value="Add New Comment">



Based on this form, what do you expect the value of $_GET['id'] to be?
Was This Post Helpful? 0
  • +
  • -

#9 hackеr  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 31
  • Joined: 18-February 16

Re: $_GET['id'] not working

Posted 12 October 2017 - 12:36 PM

@ArtificialSoldier

Yes, the ID in the URL after I click the submit button is blank "fullPost.php?id=", but before I submit the form there is an id "fullPost.php?id=5"

@no2pencil

Based on that code alone, I expect the id to be blank, but in the form action I specify "fullPost.php?id=<?php echo $PostID; ?>", and the id of the URL where the form is located is specified. I don't know if that makes a difference.

Also, I have two variables $Id and $PostIDFromURL which return the correct id, I don't know how to pass either one back to the top where I need to for the redirect_to function.
Was This Post Helpful? 0
  • +
  • -

#10 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6549
  • View blog
  • Posts: 30,681
  • Joined: 10-May 07

Re: $_GET['id'] not working

Posted 12 October 2017 - 01:25 PM

View Posthackеr, on 12 October 2017 - 03:36 PM, said:

@no2pencil

Based on that code alone, I expect the id to be blank, but in the form action I specify "fullPost.php?id=<?php echo $PostID; ?>", and the id of the URL where the form is located is specified. I don't know if that makes a difference.

Why not use input type=hidden & pull it from the $_POST array?
Was This Post Helpful? 0
  • +
  • -

#11 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1829
  • View blog
  • Posts: 5,758
  • Joined: 15-January 14

Re: $_GET['id'] not working

Posted 12 October 2017 - 01:47 PM

Quote

Yes, the ID in the URL after I click the submit button is blank "fullPost.php?id=", but before I submit the form there is an id "fullPost.php?id=5"

That doesn't make sense. The form will go to the exact action that is specified, unless you have some Javascript running that changes the action or something. If you pull up that page, and use your developer tools to inspect the form and you see that the ID is in the action URL, then that's exactly where it's going to submit to. There's no reason the browser would strip part of the URL when it submits the form, it just doesn't do that. Use your developer tools to verify that the action in the form tag is actually set to what you think it is, I bet it's blank there too and then you need to work backwards and figure out where it's getting lost.
Was This Post Helpful? 0
  • +
  • -

#12 hackеr  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 31
  • Joined: 18-February 16

Re: $_GET['id'] not working

Posted 12 October 2017 - 02:11 PM

@ ArtificialSoldier

Thank you! I checked and it really wasn't there so I plugged in the other id variable and it works now.


Thank you guys for your help!

I just picked up a few PHP 7 books
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1