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 9 of 9
  1. #1
    New Coder
    Join Date
    Jan 2012
    Posts
    84
    Thanks
    49
    Thanked 0 Times in 0 Posts

    PHP $_GET function

    Hello CF,

    Looking to populate a details page with dynamic data from my sql table..

    The link that is going to be clicked would look something like this:

    http://www.hedgevent.com/eventdetail..._Summit?id=124

    what code would I need to add to my eventdetails.php page to tell it to get the id# in the url and use that number to reference data in my calendar_event table using the same id?

  • #2
    Regular Coder
    Join Date
    May 2007
    Posts
    100
    Thanks
    16
    Thanked 12 Times in 12 Posts
    (int) $_GET['id']

    You'll want to cast it as an integer to make sure you are getting a number and to protect against MySQL injection hacks.

    Perhaps use it in a condition like this -

    if ((int) $_GET['id']) mysql_query('SELECT * FROM calendar_event WHERE id = '.(int) $_GET['id'])
    Last edited by >ssp-cdr<; 01-31-2012 at 10:57 PM.

  • Users who have thanked >ssp-cdr< for this post:

    jchrisphonte (01-31-2012)

  • #3
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    PHP Code:
    <?php

    $myid
    =$_GET[id];
    echo 
    $myid;

    // now you can use $myid anywhere you want,
    // in your example, $myid would be equal to 124.
    ?>

    .

  • Users who have thanked mlseim for this post:

    jchrisphonte (01-31-2012)

  • #4
    New Coder
    Join Date
    Jan 2012
    Posts
    84
    Thanks
    49
    Thanked 0 Times in 0 Posts
    Great!! thanks mlseim.

    How do I tell the same php page to connect to my database(hedgevent) and table(calendar_event) and only use the data there for id #124 to fill the echo fields?? ex fields : 'event', 'location', 'city' etc....

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    wow ...
    There's a lot to do for that.

    You should view an online tutorial on: PHP MySQL INSERT

    Too much code for me to provide here.

  • Users who have thanked mlseim for this post:

    jchrisphonte (01-31-2012)

  • #6
    New Coder
    Join Date
    Jan 2012
    Posts
    84
    Thanks
    49
    Thanked 0 Times in 0 Posts
    sorry for being so vague... I guess my question is more about the linking of the id with the table.

    assuming I use this code to connect and select data:

    PHP Code:
    <?php
    $con 
    mysql_connect("localhost","peter","abc123");
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }

    mysql_select_db("my_db"$con);

    $result mysql_query("SELECT * FROM Calendar_event");

    while(
    $row mysql_fetch_array($result))
      {
      echo 
    $row['FirstName'] . " " $row['LastName'];
      echo 
    "<br />";
      }

    mysql_close($con);
    ?>
    how would I tell it to check the id i found in the url and only use that rows info?

  • #7
    Regular Coder
    Join Date
    May 2007
    Posts
    100
    Thanks
    16
    Thanked 12 Times in 12 Posts
    look at my suggestion above

  • Users who have thanked >ssp-cdr< for this post:

    jchrisphonte (01-31-2012)

  • #8
    New Coder
    Join Date
    Jan 2012
    Posts
    84
    Thanks
    49
    Thanked 0 Times in 0 Posts
    thanks ssp for the help -- It did look right...

    just wasn't sure where to include it in the script I have above...


    would it look like this??

    PHP Code:
    <?php
    (int) $_GET['id'];

    if ((int) 
    $_GET['id']) mysql_query('SELECT * FROM calendar_event WHERE id = '.(int) $_GET['id']);

    $con mysql_connect("localhost","peter","abc123");
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }

    mysql_select_db("my_db"$con);

    $result mysql_query("SELECT * FROM Calendar_event");

    while(
    $row mysql_fetch_array($result))
      {
      echo 
    $row['FirstName'] . " " $row['LastName'];
      echo 
    "<br />";
      }

    mysql_close($con);
    ?>

  • #9
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by jchrisphonte View Post
    thanks ssp for the help -- It did look right...

    just wasn't sure where to include it in the script I have above...


    would it look like this??

    PHP Code:
    <?php
    (int) $_GET['id'];

    if ((int) 
    $_GET['id']) mysql_query('SELECT * FROM calendar_event WHERE id = '.(int) $_GET['id']);

    $con mysql_connect("localhost","peter","abc123");
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }

    mysql_select_db("my_db"$con);

    $result mysql_query("SELECT * FROM Calendar_event");

    while(
    $row mysql_fetch_array($result))
      {
      echo 
    $row['FirstName'] . " " $row['LastName'];
      echo 
    "<br />";
      }

    mysql_close($con);
    ?>
    No.
    PHP Code:
    // Get ID from your GET value, casting as integer
    $id = (int) $_GET['id'];

    // Mysql connection stuff here

    // Use that to SELECT from the database the fields you require
    $result mysql_query('SELECT some,fields FROM Calendar_event WHERE id='.$id);
    if(!
    $result){
        
    // Do some error reporting here, with a die or exit maybe. Example
        
    die(mysql_error());
    }
    // Seeing as script will be killed if the mysql failed, ok to assume mysql OK if down here

    // Loop through results row by row
    while($row mysql_fetch_assoc($result)){
        
    // Do something with your selected rows. In this case, $row['some'] and $row['fields']

    All of this could have been found by a google search, and a simple php mysql tutorial like mlseim said
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • Users who have thanked BluePanther for this post:

    jchrisphonte (02-01-2012)


  •  

    Posting Permissions

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