Cant upload csv to mysql db

Codes here !

Moderators: egami, macek, gesf

Post Reply
rink1151
New php-forum User
New php-forum User
Posts: 1
Joined: Fri Jan 15, 2021 6:36 am

Fri Jan 15, 2021 6:42 am

I am getting error cant upload csv file to db please advise how I can resolve this.

Index Page :

<?php

/**
* @author
* @copyright 2021
*/
$conn = mysqli_connect("localhost","test","","testdb");



if(isset($_POST["import"]))
{
$filename=$_FILES["file"]["tmp_name"];
if($_FILES["file"]["size"]>0)
{
$file = fopen($filename,"r");
while (($getData = fgetcsv($file,100000,",")) !==FALSE)
{

$sql = "INSERT INTO employee (ID,E_Name,E_Last,E_Email)
VALUES ('".$getData[0]."', '".$getData[1]."', '".$getData[2]."')";
$result = mysqli_query($conn,$sql);

if(!empty($result))
{
echo "CSV data Imported";
}
else
{
echo "file not uploded";
}
}
}
}


?>

<form class="form-horizantal" action="" method="post" name="indexphp" enctype="multipart/form-data" >
<div>
<label> Csv File </label>
<input type="file" name="file" accept=".csv">
<button type="submit" name="import">Import</button>
</div>
</form>


_____________________________________________________
DB
-__________________________________________

-- phpMyAdmin SQL Dump
-- version 5.0.3
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Jan 15, 2021 at 09:14 AM
-- Server version: 10.4.14-MariaDB
-- PHP Version: 7.2.34

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `testdb`
--

-- --------------------------------------------------------

--
-- Table structure for table `employee`
--

CREATE TABLE `employee` (
`ID` int(11) NOT NULL,
`E_Name` varchar(255) NOT NULL,
`E_Last` varchar(255) NOT NULL,
`E_Email` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `employee`
--
ALTER TABLE `employee`
ADD PRIMARY KEY (`ID`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `employee`
--
ALTER TABLE `employee`
MODIFY `ID` int(11) NOT NULL AUTO_INCREMENT;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
AdoptiveSolution
php-forum Super User
php-forum Super User
Posts: 197
Joined: Wed Jun 15, 2016 8:35 am

Sat Jan 16, 2021 10:59 am

Remove the ID from the $sql query

Code: Select all

$sql = "INSERT INTO employee (E_Name, E_Last,E_Email )
Post Reply