Our blog

 

MySQL Desc Table

If you are feeling lazy, or would like to build in some future proofness into your system, you can use the MySQL Desc query to get table column information and then use this information to create dynamic SQL insertion strings.

For example:

PHP:
  1. $cols_query = db_query("desc table");
  2. while($cq = mysql_fetch_assoc($cols_query)){
  3.     $cols[]=$cq;
  4. }
  5. foreach($cols as $c){
  6.     if(!empty($_POST[$c['Field']])){
  7.         $sets[] = $c['Field'] . " = '" . mysql_real_escape_string($_POST[$c['Field']]) . "'";
  8.     }
  9. }
  10. mysql_query("insert into table set " . implode(', ', $sets));

More Reading:

 

Leave a Reply