How to insert multiple rows and files into mysql using php array? -
how insert multiple rows , upload files mysql database using php array?
i want add 'add more' option in form same fields got duplicates down it, , can insert multiple rows file uploads mysql database.
here code :
<?php // @author - chetanaya aggarwal ?> <?php include('header_dashboard.php'); ?> <?php include('session.php'); ?> <?php $get_id = $_get['id']; ?> <body id="home"> <?php include('navbar_client.php'); ?> <div class="container-fluid"> <div class="row-fluid"> <?php include('device_sidebar.php'); ?> <div class="span9" id="content"> <div class="row-fluid"> <!-- block --> <div class="block"> <div class="navbar navbar-inner block-header"> <div class="muted pull-left">upload documents</div> <div class="muted pull-right"><a id="return" data-placement="left" title="click return" href="clients_list.php"><i class="icon-arrow-left icon-large"></i> back</a></div> <script type="text/javascript"> $(document).ready(function(){ $('#return').tooltip('show'); $('#return').tooltip('hide'); }); </script> </div> <div class="block-content collapse in"> <div class="alert alert-success"><i class="icon-info-sign"></i> please fill in required details</div> <form class="form-horizontal" method="post" enctype="multipart/form-data"> <table style width="100%"> <tr> <tr> <td> <div class="control-group"> <label class="control-label" style="font-size: 16px;" for="inputpassword"><b>document type</b></label> <div class="controls"> <select name="docstype_id" class="chzn-select" required/> <option></option> <?php $docs_type=mysql_query("select * docs_type")or die(mysql_error()); while ($row=mysql_fetch_array($docs_type)){ ?> <option value="<?php echo $row['docstype_id']; ?> name <?php echo $row['docsname']; ?>"><?php echo $row['docsname']; ?></option> <?php } ?> </select> </div> </div> </td> <td> <div class="control-group"> <label class="control-label" style="font-size: 16px;" for="inputpassword"><b>document copy</b></label> <div class="controls"> <input name="photo" class="input-file uniform_on" id="fileinput" type="file" required> </div> </div> </td> </tr> <tr> <td> <div class="control-group"> <label class="control-label" style="font-size: 16px;" id="la-add-mob" for="inputpassword"><b>document no.</b></label> <div class="controls"> <input type="text" class="span8" name="file_no" id="file_no" placeholder="document no."> </div> </div> </td> <td> </td> </tr> <tr> <td> <div class="control-group"> <div class="controls"> <button name="save" id="save" data-placement="right" title="click here save new data." class="btn btn-primary"><i class="icon-save"></i> save</button> </div> </div> </td> </tr> <script type="text/javascript"> $(document).ready(function(){ $('#save').tooltip('show'); $('#save').tooltip('hide'); }); </script> </tr> </table> </form> </div> </div> <?php $uploaddir1 = 'uploads/'; //image upload folder if (isset($_post['save'])){ $docstype_id = $_post['docstype_id']; $fileno = $_post['file_no']; function getextension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } $filename1 = $_files['photo']['name']; $extension = getextension($filename1); $extension = strtolower($extension); $tmpname1 = $_files['photo']['tmp_name']; $filesize1 = $_files['photo']['size']; $filetype1 = $_files['photo']['type']; $image_name= $fileno.'.'.$extension; $filepath1 = $uploaddir1 . $image_name; $result1 = move_uploaded_file($tmpname1, $filepath1); if (!$result1) { echo "error uploading file"; exit; } if(!get_magic_quotes_gpc()) { $filename1 = addslashes($filename1); $filepath1 = addslashes($filepath1); } mysql_query("insert upload (clients_id,docstype_id,file_no,file_path) values('$get_id','$docstype_id','$fileno','$filepath1')")or die(mysql_error()); ?> <script> window.location = "clients_list.php"; $.jgrowl("documents uploaded added", { header: 'device add' }); </script> <?php } ?> </div> </div> <!-- /block --> </div> </div> </div> <?php include('footer.php'); ?> </div> <?php include('script.php'); ?> </body>
i created sample upload can start, simple file upload.
<!-- in html --> <form method="post" enctype="multipart/form-data" accept-charset="utf-8" id="submitform"> <div class="upload"> <input type="file" id='id1' name="image1" accept=".doc, .docx, .jpg, .pdf" /> <input type="file" id='id2' name="image2" accept=".doc, .docx, .jpg, .pdf" /> <input type="submit" name="submit" value='upload' /> </div> </form> <?php // php handler if(isset($_post["submit"])){ $files = upload($_files); var_dump($files); } // create function like: function upload($data) { define('ds', directory_separator); define('root', dirname(__file__)); // initialize array $file = array(); // create folder name $folder_name = date('m-y', time()); // date today $date_today = date('d', time()); // build upload directory $upload_directory = root . ds . 'upload' . ds . $folder_name . ds . $date_today; // check if folder exist or not if(!file_exists(root . ds . 'upload' . ds . $folder_name)){ // create directory mkdir(root . ds . 'upload' . ds . $folder_name, 0777); // change permission chmod(root . ds . 'upload' . ds . $folder_name, 0777); } if(!file_exists($upload_directory)){ // create directory mkdir($upload_directory, 0777); // change permission chmod($upload_directory, 0777); } foreach($data $key => $value){ // check if file name empty if(!empty($value['name'])){ // file name $file_name = str_replace(" ", "_", strtoupper($value['name'])); // create real file $real_file = date('ymd_his',time()) .'_'.$key.'_'. $file_name; if(!move_uploaded_file($value['tmp_name'], $upload_directory . ds . $real_file)) { // change file setting chmod($upload_directory . ds . $real_file, 0774); } //move_uploaded_file $ext = explode(".", $value['name']); $file_ext = array_pop($ext); $file[$key] = array( // build array needed store in database 'file_path' => '/upload/'.$folder_name."/".$date_today."/".$real_file, 'file_name' => $value['name'], 'file_type' => $value['type'], 'file_size' => $value['size'], 'file_ext' => strtolower($file_ext), 'upload_date'=> date("y-m-d h:i:s"), ); // end array } //!empty } return $file; } ?>
note: put in test folder , create folder "upload" inside it. run php file. resulting $files array can use pass in mysql query.
Comments
Post a Comment