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 GURU
php-forum GURU
Posts: 212
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 )
wernereegranad75
New php-forum User
New php-forum User
Posts: 5
Joined: Tue Apr 20, 2021 7:32 pm

Tue Apr 20, 2021 8:52 pm

I also can't upload csv to mysql db lately and I'm wondering if it just my internet connection issue or it was the server? Anyway, it was already fixed earlier this morning and I can continue on doing the projects that I had. play hogwarts mystery on pc and unleash the magical powers of your characters in the game. There are different characters to choose from and you can also get magical spells on some of your quest. If you are into horror game then get the latest version of granny game for free
Post Reply