SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist
    Join Date
    May 2003
    Posts
    592
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Passing parameters and getting the arguments

    Hi,

    With a uri like http://domainname/datetime.php?name=...il=us@work.net

    and the file datetime.php as follows:

    PHP Code:
    <?php
    getparms
    ();

    @
    mysql_pconnect("localhost""dbuser""dbpwd") or die ("cannot connect to database");
    mysql_select_db("dbname");

    //Obtain the date and time in the format yyyy-mm-dd hh:mm:ss
    $date date ("Y-m-d H:i:s");
    $name $parameters[0];
    $company $parameters[1];
    $email $parameters[2];
    mysql_query ("insert into test values (NULL, '$date','$name','$company','$email')") or die(mysql_error());

    function 
    getparms(){
        
    // receive all parameters in the $parameters array:
        
    $parameters func_get_args();
        
    // ...
        
    return $result;
    }
    ?>
    Using the echo command, all that appears to work is the evaluation of date, and a row does get inserted in the db.

    1. Does there actually have to be a function called 'func_get_args' ? I've searched the internet, and this function is used considerably in PHP, but I cannot find the actual function, so I assume it is 'part' of PHP ?

    2. How can I modify the PHP code to 'drop out' if no arguments/parms are passed ?

    3. Is the syntax of pasing the parameters correct, now as http://domainname/datetime.php?name=...il=us@work.net

    4. How should the syntax of the uri above change when called/used from within another PHP file, or HTML or JS ?

    Thanks,

    Peter

  2. #2
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    not really sure what you are trying to do

    1) no , http://www.php.net/func_get_args is not really useful here.
    2) .. loads of ways , 1 possible way shown below
    3) yes !
    4) leave it as it is it will work fine

    PHP Code:
    <?
    <?php 
    /*array of expecyed GET variables*/
    $expected = array( 'name' 'company' 'email' );
    /*loop though the expected vars ,
     check for a corresponding $_GET variable*/
    foreach( $expected as $var ){
        if( !isset( 
    $_GET[$var] ) ){
    /*variable is missing exit or redirect etc*/
            
    echo 'error';die();
        }else{
            
    $var $_GET[$var];
        }
    }
    $date date "Y-m-d H:i:s" ); 
    echo 
    "insert into test values (NULL, '$date','$name','$company','$email')"  ;
    ?> 
    ?>
    .
    note that ideally you should also be validating the variables passed , i.e. is it an email address ? , if a database is expecting an integer is that what its actually getting ? a string will break the query etc etc

  3. #3
    SitePoint Evangelist
    Join Date
    May 2003
    Posts
    592
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Passing parameters and getting the arguments

    Hi,

    Quote Originally Posted by firepages
    not really sure what you are trying to do
    It's a bit complex for a newbie like me, and there are 3 parts. The final part is calling the PHP file, passing the parms, and inserting a row in a MySQL db. Because I'm having trouble with parts 1 & 2, I thought I would tackle it 'backwards'; make sure the final part works okay.

    No , http://www.php.net/func_get_args is not really useful here.
    Okay, reading up more on _that_ function, it seems to be only when the (PHP) function is within the same PHP file. Thanks for your answers on the other questions, and for the suggested code.

    I modified the code as you suggested, and now get a message displaying "error". Because I believe you are a PHP guru, I don't see that the problem is your code. Chewing on it for a while, I then remembered that the hosting site are only running PHP vers 4.1.2 , and in fact the version on the domain that I'm (trying) to support is even a lower version, PHP 4.0.4pl1 . I seem to remember something a few weeks ago about later versions needing to do $_GET , where (I think) the earlier versions do a $GET . Is that right, if so, I'll just remove the underscores as needed.

    note that ideally you should also be validating the variables passed , i.e. is it an email address ? , if a database is expecting an integer is that what its actually getting ? a string will break the query etc etc
    The 'test' db is:

    testid (small integer) auto increment
    datetime (datetime)
    name (vachar(50))
    company (varchar(50))
    email (varchar(50))

    The columns testid and datetime are being inserted okay, and the other variables passed are just strings, although I should do some basic checking on those, like removing unprintable chards, and checking the email address for an '@' and if it contains '.com,.net,etc,etc'

    Many thanks for your help,

    Peter

  4. #4
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no guru yet & I make code errors hourly!
    "PHP 4.0.4pl1 " ouch

    $HTTP_GET_VARS[$var]; will work for that version though

  5. #5
    SitePoint Evangelist
    Join Date
    May 2003
    Posts
    592
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Passing parameters and getting the arguments

    Hi,

    Quote Originally Posted by firepages
    $HTTP_GET_VARS[$var]; will work for that version though
    Thanks, it works like a bought one now.

    Peter


Bookmarks

Posting Permissions

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