The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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:

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!"

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?

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());

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.

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?