joachip

Input fields into MySQL with PHP

Sep 21st, 2013
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.79 KB | None | 0 0
  1. <?
  2.  
  3. if (count($_POST)>0)
  4. {
  5.     $incoming = $_POST;
  6.  
  7.     $fields_with_content = 0;
  8.     $empty_fields = 0;
  9.     $sql = 'INSERT INTO entries SET ';
  10.     $sep = '';
  11.     foreach($incoming as $key=>$value)
  12.     {
  13.         echo "The input field <b>$key</b> contains the value '$value'<br>\n";
  14.         if (strlen(trim($value))>0)
  15.         {
  16.             $fields_with_content++;
  17.             $value = trim(str_replace("\r", "", $value));       // remove Windows specific CR characters
  18.             $sql .= $sep.'`'.preg_replace('%[^a-zA-Z0-9_ ]%', '', $key)."`='".mysql_real_escape_string($value)."'";
  19.             $sep = ', ';
  20.         } else
  21.         {
  22.             $empty_fields++;
  23.         }
  24.     }
  25.  
  26.     echo "<br><hr><br>\nFields with contents: $fields_with_content<br>\n";
  27.     echo "Empty fields: $empty_fields<br>\n";
  28.     echo htmlspecialchars($sql)."<br>\n";
  29.  
  30.     exit;
  31. }
  32.  
  33. ?>
  34. <!DOCTYPE HTML>
  35. <html>
  36. <head>
  37.  <title>scrchpst input shizzles</title>
  38.  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  39.  <style type="text/css">
  40. body        { max-width: 450px; font-family: constantia, cambria, times; font-size: 12px; background: #E0E0E0; color: #000000; margin: 50px; }
  41. h1          { font-family: calibri, tahoma, verdana; color: #003080; font-size: 25px; margin: 17px 0px 9px 0px; border: #B0B0B0 solid; border-width: 0px 0px 1px 0px; }
  42. h2          { font-family: calibri, tahoma, verdana; color: #003080; font-size: 19px; margin: 15px 0px 5px 0px; }
  43. h3          { font-family: calibri, tahoma, verdana; color: #003080; font-size: 15px; margin: 13px 0px 5px 0px; }
  44. textarea    { width: 313px; height: 150px; }
  45.  </style>
  46. </head>
  47. <body>
  48.  
  49. <form action="arrayinput.php" method="post">
  50. <h1>Type in stuff</h1>
  51. <input type="text" name="first_name" placeholder="First name">
  52. <input type="text" name="last_name" placeholder="Last name"><br>
  53. <textarea name="description"></textarea><br>
  54. <input type="submit" value="OK">
  55. </form>
  56.  
  57. </body>
  58. </html>
Advertisement
Add Comment
Please, Sign In to add comment