Posted By

kidmizere on 03/27/11


Tagged

form mysql php variables


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

mikael12


CREATE INSERT FOR ALL FORM VARIABLES (POST /GET) INTO MYSQL USING PHP


 / Published in: PHP
 

This generates a simple insert statement for mysql based on all the variables in $GET or $POST using a simple page. It does not do the inserts although that is a simple addition - just builds and displays your insert statement.

  1. <?php
  2.  
  3. $fields = '';
  4. $values = '';
  5. $sql = display_post_get();
  6.  
  7. echo "<br>strSQL:<br>" . $sql;
  8.  
  9.  
  10. function display_post_get() {
  11. if ($_POST) {
  12. while (list($result_nme, $result_val) = each($_POST)) {
  13. $fields .= $result_nme . "," ;
  14. $values .= $result_val . "','" ;
  15. }
  16. $fields=substr($fields,0,strlen($fields)-5);
  17. $values=substr($values,0,strlen($values)-12);
  18. echo "Field Count : " . count(explode(",", $fields)) . "<p />";
  19. echo "Fields: " . $fields;
  20. echo "<p />Value Count : " . count(explode(",", $values)) . "<p />";
  21. echo "Values: " . $values . "<p />";
  22. $strSQL = "Insert into YOURDB ($fields) VALUES ('$values)";
  23. return $strSQL;
  24. }
  25. if ($_GET) {
  26. while (list($result_nme, $result_val) = each($_GET)) {
  27. $fields .= $result_nme . "," ;
  28. $values .= $result_val . "','" ;
  29. }
  30. $fields=substr($fields,0,strlen($fields)-5);
  31. $values=substr($values,0,strlen($values)-12);
  32. echo "Field Count : " . count(explode(",", $fields)) . "<p />";
  33. echo "Fields: " . $fields;
  34. echo "<p />Value Count : " . count(explode(",", $values)) . "<p />";
  35. echo "Values: " . $values . "<p />";
  36. $strSQL = "Insert into YOURDB ($fields) VALUES ('$values)";
  37. return $strSQL;
  38. }
  39. }
  40.  
  41. ?>

Report this snippet  

You need to login to post a comment.