无法在每个类别中保存数据 - PHP CSV

Unable to save data in each category - PHP CSV

我有一个 CSV 文件,我想从中插入数据到数据库中。

里面涉及到一些场景。

喜欢,如果类别存在于数据库中,脚本简单地在数据库中插入该类别的所有视频,而不将该类别插入类别中 table。

如果我有新类别,脚本会在类别 table 中简单地保存该类别一次,然后将他各自的所有视频保存在各自的视频中 table。

我的问题是我无法执行此操作,因为它在本应只保存一次的情况下继续保存类别数的时间。意思是在数据库中插入一个类别,并将他各自的视频保存在视频table.

同样,当类别存在时,它只是插入它的视频​​而不插入它的类别。

我正在处理的脚本是:

<?php 
ini_set('display_errors','On');
ini_set("memory_limit", -1);
set_time_limit(0);
include("includes/header.php");
include("conn.php");
?>
<form name="frm_coupon" id="frm_coupon" action="" method="post" enctype="multipart/form-data">
  <div class="element">
    <label for="name">Upload CSV File: <span class="red">(required)</span>Only File format files (.CSV)</label>
    <input  name="file" type="file" id="file"  class="text"/><br /><br />
    <input name="submitBtn" type="submit">
  </div>
</form>
<?php
if(isset($_POST['submitBtn']) && $_POST['submitBtn']!=""){
$today  = date("Y-m-d");
$file   = $_FILES["file"]["name"];
$source = $_FILES["file"]["tmp_name"];
$target = "../assets/styles/csvFile/".$file;
$asdss = array();
move_uploaded_file($source, $target);

$seletrec = mysqli_query("SELECT `catName` FROM `web_category`");

$ar = array();
while($rec = mysqli_fetch_array($seletrec))
{
    array_push($ar,$rec['catName']);
}

if (($handle = fopen($target, "r")) !== FALSE) 
{
    $i=0;
    while (($data = fgetcsv($handle, 10000, ",")) !== FALSE) 
    {
        $i++;
        if($i==1) continue;
        /*echo "<pre>";
        print_r($data);
        echo "</pre>"; die;*/
        //var_dump($data);
        if(!in_array($data[1],$ar))
        {
            //echo " IF ";
            $vCode   = explode("=", $data[4]);
            $vidCode = $vCode[1]; 

            $sdsdsd     = mysqli_query("SELECT `catID` FROM `web_category` WHERE `catName` = '".$data[1]."'");
            $cscssc = mysqli_fetch_array($sdsdsd);
            $videocid     = $cscssc['catID'];

            $sdf = mysqli_query("SELECT * FROM `web_videos` WHERE catID = '".$data[1]."'");
            $ds = mysqli_fetch_array($sdf);
            $videoCode    = $ds['catID'];

            if($videocid!=$videoCode){

            $query  = mysqli_query("INSERT INTO `web_category` SET `catName` = '".$data[1]."'");
            $getid  = mysqli_insert_id();

            $insert = mysqli_query("INSERT `web_videos` SET
             `vName` = '".mysqli_real_escape_string($data[1])."',
             `catID` = '".$videocid."',
             `videoName` = '".mysqli_real_escape_string($data[2])."',
             `vCreated` = '".$today."',
             `videoLink` = '".$data[4]."',
             `videoCode` = '".$vidCode."', `videoUpload` = ''");
            }else{
            //$query    = mysqli_query("INSERT INTO `web_category` SET `catName` = '".$data[1]."'");
            //$getid    = mysqli_insert_id();

            $insert = mysqli_query("INSERT `web_videos` SET
             `vName` = '".mysqli_real_escape_string($data[1])."',
             `catID` = '".$videocid."',
             `videoName` = '".mysqli_real_escape_string($data[2])."',
             `vCreated` = '".$today."',
             `videoLink` = '".$data[4]."',
             `videoCode` = '".$vidCode."', `videoUpload` = ''");    

                }
        }
        else if(in_array($data[1],$ar))
        {

            $vCode   = explode("=", $data[4]);
            $vidCode = $vCode[1]; 
            $vidsCode = $data[2]; 

            $seletrec = mysqli_query("SELECT * FROM `web_category` WHERE catName = '".$data[1]."'");
            $ids      = mysqli_fetch_array($seletrec);
            $id       = $ids['catID'];

            $sdf = mysqli_query("SELECT * FROM `web_videos` WHERE vName = '".mysqli_real_escape_string($data[2])."'");
            $ds = mysqli_fetch_array($sdf);
            $videoCode    = $ds['vName'];

            if($videoCode!=$vidsCode){
            $insert = mysqli_query("INSERT `web_videos` SET
             `vName` = '".mysqli_real_escape_string($data[2])."',
             `catID` = '".$id."',
             `videoName` = '".mysqli_real_escape_string($data[3])."',
             `vCreated` = '".$today."',
             `videoLink` = '".$data[4]."',
             `videoCode` = '".$vidCode."', `videoUpload` = ''");
                }
            }
        }
    }
 }
include("includes/footer.php");

这是 csv 文件的样子:

这是类别 table 的样子:

这是视频 table 的样子:

编辑 2015 年 4 月 6 日

这是 CSV 数据:

No.,Category,Life in Qatar 2012+B280:D288284,Iframe Code,Video Link
1,Emir Sheikh Cars,Emir Hamad Bin Khalifa Al Thani in National Day 2010,"<iframe width=""740"" height=""4555"" src=""//www.youtube.com/embed/Ewksi3sNNFo"" frameborder=""0"" allowfullscreen></iframe>",http://www.youtube.com/watch?v=Ewksi3sNNFo
2,Emir Sheikh Cars,Arab Sheikh's car garage (2007),"<iframe width=""740"" height=""455"" src=""//www.youtube.com/embed/c0fNKdgUcCk"" frameborder=""0"" allowfullscreen></iframe>",http://www.youtube.com/watch?v=c0fNKdgUcCk
3,Emir Sheikh Cars,G63 AMG Mercedes-Benz No. 1 of Sheikh Mohammed bin Rashid Al Maktoum's,"<iframe width=""740"" height=""455"" src=""//www.youtube.com/embed/2qfIoQqjNpQ"" frameborder=""0"" allowfullscreen></iframe>",http://www.youtube.com/watch?v=2qfIoQqjNpQ
4,Cars,the cars of Dubai princes,"<iframe width=""740"" height=""315"" src=""//www.youtube.com/embed/dyr4SSlq4J8"" frameborder=""0"" allowfullscreen></iframe>",http://www.youtube.com/watch?v=dyr4SSlq4J8
5,Cars,UAE Unveils World's Most Expensive Car: Gold and diamond Lamborghini goes on show in Dubai,"<iframe width=""740"" src=""//www.youtube.com/embed/ihUOvVyr1DQ"" frameborder=""0"" allowfullscreen></iframe>",http://www.youtube.com/watch?v=ihUOvVyr1DQ
6,Cars,"BMW X6 gold.  UAE president's son, a car made ??of gold","<iframe width=""740"" height=""315"" src=""//www.youtube.com/embed/NfH2X1_pZyw"" frameborder=""0"" allowfullscreen></iframe>",http://www.youtube.com/watch?v=NfH2X1_pZyw
7,Cars,the cars of Dubai princes,"<iframe width=""740"" height=""315"" src=""//www.youtube.com/embed/dyr4SSlq4J8"" frameborder=""0"" allowfullscreen></iframe>",http://www.youtube.com/watch?v=dyr4SSlq4J8
8,Cars,Sheikh's Pyramid of Cars,"<iframe width=""740"" height=""315"" src=""//www.youtube.com/embed/LuDmCOFrId0"" frameborder=""0"" allowfullscreen></iframe>",http://www.youtube.com/watch?v=LuDmCOFrId0
9,Cars,"BMW X6 gold. UAE president son, a car made ??of gold","<iframe width=""740"" height=""315"" src=""//www.youtube.com/embed/NfH2X1_pZyw"" frameborder=""0"" allowfullscreen></iframe>",http://www.youtube.com/watch?v=NfH2X1_pZyw
10,Cars,the cars of Dubai princes,"<iframe width=""740"" height=""315"" src=""//www.youtube.com/embed/dyr4SSlq4J8"" frameborder=""0"" allowfullscreen></iframe>",http://www.youtube.com/watch?v=dyr4SSlq4J8
11,Cars,Rubix Car of Sheikh Hamad Bin Hamdan AL NAHYAN,"<iframe width=""740"" height=""455"" src=""//www.youtube.com/embed/b59mWQ90_k4"" frameborder=""0"" allowfullscreen></iframe>",http://www.youtube.com/watch?v=b59mWQ90_k4
12,Cars,MARIFE rent a car-EMIR DE QATAR,"<iframe width=""740"" height=""455"" src=""//www.youtube.com/embed/PeayBYn1W6M"" frameborder=""0"" allowfullscreen></iframe>",http://www.youtube.com/watch?v=PeayBYn1W6M
13,Cars,black spider designed by sheikh hamad bin hamdan bin mohamed al nahyan,"<iframe width=""740"" height=""455"" src=""//www.youtube.com/embed/V-3ArmHGyd0"" frameborder=""0"" allowfullscreen></iframe>",http://www.youtube.com/watch?v=V-3ArmHGyd0
14,Cars,Sheikh Hamad Bin Hamdan Al Nahyan,"<iframe width=""740"" height=""455"" src=""//www.youtube.com/embed/3QFNIJmsCK8"" frameborder=""0"" allowfullscreen></iframe>",http://www.youtube.com/watch?v=3QFNIJmsCK8
15,Cars,Shiekh Hamad Bin Hamdan Al Nahyan LARGE SPIDER,"<iframe width=""740"" height=""455"" src=""//www.youtube.com/embed/_lB9tXTqBZw"" frameborder=""0"" allowfullscreen></iframe>",http://www.youtube.com/watch?v=_lB9tXTqBZw
16,Emir Palace,Billionaire Prince Al Waleed bin Talal PALACE - INSIDE LOOK,"<iframe width=""740"" height=""455"" src=""//www.youtube.com/embed/QIdYRI4ci4A"" frameborder=""0"" allowfullscreen></iframe>",http://www.youtube.com/watch?v=QIdYRI4ci4A
17,Emir Palace,Qatar Sheikh's Beautiful Palace,"<iframe width=""740"" height=""455"" src=""//www.youtube.com/embed/PWAfLLLoJuo"" frameborder=""0"" allowfullscreen></iframe>",http://www.youtube.com/watch?v=PWAfLLLoJuo

这是类别 table 的 table 结构:

-- ----------------------------
-- Table structure for web_category
-- ----------------------------
DROP TABLE IF EXISTS `web_category`;
CREATE TABLE `web_category` (
  `catID` int(8) NOT NULL AUTO_INCREMENT,
  `catName` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`catID`)
) ENGINE=MyISAM AUTO_INCREMENT=2125 DEFAULT CHARSET=latin1;

这里是视频 table 的结构 table:

-- ----------------------------
-- Table structure for web_videos
-- ----------------------------
DROP TABLE IF EXISTS `web_videos`;
CREATE TABLE `web_videos` (
  `vID` int(11) NOT NULL AUTO_INCREMENT,
  `catID` int(11) NOT NULL,
  `vName` varchar(100) NOT NULL,
  `videoName` varchar(255) NOT NULL,
  `videoUpload` varchar(255) NOT NULL,
  `videoLink` varchar(255) NOT NULL,
  `videoCode` varchar(50) NOT NULL,
  `vCreated` date NOT NULL,
  PRIMARY KEY (`vID`)
) ENGINE=InnoDB AUTO_INCREMENT=2580 DEFAULT CHARSET=latin1;

能否请您使用此代码,看看是否有帮助。已根据您的要求更新代码。

if(isset($_POST['submitBtn']) && $_POST['submitBtn']!=""){
    $today  = date("Y-m-d");
    $file   = $_FILES["file"]["name"];
    $source = $_FILES["file"]["tmp_name"];
    $target = "../assets/styles/csvFile/".$file;
    $asdss = array();
    move_uploaded_file($source, $target);
    if(($handle = fopen($target, "r")) !== FALSE){
      $i=0;
      while(($data = fgetcsv($handle, 10000, ",")) !== FALSE){
        $i++;
        if($i==1) continue;
        $sdsdsd     = mysqli_query("SELECT `catID` FROM `web_category` WHERE `catName` = '".$data[1]."'");
        $cscssc = mysqli_fetch_array($sdsdsd);
        $videocid     = $cscssc['catID'];
        if(empty($videocid)){
          $vCode   = explode("=", $data[4]);
          $vidCode = $vCode[1]; 

          $query   = mysqli_query("INSERT INTO `web_category` SET `catName` = '".$data[1]."'");

          $seletrec = mysqli_query("SELECT * FROM `web_category` WHERE catName = '".$data[1]."'");
          $ids      = mysqli_fetch_array($seletrec);
          $id       = $ids['catID'];

          $insert = mysqli_query("INSERT `web_videos` SET
         `vName` = '".mysqli_real_escape_string($data[2])."',
         `catID` = '".$id."',
         `videoName` = '".mysqli_real_escape_string($data[3])."',
         `vCreated` = '".$today."',
         `videoLink` = '".$data[4]."',
         `videoCode` = '".$vidCode."', `videoUpload` = ''");

        }else{

          $vCode   = explode("=", $data[4]);
          $vidCode = $vCode[1]; 
          $vidsCode = $data[2]; 

          $seletrec = mysqli_query("SELECT * FROM `web_category` WHERE catName = '".$data[1]."'");
          $ids      = mysqli_fetch_array($seletrec);
          $id       = $ids['catID'];

          $sdf = mysqli_query("SELECT * FROM `web_videos` WHERE vName = '".mysqli_real_escape_string($data[2])."'");
          $ds = mysqli_fetch_array($sdf);
          $videoCode    = $ds['vName'];

          if($videoCode!=$vidsCode){
            $insert = mysqli_query("INSERT `web_videos` SET
            `vName` = '".mysqli_real_escape_string($data[2])."',
            `catID` = '".$id."',
            `videoName` = '".mysqli_real_escape_string($data[3])."',
            `vCreated` = '".$today."',
            `videoLink` = '".$data[4]."',
            `videoCode` = '".$vidCode."', `videoUpload` = ''");
            }
         }
      }
   }
}