duanjiu4498 2018-07-05 17:23
浏览 60

通过php主键将大量Excel信息上传到MySQL

I'm having this issue I have figure out a way to upload my data which is on .xls format and input the correct info on mysql. I'm using phpmyadmin and php to upload the file but right now I'm having trouble with primary keys... sorry I'm kinda new programming trough php, and I don't know why, once I define my primary key and the script send the data the primary key wont respect and don't assign the same id(primarykey) to the same value for example :

enter image description here

and my code to input data is as follows :

<?php 
require_once 'db_con.php';
if(isset($_POST["submit_file"]))
{ 
    $file = $_FILES["file"]["tmp_name"];
    $file_open = fopen($file,"r");
    while(($csv = fgetcsv($file_open, 1000, ",")) !== false) 
    {
         $fecha= $csv[0]; 
         $tipo = $csv[1]; 
         $impresion = $csv[2]; 
         $hojas= $csv[3]; 
         $cadena_nombre = explode('(', $csv[4]);
         $usuario =$cadena_nombre [0];
         $usuario =$csv[4];
         $stmt = $DBcon->prepare("INSERT INTO trabajo 
                                    (fecha,tipo,impresion,hojas,usuario) 
                            VALUES(:fecha,:tipo,:impresion,:hojas,:usuario)");

        $stmt->bindparam (':fecha', $fecha);
        $stmt->bindparam(':tipo', $tipo);
        $stmt->bindparam(':impresion', $impresion);
        $stmt->bindparam(':hojas', $hojas);
        $stmt->bindparam(':usuario', $usuario);

        $stmt->execute();
    }
}

echo "CSV Imported Successfully";
?>

My DDL looks like this :

CREATE TABLE `trabajo` (
  `fecha` date NOT NULL,
  `hojas` int(25) NOT NULL,
  `impresion` int(25) NOT NULL,
  `tipo` varchar(60) NOT NULL,
  `usuario` varchar(60) NOT NULL,
  `usuario_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `trabajo`
  ADD PRIMARY KEY (`usuario_id`);

--
-- AUTO_INCREMENT de las tablas volcadas
--

--
-- AUTO_INCREMENT de la tabla `trabajo`
--
ALTER TABLE `trabajo`
  MODIFY `usuario_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=12592;
COMMIT;
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)