Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.43 KB | None | 0 0
  1. <html>
  2.     <head>
  3.         <title>Update Star Trades Database</title>
  4.     </head>
  5.     <body>
  6.         <div style="width:100%; text-align:center;">
  7.         <?php
  8.         if(!isset($_POST['submit'])){ ?>
  9.                 <form action="<?php echo $_SERVER['PHP_SELF'] ; ?>" method="post" enctype="multipart/form-data">
  10.                     <label for="file">Filename:</label>
  11.                     <input type="file" name="file" id="file" />
  12.                     <input type="submit" name="submit" value="Submit" />
  13.                 </form>
  14.                    
  15.         <? } else {
  16.             echo "Processing File......";
  17.             $file = $_FILES["file"]["tmp_name"];
  18.             echo "$file";
  19.         }
  20.         ?>
  21.         <hr>
  22.         </div>
  23.     </body>
  24.    
  25. </html>
  26.  
  27. <?php
  28. /*
  29.  * Used to update the startrades database
  30.  *
  31.  *  First we do some very basic data sanitization,
  32.  *
  33.  * NDM 28/06/2011
  34.  *
  35.  */
  36.  
  37. /* Only report simple running errors */
  38. error_reporting(E_ERROR | E_WARNING | E_PARSE);
  39.  
  40.  
  41. if(isset($_POST['submit'])){
  42.     /* Connect to the SQL server */
  43.     $db = mysql_connect("localhost","root","4LE0ia7roa");
  44.     if (!$db){ die('Could not connect: ' . mysql_error()); }
  45.    
  46.     mysql_select_db("startrades", $db);
  47.    
  48.     /* Read the file */
  49.     $lines = file($file);
  50.     $count['count'] = 0;
  51.     $count['skipped'] = 0;
  52.     $count['success']['num'] = 0;
  53.    
  54.     /* foreach line in the file, assign it to $line, and keep a line number */
  55.     foreach ($lines as $line_num => $line) {
  56.         $line_num++; /* Incease the line num by 1, php is 0 based, excel is not. */
  57.         $line = explode(",",$line); /* Split the file by a ',' into an array in $line */
  58.        
  59.         /* If we don't have a company name, its a blank line, continue to next loop */
  60.         if ($line[2] == ""){ $count['empty']++ ; continue;}
  61.        
  62.         /* if we don't have a Publicaion, use the last one, else usee this line's one */
  63.         if (trim($line[0]) == ""){
  64.             $inserts[$line_num]['pub'] = $last_pub ;   
  65.         }else{
  66.             $inserts[$line_num]['pub'] = $line[0];
  67.             $last_pub = $line[0];
  68.         }
  69.        
  70.         /* if we don't have a classification, use the last one, else usee this line's one */
  71.         if ($line[1] == ""){
  72.             $inserts[$line_num]['class'] = $last_class ;   
  73.         }else{
  74.             $inserts[$line_num]['class'] = $line[1];
  75.             $last_class = $line[1];
  76.         }
  77.        
  78.         /* build an array to loop through of good data, this is for our insert later */
  79.         $inserts[$line_num]['name']         = trim(mysql_real_escape_string($line[2]));
  80.         $inserts[$line_num]['special_tag']  = trim(mysql_real_escape_string($line[3]));
  81.         $inserts[$line_num]['features']     = trim(mysql_real_escape_string($line[4]));
  82.         $inserts[$line_num]['contact']      = trim(mysql_real_escape_string($line[5]));
  83.         $inserts[$line_num]['land_line']    = trim(mysql_real_escape_string($line[6]));
  84.         $inserts[$line_num]['mobile']       = trim(mysql_real_escape_string($line[7]));
  85.         $inserts[$line_num]['website']      = trim(mysql_real_escape_string($line[8]));
  86.         $inserts[$line_num]['email']        = trim(mysql_real_escape_string($line[9]));
  87.         $inserts[$line_num]['license']      = trim(mysql_real_escape_string($line[10]));
  88.     }
  89.    
  90.    
  91.     /*
  92.      *  Loop through our insert array, and do the following.
  93.      *  
  94.      *  1) Insert into goods table -- Returning ID
  95.      *  2) Insert into Goods_cat table -- Including ID
  96.      *  3) Insert/Update sng_goods_log
  97.      *  4) Insert/Update sng_goods_properties
  98.      *  5) Insert/Update sng_goods_suburb
  99.      *  6) Insert/Update sng_good_suburb_range
  100.      *
  101.      */
  102.      
  103.      foreach ($inserts as $insert_num => $insert){
  104.         /* Do the complicated and retarded insert into jim's fucked db scheme */
  105.            
  106.         $insert_goods = " INSERT INTO sng_goods ( `user_id`,`modify_datetime`,`cat_id`,`goods_sn`,`goods_name`,`subdomain_name`,`source_paper_id`,`promote_start`,`promote_end`,`goods_brief`,`is_real`,`is_on_sale`,`is_confirmed`,`is_alone_sale`,`is_linked`,`can_handsel`,`add_time`,`is_promote`,`last_update`,`duration_years`,`goods_desc`,`seller_note`,`payment_name`) VALUES ('0',current_timestamp(),'0','','$insert[name]','','0',current_timestamp(),date_add(current_date,INTERVAL 1 YEAR),'$insert[special_tag] $insert[features]','1','1','1','1','1','1',UNIX_TIMESTAMP(current_date),'1',UNIX_TIMESTAMP(current_date),'1','','','');";
  107.        
  108.         /* If we cannot insert into the goods table, we cannot continue with this line, report error and continue with next */
  109.         if(!mysql_query($insert_goods)){
  110.             $errors[$insert_num]['sql_error'] = mysql_error();
  111.             $errors[$insert_num]['sql'] = $insert_goods;
  112.             $count['skipped']++;
  113.             continue;          
  114.         }
  115.            
  116.         /* Get the last insert id */   
  117.         $goods_id = mysql_insert_id();
  118.        
  119.         /* Get the category ID, and name, or go to next row */
  120.         $select_goods_cat = "SELECT * from sng_category WHERE cat_name='$insert[class]'";
  121.         $result_goods_cat = mysql_query($select_goods_cat);
  122.         $row_goods_cat    = mysql_fetch_row($result_goods_cat);
  123.        
  124.         if(!$row_goods_cat){
  125.             $errors[$insert_num]['message'] = "Could not find category '$insert[class]'";
  126.             $errors[$insert_num]['sql'] = $select_goods_cat;
  127.             $count['skipped']++;
  128.             continue;
  129.         }
  130.        
  131.         /* Insert into the goods_cat table */
  132.         $insert_goods_cat = "INSERT INTO sng_goods_cat ( `goods_id`,`cat_id`,`cat_name` ) VALUES ( '$goods_id', '" .mysql_real_escape_string($row_goods_cat[0]) ."', '" .mysql_real_escape_string($row_goods_cat[1]) ."');";
  133.      
  134.         /* If we cannot insert goods_cat, error and continue */
  135.         if(!mysql_query($insert_goods_cat)){
  136.             $errors[$insert_num]['sql_error'] = mysql_error();
  137.             $errors[$insert_num]['message'] = $row_goods_cat;
  138.             $errors[$insert_num]['sql'] = $insert_goods_cat;
  139.             $count['skipped']++;
  140.             continue;          
  141.         }
  142.        
  143.         $insert_goods_log = "INSERT INTO sng_goods_log (`log_time`, `goods_id`, `admin_name`, `log_info`, `ip_address`) VALUES (UNIX_TIMESTAMP(current_date), $goods_id, 'Batch Uploaded', 'Batch Uploaded', '127.0.0.1')" ;
  144.        
  145.         /* If we cannot insert goods_log, error and continue */
  146.         if(!mysql_query($insert_goods_log)){
  147.             $errors[$insert_num]['sql_error'] = mysql_error();
  148.             $errors[$insert_num]['sql'] = $insert_goods_log;
  149.             $count['skipped']++;
  150.             continue;          
  151.         }
  152.        
  153.         /* Explode the name into first_name and last_name */
  154.         $exploded_name  = explode(" ", $inserts[$line_num]['contact']);
  155.         $first_name     = $exploded_name[0];
  156.         $last_name      = isset($exploded_name[1]) ? $exploded_name[1] : "";
  157.        
  158.         $insert_goods_properties = "INSERT INTO sng_goods_properties (`goods_id`, `user_id`, `first_name`, `last_name`, `phone_number`, `mobile_number`, `goods_email`, `goods_website`, `fax_number`, `hear_about_us`, `yeas_in_business`, `particular_type`, `goods_awards`, `goods_brands`, `Pensionser_Discounts`, `Free_Quotes`, `other_information`) VALUES    ('$goods_id', '0', '$first_name', '$last_number', '$insert[land_line]', '$insert[mobile]', '$insert[email]', '$insert[website]', '', '', '', '', '', '', '0', '0', '')" ;
  159.        
  160.         /* If we cannot insert goods_propertirs, error and continue */
  161.         if(!mysql_query($insert_goods_properties)){
  162.             $errors[$insert_num]['sql_error'] = mysql_error();
  163.             $errors[$insert_num]['sql'] = $insert_goods_properties;
  164.             $count['skipped']++;
  165.             continue;          
  166.         }
  167.        
  168.        
  169.         /* Lookup the publication, select the post code, and do something with it */
  170.         $select_post_code = "SELECT * from sng_post_code_pub WHERE publication = '" .$insert['pub'] ."';";
  171.         $result_post_code = mysql_query($select_post_code);
  172.         $row_post_code    = mysql_fetch_array($result_post_code) ;
  173.         if(mysql_num_rows($result_post_code) > 0){
  174.                 $select_post_code1  = "SELECT * FROM sng_post_code WHERE post_code = '$row_post_code[post_code]';";
  175.                 $result_post_code1  = mysql_query($select_post_code1);
  176.                 $row_post_code1     = mysql_fetch_row($result_post_code1);
  177.                
  178.                 /* Make sure we got the post code, or continur next line */
  179.                 if(mysql_num_rows($result_post_code1) > 0){
  180.                     $insert_goods_suburb = "INSERT INTO sng_goods_suburb (`goods_id`, `suburb_id`, `suburb_name`, `post_code`) VALUES ('$goods_id', '$row_post_code1[0]', '" .mysql_real_escape_string($row_post_code1[4]) ."','$row_post_code1[1]');";
  181.                     if(!mysql_query($insert_goods_suburb)){
  182.                         $errors[$insert_num]['error'] = mysql_error();
  183.                         $errors[$insert_num]['sql'] = $insert_goods_suburb;
  184.                         $count['skipped']++;
  185.                         continue;          
  186.                     }
  187.                 }else{
  188.                         $errors[$insert_num]['error'] = "Could not lookup Post Code";
  189.                         $errors[$insert_num]['message'] = $select_post_code1;
  190.                         $errors[$insert_num]['sql'] = $select_post_code;
  191.                         $count['skipped']++;
  192.                         continue;          
  193.                 }  
  194.         }else{
  195.             $errors[$insert_num]['error'] = "Could not look up publication: $insert[pub]";
  196.             $errors[$insert_num]['sql'] = $select_post_code;
  197.             $count['skipped']++;
  198.             continue;
  199.         }
  200.        
  201.        
  202.         /* Increase the success count */
  203.         $count['success']['num']++ ;
  204.         $count['success']['rows'][$insert_num] = "Success" ;
  205.      }
  206.     /* Delete the file we uploaded, were done */
  207.     unlink($file);
  208.  
  209.     /* Close the mysql connection, were done */
  210.     mysql_close($db);
  211.  
  212. }
  213.  
  214. /* Do some reporting on what's happened */
  215. if($errors){
  216.     printf("The following %s lines imported OK: <br>", $count['success']['num']);
  217.      foreach($count['success']['rows'] as $line_number => $success_row){
  218.         echo "$line_number, ";
  219.      }
  220.      echo "<h2> Here are the errors:</h2>";
  221.      echo "<h3> We have have skipped $count[empty] empty rows, and have $count[skipped] errors.";
  222.      echo "<ul>";
  223.      foreach ($errors as $error_num => $error){
  224.         echo "<li>";
  225.             $string = "<b>" .$error_num .":</b>&nbsp" ;
  226.             $string .= isset($error['message']) ? $error['message'] ." ": "" ;
  227.             $string .= isset($error['error']) ? $error['error'] ." ": "" ;
  228.             //$string .= isset($error['sql']) ? $error['sql'] ." ": "" ;
  229.             echo  $string;
  230.         echo "</li>";
  231.      }
  232.      echo "</ul>";
  233. }
  234.  
  235. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement