Can I see an example of something..

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

User avatar
Michael 01
Forum Commoner
Posts: 87
Joined: Wed Feb 04, 2004 12:26 am

Post 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.. :(
seeker2921
Forum Contributor
Posts: 120
Joined: Sat Mar 22, 2003 7:10 pm
Location: Wiesbaden Germany
Contact:

Post 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..
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

oh and if that code dont work, add some or die(mysql_error());s to it n see whats baking with it
seeker2921
Forum Contributor
Posts: 120
Joined: Sat Mar 22, 2003 7:10 pm
Location: Wiesbaden Germany
Contact:

Post 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??
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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.
seeker2921
Forum Contributor
Posts: 120
Joined: Sat Mar 22, 2003 7:10 pm
Location: Wiesbaden Germany
Contact:

Post by seeker2921 »

Nope
Post Reply