Advertisement
Guest User

Untitled

a guest
Feb 18th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. <?php
  2. $mysql_db_hostname = "localhost";
  3. $mysql_db_user = "root";
  4. $mysql_db_password = "password";
  5. $mysql_db_database = "dynamic";
  6.  
  7. $dbc = mysql_connect($mysql_db_hostname, $mysql_db_user,
  8.  
  9. $mysql_db_password) or die("Could not connect database");
  10. mysql_select_db($mysql_db_database, $dbc) or die("Could not select database");
  11. ?>
  12.  
  13. <?
  14. if (isset($_POST['add_account'])) {
  15. if ($_POST['fields']) {
  16. foreach( $_POST['fields'] as $key=>$fieldArray ) {
  17.  
  18. $keys = array_keys($fieldArray);
  19. $values = array_map("mysql_real_escape_string",$fieldArray);
  20. $q = "INSERT INTO accounts (".implode(',',$keys).") VALUES ('".implode('','',$values)."')";
  21. $r = mysql_query($q, $dbc );
  22.  
  23. }
  24. }
  25. echo "<i><h2><strong>" . count($_POST['fields']) . "</strong> Account(s) Added</h2></i>";
  26. }
  27. ?>
  28.  
  29. <?php if (!isset($_POST['add_account'])) { ?>
  30.  
  31. <form method="post" action="" enctype="multipart/form-data">
  32.  
  33. <p id="add_field"><a class="btn btn-default" href="#">Add Rows</a></p>
  34. <table id="myTable">
  35. <thead>
  36. <tr>
  37. <th>#</th>
  38. <th>First Name:</th>
  39. <th>Last Name:</th>
  40. <th>E-mail:</th>
  41. <th>Upload file</th>
  42. <th></th>
  43. </tr>
  44. </thead>
  45. <tbody id="container">
  46. </tbody>
  47. </table>
  48.  
  49. <input class="btn btn-default" type="submit" name="add_account" value="Submit" />
  50. </form>
  51. <?php } ?>
  52.  
  53. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  54. <script type="text/javascript">
  55.  
  56. $(function(){
  57. var counter = 0;
  58. $('p#add_field').click(function(){
  59. counter += 1;
  60. $('#container').append(
  61. '<tr><td>' + counter + '</td><td><input id="field_' + counter + '" name="fields['+counter+'][first]" type="text" placeholder="First Name" required/></td><td><input id="field_' + counter + '" name="fields['+counter+'][last]" type="text" placeholder="Last Name" required/></td><td><input id="field_' + counter + '" name="fields['+counter+'][email]" type="email" placeholder="email" required/></td><td><input id="field_' + counter + '" name="fields['+counter+'][file_uploaded]" type="file" /></td><td><input button" value="Remove" onclick="delRow(this)"></td></tr>');
  62.  
  63. });
  64. });
  65.  
  66. function delRow(currElement) {
  67. var parentRowIndex = currElement.parentNode.parentNode.rowIndex;
  68. document.getElementById("myTable").deleteRow(parentRowIndex);
  69. }
  70.  
  71. </script>
  72.  
  73. id | first | last | email | file_uploaded
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement