This isn't the code you were talking about but it's something I wrote and
use all the time. update_table() requires a "where" variable which is an
array which needs to be declared before the function is called. The $where
array is only 2 parts with the column name as [0] and the condition as [1].
It would be declared something like: $where=array("name","George");  They
both require $db as the database pointer and $db_name as the database name.
insert_table() has the option of using a second database ($db_name2, $db2)
if desired. I haven't needed this feature in update_table() yet so I didn't
add it in. It wouldn't be hard to take the code from one to the other if
needed.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function update_table($table,$where){
 GLOBAL $db,$db_name,$HTTP_POST_VARS;
 $fields = mysql_list_fields($db_name, $table, $db);
 $columns = mysql_num_fields($fields);
 for ($i = 0; $i < $columns; $i++) {
  $colnames[$i]= mysql_field_name($fields, $i);
 }
 foreach($HTTP_POST_VARS as $key=>$value){
  if(in_array($key,$colnames) AND $value!="~NULL~"){
   $value=addslashes($value);
   $save.="$c $key='$value'";
   $c=",";
  }
 }
 $update="UPDATE $table SET $save WHERE $where[0]='$where[1]'";
  mysql_query($update,$db);
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function insert_table($table,$db_name2="",$db2=""){
 GLOBAL $db,$db_name,$HTTP_POST_VARS;
  $db1=$db;
  if($db2!="")$db1=$db2;
  $db_name1=$db_name;
  if($db_name2!="")$db_name1=$db_name2;
 $fields = mysql_list_fields($db_name1, $table, $db1);
 $columns = mysql_num_fields($fields);
 for ($i = 0; $i < $columns; $i++) {
  $colnames[$i]= mysql_field_name($fields, $i);
 }
 $cols="(";
 $save="(";
 foreach($HTTP_POST_VARS as $key=>$value){
  if(in_array($key,$colnames) AND $value!="~NULL~"){
   if($value==" ")$value="";
   $value=addslashes($value);
   $cols.="$comma$key";
   $save.="$comma'$value'";
   $comma=",";
  }
 }
 $cols.=")";
 $save.=")";
 mysql_query("INSERT INTO $table $cols VALUES $save",$db1);
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"Rory O'Connor" <[EMAIL PROTECTED]> wrote in message
20011109191732.B783@jacktasty">news:20011109191732.B783@jacktasty...
> A while back somebody answered a question about some PHP code that would
> take the $HTTP_POST_VARS and create the SQL that would write them to a
> MySQL table  (provided the posted var names matches the MySQL
> fieldnames).
>
> Does anyone have info on that project?  The PHP searchable archive is
> down, otherwise I wouldn't be asking on the list.
>
> Thanks,
> Rory



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to