|
| 1 | +<?php |
| 2 | +$mysql_db_hostname = "localhost"; |
| 3 | +$mysql_db_user = "uploaduser"; |
| 4 | +$mysql_db_password = "your_password"; |
| 5 | +$mysql_db_database = "testupload"; |
| 6 | + |
| 7 | +$dbc = mysqli_connect('' . $mysql_db_hostname . '', '' . $mysql_db_user . '', '' . $mysql_db_password . '', '' . $mysql_db_database . '') OR die('Could not connect because: '.mysqli_connect_error()); |
| 8 | + |
| 9 | + |
| 10 | +if (isset($_POST['add_account'])) { |
| 11 | + |
| 12 | + |
| 13 | + if($_POST['fields']) { |
| 14 | + foreach($_POST['fields'] as $key=>$fieldArray ) { |
| 15 | + |
| 16 | + $keys = array_keys($fieldArray); |
| 17 | + |
| 18 | + if (!empty($_FILES)) { |
| 19 | + |
| 20 | + if($_FILES['fields']['name'][$key]['file_uploaded'][0] != ''){ |
| 21 | + // Get e-mail used for registration |
| 22 | + if($_POST['fields'][$key]['email'] !=''){ |
| 23 | + |
| 24 | + //Set the upload directory |
| 25 | + $uploaddir = 'uploads/'; |
| 26 | + //Get time to use in file name |
| 27 | + $newname = time(); |
| 28 | + //Generate random number to add in file name |
| 29 | + $rand = rand(100,999); |
| 30 | + //Construct the name using the above values + original file name |
| 31 | + $name = $newname.'-'.$rand.'-'.$_FILES['fields']['name'][$key]['file_uploaded'][0]; |
| 32 | + //Get the temporary file name |
| 33 | + $tempFile = $_FILES['fields']['tmp_name'][$key]['file_uploaded'][0]; |
| 34 | + //Set the path and file name as it will be saved in the db |
| 35 | + $uploadfile = $uploaddir.$name; |
| 36 | + |
| 37 | + //If the file was NOT moved from /tmp/ to our upload directory |
| 38 | + if (move_uploaded_file($tempFile, $uploadfile)) { |
| 39 | + |
| 40 | + //Get the email value in $_POST |
| 41 | + $email = $_POST['fields'][$key]['email']; |
| 42 | + $first = $_POST['fields'][$key]['first']; |
| 43 | + $last = $_POST['fields'][$key]['last']; |
| 44 | + |
| 45 | + //Construct the query to insert the data |
| 46 | + $q = "INSERT INTO accounts (first, last, email, uploaded_file) VALUES ('".$first."','".$last."','".$email."', '".$uploadfile."')"; |
| 47 | + $r = mysqli_query($dbc, $q); |
| 48 | + |
| 49 | + //If the query is successfull |
| 50 | + if($r){ |
| 51 | + |
| 52 | + echo 'Name: '.$first.' '.$last.' <br />Email:'. $email.' <br /><img src="'. $uploadfile.'" style="max-width:120px; height: auto;"><br /><div style="color: green;"><strong>Success</strong></div>'; |
| 53 | + |
| 54 | + //Else if the query is not successfull, check if there is already a record with same data |
| 55 | + |
| 56 | + } else { |
| 57 | + |
| 58 | + echo '<div class="alert alert-danger">The request failed! Please try again later or open a ticket'; |
| 59 | + |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + } else { //If the file was not attached to the request -- check can be skipped, as the field is required anyway |
| 64 | + |
| 65 | + echo '<br /> |
| 66 | + <div class="alert alert-danger" role="alert"> |
| 67 | + The data could not be saved to DB. |
| 68 | + </div>'; |
| 69 | + } |
| 70 | + } // end if $_FILES |
| 71 | + } // end for each loop |
| 72 | + } |
| 73 | + |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + echo '<hr /><div style="width: 100%;"><i><h2><strong>' . count($_POST['fields']) . '</strong> Account(s) Added</h2></i> '; |
| 78 | + echo '<p><a href="javascript:history.back();" class="btn btn-default">Go Back</a></p></div>'; |
| 79 | + |
| 80 | + |
| 81 | +} |
| 82 | + |
| 83 | +if (!isset($_POST['add_account'])) { |
| 84 | + |
| 85 | +// The form ?> |
| 86 | +<form method="post" action="" enctype="multipart/form-data"> |
| 87 | +<?php // adding a button to add new rows ?> |
| 88 | +<p id="add_field"><a class="btn btn-default" href="#">Add Rows</a></p> |
| 89 | + |
| 90 | +<?php //building our form as a table. Also, adding a 1st line in the form. ?> |
| 91 | +<table id="myTable"> |
| 92 | +<thead> |
| 93 | + <tr> |
| 94 | + <th>#</th> |
| 95 | + <th>First Name:</th> |
| 96 | + <th>Last Name:</th> |
| 97 | + <th>E-mail:</th> |
| 98 | + <th>Upload file</th> |
| 99 | + <th></th> |
| 100 | + </tr> |
| 101 | +</thead> |
| 102 | +<tbody id="container"> |
| 103 | + <tr> |
| 104 | + <td>1</td> |
| 105 | + <td><div class="form-group"><input class="form-control" name="fields[1][first]" type="text" placeholder="First" required/></div></td> |
| 106 | + <td><div class="form-group"><input class="form-control" name="fields[1][last]" type="text" placeholder="Last" required/></div> </td> |
| 107 | + <td><div class="form-group"><input class="form-control" name="fields[1][email]" type="email" placeholder="email" required/></div></td> |
| 108 | + <td><input class="btn btn-primary" id="userfiles" name="fields[1][file_uploaded][]" type="file" required = "required"/> </td> |
| 109 | + <td><input class="btn btn-danger" type="button" value="Remove" onclick="delRow(this)"> </td> |
| 110 | + </tr> |
| 111 | +</tbody> |
| 112 | +</table> |
| 113 | + |
| 114 | +<input class="btn btn-success" type="submit" name="add_account" value="Submit Form" /> |
| 115 | +</form> |
| 116 | +<?php } ?> |
| 117 | + |
| 118 | +<?php //jQuery (necessary for Bootstrap's JavaScript plugins) ?> |
| 119 | +<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> |
| 120 | + |
| 121 | + |
| 122 | +<script type="text/javascript"> |
| 123 | + |
| 124 | +// function to build our form rows every time we click on the "Add row" button |
| 125 | +$(function(){ |
| 126 | + var counter = 1; |
| 127 | + $('p#add_field').click(function(){ |
| 128 | + counter += 1; |
| 129 | + $('#container').append( |
| 130 | + '<tr> \ |
| 131 | + <td>' + counter + '</td> \ |
| 132 | + <td><div class="form-group"><input class="form-control" name="fields['+counter+'][first]" type="text" placeholder="First" required/></div></td> \ |
| 133 | + <td><div class="form-group"><input class="form-control" name="fields['+counter+'][last]" type="text" placeholder="Last" required/></div> </td> \ |
| 134 | + <td><div class="form-group"><input class="form-control" name="fields['+counter+'][email]" type="email" placeholder="email" required/></div></td> \ |
| 135 | + <td><input class="btn btn-primary" id="userfiles" name="fields['+counter+'][file_uploaded][]" type="file" required = "required"/> </td> \ |
| 136 | + <td><input class="btn btn-danger" type="button" value="Remove" onclick="delRow(this)"> </td> \ |
| 137 | + </tr>'); |
| 138 | + |
| 139 | + }); |
| 140 | +}); |
| 141 | + |
| 142 | +// function to remove selected row |
| 143 | +function delRow(currElement) { |
| 144 | + var parentRowIndex = currElement.parentNode.parentNode.rowIndex; |
| 145 | + document.getElementById("myTable").deleteRow(parentRowIndex); |
| 146 | +} |
| 147 | +</script> |
0 commit comments