Re: One database, different forms

From: Date: Mon, 14 May 2001 16:22:50 +0000
Subject: Re: One database, different forms
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-52700@lists.php.net to get a copy of this message
Oh, here is the update code, which uses the same concepts: $column = mysql_list_fields("$db","$table"); for($i = 1; $i < mysql_num_fields($column); $i++) { $value = mysql_field_name($column,$i); $name = $value; $value = $$value; $values .= "$name='$value'"; if($i != mysql_num_fields($column)-1) $values .= ","; } $sql = "UPDATE $table SET $values WHERE id='$id'"; mysql_query($sql); And the INSERT code again for good measure: $column = mysql_list_fields("$db","$table"); for($i = 0; $i < mysql_num_fields($column); $i++) { $value = mysql_field_name($column,$i); $value = $$value; $values .= "'$value'"; if($i != mysql_num_fields($column)-1) $values .= ","; } $sql = "INSERT INTO $table VALUES ($values)"; mysql_query($sql); This code relies on 2 variables being correctly set: $db is the name of the database you are working with, and $table is the name of the table. Other than that what this script basically does is goes through every column in the specified table and dynamically creates a query from the names of the columns. Your variable names will need to match the column names exactly or this will insert/update null values. ----- Original Message ----- From: "midget2000x" <midget@elaris.com> To: <php-general@lists.php.net> Sent: Sunday, May 13, 2001 1:31 PM Subject: [PHP] One database, different forms > I am writing a PHP application that will operate on a MySQL database. There > will be 4 types of forms (like catalog request, info request, etc.) calling > the code which writes the data to the database. > > Since the variable names on the forms will equal the MySQL column names (as they > should), I am wondering if there is a way for me to create my SQL statements on > the fly so that only data passed is written. Otherwise I'll have to create 8 > types of SQL statements...INSERT (if the record, keyed by e-mail address, > doesn't exist), and UPDATE (if the record does exist) for all 4 forms. > > For example, on my "add to mailing list" form, only the e-mail address is > collected. I'd like the PHP to recognize that only the 'email' field is passed > and create the SQL statement that only writes the e-mail to the database. > > Any ideas greatly appreciated! > > Thanks, > > Rory > > -- ----------- > providing the finest in midget technology > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net >

« previous php.general (#52700) next »