How to put this in a function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
seeker2921
Forum Contributor
Posts: 120
Joined: Sat Mar 22, 2003 7:10 pm
Location: Wiesbaden Germany
Contact:

How to put this in a function

Post by seeker2921 »

How do I make a form work with this function? I tried just having the action be the file that it is in but it didn;t work note that the form and function below are all in one page..

Function
їphp]
function addentry(){
$query = "INSERT INTO $table VALUES ('NULL','$date','$topic','$entry')";
$result = mysql_query($query);
if ($result) {
echo("done");
} else {
echo ("Entry not added. There was a problem somewhere, fix it.");
}
}
ї/php]

Also I have a mysql connection that I want to place in a funtion as well in the config file included to this page how would I make thethe query above use the fuction?
?>
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

$query = "INSERT INTO $table VALUES ('NULL','$date','$topic','$entry')";
If these values are coming from the form post, then you'll need to do either...
a) Make sure you have register_globals on and do..
function addentry(){
global $date, $topic, $entry;
....

b) function addentry(){
global $table; //presuming table isn't posted, but is 'set' elsewhere
$query = "INSERT INTO $table VALUES ('NULL','{$_POST['date']}','{$_POST['topic']}','{$_POST['entry']}')";

b) is the 'better' option rather than rely on register_globals being On.
valaridz
Forum Newbie
Posts: 4
Joined: Wed Feb 11, 2004 5:23 am

Post by valaridz »

May be it better:

1.
////////////////////////////////
//your_form.html
////////////////////////////////
<form method=post action=add_entry.php>
<input name=entry[field1]>
<input name=entry[field2]>
...
<input name=entry[fieldN]>
<input type=submit>
</form>

2.
////////////////////////////////////
//db.inc.php
////////////////////////////////////
define('DB_HOST', '<your_host>');
define('DB_USER', '<your_user_name>');
define('DB_PASSWORD', '<your password>');
define('DB_NAME', 'your_db_name>');

//Tables
define('TBLprefix', '<your_prifix>');
define('TBL_ENTRIES', TBLprefix.'_entries');

//db connect
//for example:

//Data Source Name: This is the universal connection string
$dsn = 'mysql://' . DB_USER . ':' . DB_PASSWORD . '@' . DB_HOST . '/' . DB_NAME;

//Connect
$DB = DB::connect($dsn, true);

3.
///////////////////////////////
//add_entry.php
///////////////////////////////

include 'functions.inc.php';

$entry = & POST['entry'];
addEntry($entry);

4.
///////////////////////////////
//functions.inc.php
///////////////////////////////

include 'db.inc.php';

addEntry($entry) {
...
$query = 'INSERT INTO ' . TBL_ENTRIES . ' VALUES (''NULL'', ''' . $entry['field1'] . ''', ''' . $entry['field2'] . ''', ''' . $entry['field3'] . ''')';
...
}

5.

good luck^)
seeker2921
Forum Contributor
Posts: 120
Joined: Sat Mar 22, 2003 7:10 pm
Location: Wiesbaden Germany
Contact:

Post by seeker2921 »

I'll try both options and see what works better for me.. Thanks for your help..
Post Reply