简体   繁体   中英

PHPExcel not inserting into MySQL database

I'm using PHPExcel where I'm testing the ability to upload data from the file to a table in MySQL.

The data I extract will always start at cell C8 and I will need data in Columns C, D, E, F, and G until the rows are blank.

Right now, the code below will extract the data. I was able to echo all of my values, the problem seems to lie in the $insertTable query (or near it) because the data is not inserting.

ALSO - I AM NOT GETTING ANY ERRORS

The script runs through, the message appears that "Record has been added" , but when I look at my table in phpMyAdmin - I see nothing.

Can anyone spot the error(s)?? Also, if anyone wants to give me pointers on converting this to PDO it would be greatly appreciated!!

<?php
  /************************ YOUR DATABASE CONNECTION START HERE   ****************************/

  define ("DB_HOST", "localhost"); // set database host
  define ("DB_USER", "root"); // set database user
  define ("DB_PASS",""); // set database password
  define ("DB_NAME","setpoints"); // set database name
  define ("DSN", "mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset=utf8");


  $link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("Couldn't make connection.");
  $db = mysql_select_db(DB_NAME, $link) or die("Couldn't select database");

  $databasetable = "job21433";

  /************************ YOUR DATABASE CONNECTION END HERE  ****************************/

  $jobnum = "21433189-01";

  set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');
  include 'PHPExcel/IOFactory.php';

  // This is the file path to be uploaded.
  $inputFileName = '21433189_LadnerLiesure_pointsreport.xlsx'; 

  try {
    $objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
  } catch(Exception $e) {
    die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
  }


  $allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
  $arrayCount = count($allDataInSheet);  // Here get total count of row in that Excel sheet


  for($i=8;$i<=$arrayCount;$i++){
      $block_name = trim($allDataInSheet[$i]["C"]); 
      $typ = trim($allDataInSheet[$i]["D"]);
      $obj_id = trim($allDataInSheet[$i]["E"]);
      $device_id = trim($allDataInSheet[$i]["F"]);
      $obj_name = trim($allDataInSheet[$i]["G"]);


      $query = "SELECT BLOCK_NAME FROM `job21433` WHERE BLOCK_NAME = '".$block_name."' and TYP = '".$typ."' and OBJ_ID = '".$obj_id."' and DEVICE_ID = '".$device_id."' and OBJ_NAME = '".$obj_name."'";
      $sql = mysql_query($query);
      $recResult = mysql_fetch_array($sql);
      $existName = $recResult["BLOCK_NAME"];

      if($existName=="") {
          $insertTable= mysql_query("insert into `job21433` (JOBNUM, BLOCK_NAME, TYP, OBJ_ID, DEVICE_ID, OBJ_NAME) values('".$jobnum."', '".$block_name."', ".$typ."', ".$obj_id."', ".$device_id."', ".$obj_name."');");
          $msg = 'object name = '.$obj_name.'Record has been added. <div style="Padding:20px 0 0 0;"><a href="">Go Back to tutorial</a></div>';
      } else {
          $msg = 'Record already exist. <div style="Padding:20px 0 0 0;"><a href="">Go Back to tutorial</a></div>';
       }
   }
   echo "<div style='font: bold 18px arial,verdana;padding: 45px 0 0 500px;'>".$msg."</div>";

 ?>

Here's how the database is setup:

在此处输入图片说明

,“。$ typ。”,“。$ obj_id。”,“。$ device_id。”,“。$ obj_name。”。<-缺少引号吗?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM