Page 1 of 1
How to put this in a function
Posted: Mon Feb 09, 2004 3:19 am
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?
?>
Posted: Mon Feb 09, 2004 5:26 am
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.
Posted: Wed Feb 11, 2004 5:58 am
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^)
Posted: Fri Feb 13, 2004 1:39 am
by seeker2921
I'll try both options and see what works better for me.. Thanks for your help..