SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    midnight coder
    Join Date
    Dec 2000
    Location
    The flat edge of the world
    Posts
    838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Part of my script is a simple add record script, it works fine normally, but when I put in inside a function, it'll submit the record, confirm, and tell me it's successfuly, but when I look at it in PHPMyAdmin, a new row was made, but none of the variables were submitted, only the automatic ones, like Primary Key and Timestamp, here's the code:

    Code:
    // this function adds an item to the db
    function add_record()
    	{
    	// we're adding a new record
    	$sql = mysql_query ("INSERT INTO items (title, blah1, blah2) 
    	VALUES ('$title', '$blah1', '$blah2')") or die (mysql_error());
    			
    
    	// run SQL against the DB to confirm
    	$result = mysql_query($sql);
    	echo "<p>Record updated/edited!</p>";
    	}
    
    
    
    // if the Add Record button was pushed
    if ($add_record)
    	{
    
    	// connect to MySQL database
    	$dbcnx = mysql_connect($site, $username, $password) or die (mysql_error());
    	mysql_select_db($dbName, $dbcnx);
    
    	add_record();
    
    	} // end of form submition
    The script runs fine, if the instructions inside the function add_record() was inside the "if ($add_record)", but I have no idea why it will not submit variables once it's inside the function(), I even get the message "Record updated/edited!"

  2. #2
    SitePoint Wizard silver trophy Karl's Avatar
    Join Date
    Jul 1999
    Location
    Derbyshire, UK
    Posts
    4,411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try adding this at the top of your function:

    globals $title, $blah1, $blah2;
    Karl Austin :: Profile :: KDA Web Services Ltd.
    Business Web Hosting :: Managed Dedicated Hosting
    Call 0800 542 9764 today and ask how we can help your business grow.

  3. #3
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    That, or pass the variables in as parameters.
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    or pass the whole $HTTP_POST_VARS in as a parameter or globalize it then use extract() inside the function
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    midnight coder
    Join Date
    Dec 2000
    Location
    The flat edge of the world
    Posts
    838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, I understand Karl's post about putting global to $title, $blah1, $blah2, etc. But I dunno how to pass the variables as parameters or that thing about $HTTP_POST_VARS...can you please explain how I can do that, and why?

  6. #6
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well let's say you have a form that posts to a php script that then puts the form data into a database with the given funtion that you provided above.

    1) You need to somehow get the form data in to the function

    So you could gloablize each var which could be a pain in the as
    function add_record()
    {
    global $var1, $var2, $var3, etc....
    // we're adding a new record
    $sql = mysql_query ("INSERT INTO items (title, blah1, blah2)
    VALUES ('$title', '$blah1', '$blah2')") or die (mysql_error());


    // run SQL against the DB to confirm
    $result = mysql_query($sql);
    echo "<p>Record updated/edited!</p>";
    }


    Or you could use the array that is automatically available to you trhough the post that the form just made called $HTTP_POST_VARS

    function add_record()
    {
    global $HTTP_POST_VARS;
    extract($HTTP_POST_VARS);
    // we're adding a new record
    $sql = mysql_query ("INSERT INTO items (title, blah1, blah2)
    VALUES ('$title', '$blah1', '$blah2')") or die (mysql_error());


    // run SQL against the DB to confirm
    $result = mysql_query($sql);
    echo "<p>Record updated/edited!</p>";
    }


    When you posted the form you post a bunch of key/value pairs that were stored in the $HTTP_POST_VARS array like so:
    $HTTP_POST_VARS["var1"]
    $HTTP_POST_VARS["var2"]
    $HTTP_POST_VARS["var3"]
    etc...
    by using extract you end up with
    $var1
    $var2
    $var3
    etc...

    Does that make more sense?
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  7. #7
    midnight coder
    Join Date
    Dec 2000
    Location
    The flat edge of the world
    Posts
    838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh ok, it makes more sense now, I'll try those methods and see how I get on.

    Another question, what is the advantage of putting instructions into functions? I thought of that you could call the function multiple times instead of copying the same code all over again, but what other advantages are there in using function ()? And are there any performance hits and speed difference?


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
  •