mysql - Unable to save data in each category - PHP CSV -


i have csv file want insert data in database.

there scenarios involved in it.

like, if category exists in database, script simple insert video of category in database without inserting category in category table.

and if new category comes in, script simple save category once in category table , save respective videos in respective videos table.

my problem unable because keep saving category number of time when should once. means inserting 1 category in database , save respective videos in videos table.

same when category exists, insert videos without inserting category.

the script working on is:

<?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` `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` `web_category` `catname` = '".$data[1]."'");             $cscssc = mysqli_fetch_array($sdsdsd);             $videocid     = $cscssc['catid'];              $sdf = mysqli_query("select * `web_videos` catid = '".$data[1]."'");             $ds = mysqli_fetch_array($sdf);             $videocode    = $ds['catid'];              if($videocid!=$videocode){              $query  = mysqli_query("insert `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 `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 * `web_category` catname = '".$data[1]."'");             $ids      = mysqli_fetch_array($seletrec);             $id       = $ids['catid'];              $sdf = mysqli_query("select * `web_videos` 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"); 

here csv file how like:

enter image description here

here how category table like:

enter image description here

here how video table like:

enter image description here

edit 04-06-2015

here csv data:

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 expensive car: gold , 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, 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, 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 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 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 

here table structure of category table:

-- ---------------------------- -- table structure 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; 

here table structure of video table:

-- ---------------------------- -- table structure 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; 

could please use code , see if helps. have updated code based on requirements.

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` `web_category` `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 `web_category` set `catname` = '".$data[1]."'");            $seletrec = mysqli_query("select * `web_category` 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 * `web_category` catname = '".$data[1]."'");           $ids      = mysqli_fetch_array($seletrec);           $id       = $ids['catid'];            $sdf = mysqli_query("select * `web_videos` 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` = ''");             }          }       }    } } 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -