mysql - Php code to import excel file to database -
i want import excel data database using phpexcel. below code.
when upload excel file , submit form, can echo out table no data being stored in database, can please me this. wrong sql part or creating array of data. please check , me out. can't use csv file, otherwise have been easier.
error: have error in sql syntax; check manual corresponds mysql server version right syntax use near 'use, tag, image, link) values ('null', 'silk 18', 'silk 18', 'machine made', '1' @ line 1
<?php /************************ database connection start here ****************************/ # database connection $dbc = mysqli_connect('localhost', 'root', 'ron143', 'tcc') or die('error: '.mysqli_connect_error()); /************************ database connection end here ****************************/ /** phpexcel_iofactory */ include 'phpexcel/classes/phpexcel.php'; /** phpexcel_iofactory */ include 'phpexcel/classes/phpexcel/iofactory.php'; # upload file server $file = $_files["file"]["name"]; $target_dir = "../../temp/"; $target_file = $target_dir . basename($file); move_uploaded_file($_files["file"]["tmp_name"], $target_file); $xls_url = "$target_dir$file"; // file info uploaded. $inputfilename = $xls_url; $inputfiletype = $_post['file_type']; $sheetname = '0'; /** input worksheet number read. 0 first , 1 second worksheet */ try { /** create new reader of type defined in $inputfiletype **/ $objreader = phpexcel_iofactory::createreader($inputfiletype); /** advise reader want load cell data , not formating or formula set on **/ #$objreader->setreaddataonly(true); /** load $inputfilename phpexcel object **/ $objphpexcel = $objreader->load($inputfilename); } catch(exception $e) { die('error loading file "'.pathinfo($inputfilename,pathinfo_basename).'": '.$e->getmessage()); } $alldatainsheet = $objphpexcel->getsheet($sheetname)->toarray(null,true,true,true); $arraycount = count($alldatainsheet); // here total count of row in excel sheet echo $arraycount; for($i=2;$i<=$arraycount;$i++){ $product_name = trim($alldatainsheet[$i]["a"]); $product_code = trim($alldatainsheet[$i]["b"]); $category1 = trim($alldatainsheet[$i]["c"]); $category1_id = trim($alldatainsheet[$i]["d"]); $category2 = trim($alldatainsheet[$i]["e"]); $category2_id = trim($alldatainsheet[$i]["f"]); $stylename = trim($alldatainsheet[$i]["g"]); $grams = trim($alldatainsheet[$i]["h"]); $thickness = trim($alldatainsheet[$i]["i"]); $width = trim($alldatainsheet[$i]["j"]); $length = trim($alldatainsheet[$i]["k"]); $color_ground = trim($alldatainsheet[$i]["l"]); $color_border = trim($alldatainsheet[$i]["m"]); $material = trim($alldatainsheet[$i]["n"]); $backing = trim($alldatainsheet[$i]["o"]); $reed = trim($alldatainsheet[$i]["p"]); $weave = trim($alldatainsheet[$i]["q"]); $ply = trim($alldatainsheet[$i]["r"]); $pile = trim($alldatainsheet[$i]["s"]); $care = trim($alldatainsheet[$i]["t"]); $precaution = trim($alldatainsheet[$i]["u"]); $use = trim($alldatainsheet[$i]["v"]); $tag = trim($alldatainsheet[$i]["w"]); $image = trim($alldatainsheet[$i]["x"]); $link = trim($alldatainsheet[$i]["y"]); $query = "select product_code products product_code = '".$product_code."'"; $sql = mysqli_query($dbc, $query); $recresult = mysqli_fetch_assoc($sql); $existcode = $recresult["product_code"]; $q = "insert products (product_id, product_name, product_code, category1, category1_id, category2, category2_id, stylename, grams, thickness, width, length, color_ground, color_border, material, backing, reed, weave, ply, pile, care, precaution, use, tag, image, link) values ('null', '".$product_name."', '".$product_code."', '".$category1."', '".$category1_id."', '".$category2."', '".$category2_id."', '".$stylename."', '".$grams."', '".$thickness."', '".$width."', '".$length."', '".$color_ground."', '".$color_border."', '".$material."', '".$backing."', '".$reed."', '".$weave."', '".$ply."', '".$pile."', '".$care."', '".$precaution."', '".$use."', '".$tag."', '".$image."', '".$link."')"; if (mysqli_query($dbc, $q)) { echo "new record created successfully"; } else { echo "error: " . $q . "<br>" . mysqli_error($dbc); } } echo "<div style='font: bold 18px arial,verdana;padding: 45px 0 0 500px;'>".$msg."</div>"; ?>
you better save excell file csv file , parsing php without using other library.
Comments
Post a Comment