Page 3 of 3

Posted: Sat Feb 14, 2004 1:33 am
by Michael 01
Thats only because of the global statement. Globals are really becoming a pain in the ass all of sudden for coding functions or any other situation needed such as what you coded up.. :(

Posted: Sat Feb 14, 2004 1:36 am
by seeker2921
Even without the globals it doesn't work.. I can't figuare it out.. I'm to new to php.. I'm about ready to give up..

Posted: Sat Feb 14, 2004 2:48 am
by tim
perhaps:

Code: Select all

<?php
function addentry($data, $table) {
unset ($data["field1 in table"]);
unset ($data["field2 in table"]);
// do this for every field you wish to put data in and by doing it this way, the names of your fields must be the same for the names in your forms or this wont work, if it works at all
$query    = "INSERT INTO ";
$column = "column title (";
$vars    = "VALUES (";
   
foreach ( $data as $key => $value ) {
$column .= "`$key`, ";
$vars    .= "'$value', ";
   }
$column = rtrim($column, ", ");
$vars = rtrim($vars, ", ");
$query = $query . $column . ") " . $vars . ") ";
return $query;
} 
?>
The var $data is your $_POST variables made into a array for transfering the values into the DB, let me know if it works.

Posted: Sat Feb 14, 2004 2:53 am
by tim
oh and if that code dont work, add some or die(mysql_error());s to it n see whats baking with it

Posted: Sat Feb 14, 2004 2:59 am
by seeker2921
Didn't work.. Just put me back to the link.. Didn't give errors and it didn't update to the database.. I don't understand what I'm doing wrong.... In my form is there somehting special I need to do to have it choose that function??

Posted: Sat Feb 14, 2004 3:16 am
by tim
lets try this then to just test some things:

Code: Select all

<form name='form1' method='post' action='$strPAGE'>
Change to:

Code: Select all

<form action=add.php name=info method=POST>
make a add.php and paste this code:

Code: Select all

<?php
if(isset($info)) {
function addentry($data, $table) { 
unset ($data["field1 in table"]); 
unset ($data["field2 in table"]); 
$query    = "INSERT INTO "; 
$column = "column title ("; 
$vars    = "VALUES ("; 
    
foreach ( $data as $key => $value ) { 
$column .= "`$key`, "; 
$vars    .= "'$value', "; 
   } 
$column = rtrim($column, ", "); 
$vars = rtrim($vars, ", "); 
$query = $query . $column . ") " . $vars . ") "; 
return $query; 
  } 
}
?>
Let me know how this works

Posted: Sat Feb 14, 2004 3:22 am
by tim
and like I said
the unset ($data["field1 in table"]);
by field1 in table is the name of the field in your SQL table, and that name must be the same for the name your are assigning the variable in the <form> tags or this wont work peroid.
so <input name='date' type='text' value='$date' maxlength='20'>
would look like
unset ($data["date"]);

date being the title of the field in the table as well as the title assigned variable.

Posted: Sat Feb 14, 2004 3:38 am
by seeker2921
Nope