在使用PHP将数据插入mysql之前截断
我正在尝试使用PHP将excel文件上载到mysql数据库。我让它工作了。 但是,如果我刷新页面或再次上传另一个文件,它会被复制 我希望在它上载新文件之前清除(截断)表,然后插入新数据 但是我找不到将在使用PHP将数据插入mysql之前截断,php,mysql,truncate,Php,Mysql,Truncate,我正在尝试使用PHP将excel文件上载到mysql数据库。我让它工作了。 但是,如果我刷新页面或再次上传另一个文件,它会被复制 我希望在它上载新文件之前清除(截断)表,然后插入新数据 但是我找不到将TRUNCATE表existencias_2018放置在何处或如何放置如果在插入数据之前单击提交按钮 另一个问题是令人耳目一新的事情。这是一种在我上传数据后停止刷新的方法吗?或者是一种刷新不会复制它的方式 总之,我需要的帮助是: 放置在何处以及如何截断表existencias_2018 如果页面刷
TRUNCATE表existencias_2018放置在何处或如何放置代码>如果在插入数据之前单击提交按钮
另一个问题是令人耳目一新的事情。这是一种在我上传数据后停止刷新的方法吗?或者是一种刷新不会复制它的方式
总之,我需要的帮助是:
- 放置在何处以及
如何截断表existencias_2018代码>
- 如果页面刷新,请停止复制数据
以下是我的代码:
<?php
$conn = mysqli_connect("localhost","root","","papa");
require_once('vendor/php-excel-reader/excel_reader2.php');
require_once('vendor/SpreadsheetReader.php');
if (isset($_POST["import"])){
$allowedFileType = ['application/vnd.ms-excel','text/xls','text/xlsx','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'];
if(in_array($_FILES["file"]["type"],$allowedFileType)){
$targetPath = 'uploads/'.$_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'], $targetPath);
$Reader = new SpreadsheetReader($targetPath);
$sheetCount = count($Reader->sheets());
for($i=0;$i<$sheetCount;$i++){
$Reader->ChangeSheet($i);
foreach ($Reader as $Row){
$model = "";
if(isset($Row[0])) {
$model = mysqli_real_escape_string($conn,$Row[0]);
}
$cup = "";
if(isset($Row[1])) {
$cup = mysqli_real_escape_string($conn,$Row[1]);
}
$color = "";
if(isset($Row[1])) {
$color = mysqli_real_escape_string($conn,$Row[2]);
}
$description = "";
if(isset($Row[1])) {
$description = mysqli_real_escape_string($conn,$Row[3]);
}
$size36 = "";
if(isset($Row[1])) {
$size36 = mysqli_real_escape_string($conn,$Row[4]);
}
$size38 = "";
if(isset($Row[1])) {
$size38 = mysqli_real_escape_string($conn,$Row[5]);
}
$size40 = "";
if(isset($Row[1])) {
$size40 = mysqli_real_escape_string($conn,$Row[6]);
}
$size42 = "";
if(isset($Row[1])) {
$size42 = mysqli_real_escape_string($conn,$Row[7]);
}
$size44 = "";
if(isset($Row[1])) {
$size44 = mysqli_real_escape_string($conn,$Row[8]);
}
$size46 = "";
if(isset($Row[1])) {
$size46 = mysqli_real_escape_string($conn,$Row[9]);
}
$size48 = "";
if(isset($Row[1])) {
$size48 = mysqli_real_escape_string($conn,$Row[10]);
}
$size50 = "";
if(isset($Row[1])) {
$size50 = mysqli_real_escape_string($conn,$Row[11]);
}
$size52 = "";
if(isset($Row[1])) {
$size52 = mysqli_real_escape_string($conn,$Row[12]);
}
$size54 = "";
if(isset($Row[1])) {
$size54 = mysqli_real_escape_string($conn,$Row[13]);
}
if (!empty($model) || !empty($cup) || !empty($color) || !empty($description) || !empty($size36) || !empty($size38) || !empty($size40) || !empty($size42) || !empty($size44) || !empty($size46) || !empty($size48) || !empty($size50) || !empty($size52) || !empty($size54)) {
$query = "insert into existencias_2018(model,cup,color,description,size36,size38,size40,size42,size44,size46,size48,size50,size52,size54) values('".$model."','".$cup."','".$color."','".$description."','".$size36."','".$size38."','".$size40."','".$size42."','".$size44."','".$size46."','".$size48."','".$size50."','".$size52."','".$size54."')";
$result = mysqli_query($conn, $query);
if (! empty($result)) {
$type = "success";
$message = "Datos de Excel importados en la base de datos satisfactoriamente";
} else {
$type = "error";
$message = "Ha habido un problema al importar los datos de Excel";
}
}
}
}
}else{
$type = "error";
$message = "Tipo de archivo invalido. Suba un archivo de Excel.";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: Arial;
width: 1000px;
}
.outer-container {
background: #F0F0F0;
border: #e0dfdf 1px solid;
padding: 40px 20px;
border-radius: 2px;
}
.btn-submit {
background: #333;
border: #1d1d1d 1px solid;
border-radius: 2px;
color: #f0f0f0;
cursor: pointer;
padding: 5px 20px;
font-size:0.9em;
}
.tutorial-table {
margin-top: 40px;
font-size: 0.8em;
border-collapse: collapse;
width: 100%;
}
.tutorial-table th {
background: #f0f0f0;
border-bottom: 1px solid #dddddd;
padding: 8px;
text-align: left;
}
.tutorial-table td {
background: #FFF;
border-bottom: 1px solid #dddddd;
padding: 8px;
text-align: left;
}
#response {
padding: 10px;
margin-top: 10px;
border-radius: 2px;
display:none;
}
.success {
background: #c7efd9;
border: #bbe2cd 1px solid;
}
.error {
background: #fbcfcf;
border: #f3c6c7 1px solid;
}
div#response.display-block {
display: block;
}
</style>
</head>
<body>
<h2>Importar existencias actualizadas</h2>
<div class="outer-container">
<form action="" method="post"
name="frmExcelImport" id="frmExcelImport" enctype="multipart/form-data">
<div>
<label>Buscar archivo Excel</label>
<input type="file" name="file" id="file" accept=".xls,.xlsx">
<button type="submit" id="submit" name="import" class="btn-submit">Importar</button>
</div>
</form>
</div>
<div id="response" class="<?php if(!empty($type)) { echo $type . " display-block"; } ?>"><?php if(!empty($message)) { echo $message; } ?></div>
<?php
$sqlSelect = "SELECT * FROM existencias_2018";
$result = mysqli_query($conn, $sqlSelect);
if (mysqli_num_rows($result) > 0){
?>
<table class='tutorial-table'>
<thead>
<tr>
<th>Modelo</th>
<th>Copa</th>
<th>Color</th>
<th>Descripcion</th>
<th>36</th>
<th>38</th>
<th>40</th>
<th>42</th>
<th>44</th>
<th>46</th>
<th>48</th>
<th>50</th>
<th>52</th>
<th>54</th>
</tr>
</thead>
<?php
$sql = "TRUNCATE TABLE existencias_2018";
while ($row = mysqli_fetch_array($result)) {
?>
<tbody>
<tr>
<td><?php echo $row['model']; ?></td>
<td><?php echo $row['cup']; ?></td>
<td><?php echo $row['color']; ?></td>
<td><?php echo $row['description']; ?></td>
<td><?php echo $row['size36']; ?></td>
<td><?php echo $row['size38']; ?></td>
<td><?php echo $row['size40']; ?></td>
<td><?php echo $row['size42']; ?></td>
<td><?php echo $row['size44']; ?></td>
<td><?php echo $row['size46']; ?></td>
<td><?php echo $row['size48']; ?></td>
<td><?php echo $row['size50']; ?></td>
<td><?php echo $row['size52']; ?></td>
<td><?php echo $row['size54']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
}
?>
</body>
</html>
sheets());
对于($i=0;$iChangeSheet($i);
foreach($Reader作为$Row){
$model=“”;
如果(isset($Row[0])){
$model=mysqli\u real\u escape\u字符串($conn,$Row[0]);
}
$cup=“”;
如果(isset($Row[1])){
$cup=mysqli\u real\u escape\u字符串($conn,$Row[1]);
}
$color=“”;
如果(isset($Row[1])){
$color=mysqli\u real\u escape\u字符串($conn,$Row[2]);
}
$description=“”;
如果(isset($Row[1])){
$description=mysqli\u real\u escape\u字符串($conn,$Row[3]);
}
$size36=“”;
如果(isset($Row[1])){
$size36=mysqli\u real\u escape\u字符串($conn,$Row[4]);
}
$size38=“”;
如果(isset($Row[1])){
$size38=mysqli\u real\u escape\u字符串($conn,$Row[5]);
}
$size40=“”;
如果(isset($Row[1])){
$size40=mysqli\u real\u escape\u字符串($conn,$Row[6]);
}
$size42=“”;
如果(isset($Row[1])){
$size42=mysqli\u real\u escape\u字符串($conn,$Row[7]);
}
$size44=“”;
如果(isset($Row[1])){
$size44=mysqli\u real\u escape\u字符串($conn,$Row[8]);
}
$size46=“”;
如果(isset($Row[1])){
$size46=mysqli\u real\u escape\u字符串($conn,$Row[9]);
}
$size48=“”;
如果(isset($Row[1])){
$size48=mysqli\u real\u escape\u字符串($conn,$Row[10]);
}
$size50=“”;
如果(isset($Row[1])){
$size50=mysqli\u real\u escape\u字符串($conn,$Row[11]);
}
$size52=“”;
如果(isset($Row[1])){
$size52=mysqli\u real\u escape\u字符串($conn,$Row[12]);
}
$size54=“”;
如果(isset($Row[1])){
$size54=mysqli\u real\u escape\u字符串($conn,$Row[13]);
}
如果(!empty($model)| |!empty($cup)|!empty($color)|!empty($description)| |!empty($size36)| |!empty($size40)| |!empty($size42)| |!empty($size46)| |!empty($size48)| |!empty($size50)|!empty($size54)|!empty($size54)| |{
$query=“插入existencias_2018(型号、杯型、颜色、说明、尺寸36、尺寸38、尺寸40、尺寸42、尺寸44、尺寸46、尺寸48、尺寸50、尺寸52、尺寸54)值(“$model.”、“$cup.”、“$cup.”、“$color.”、“$description.”、“$size36.”、“$size38.”、“$size40.”、“$size42.”、“$size44.”、“$size46.”、“$size48.”、$size42.”、“$size54.”、$size54.”、$size46.“$size54.”;
$result=mysqli\u查询($conn,$query);
如果(!空($result)){
$type=“成功”;
$message=“满足基本数据的Excel导入数据”;
}否则{
$type=“error”;
$message=“Ha habido un problema al importar los datos de Excel”;
}
}
}
}
}否则{
$type=“error”;
$message=“文件无效。文件无效。”;
}
}
?>
身体{
字体系列:Arial;
宽度:1000px;
}
.外容器{
背景:#f0;
边框:#e0dfdf 1px实心;
填充:40px 20px;
边界半径:2px;
}
.btn提交{
背景:#333;
边框:#1d1px实心;
边界半径:2px;
颜色:#f0;
光标:指针;
填充:5px20px;
字号:0.9em;
}
.教程表{
边缘顶端:40px;
字号:0.8em;
边界塌陷:塌陷;
宽度:100%;
}
.教程表th{
背景:#f0;
边框底部:1px实心#dddddd;
填充:8px;
文本对齐:左对齐;
}
.教程表td{
背景:#FFF;
边框底部:1px实心#dddddd;
填充:8px;
文本对齐:左对齐;
}
#回应{
填充:10px;
边缘顶部:10px;
边界半径:2px;
显示:无;
}
.成功{
背景:c7efd9;
边框:#bbe2cd 1px实心;
}
.错误{
背景:#fbb;
边框:#f3c6c7 1px实心;
}
<?php
$conn = mysqli_connect("localhost","root","","papa");
require_once('vendor/php-excel-reader/excel_reader2.php');
require_once('vendor/SpreadsheetReader.php');
if (isset($_POST["import"])){
$allowedFileType = ['application/vnd.ms-excel','text/xls','text/xlsx','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'];
if(in_array($_FILES["file"]["type"],$allowedFileType)){
// Looks like we have a correct file to upload.
// Let's TRUNCATE the table first :
$query = "TRUNCATE TABLE existencias_2018;";
mysqli_query($conn, $query);
// OK, table is empty, proceed with upload....
$targetPath = 'uploads/'.$_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'], $targetPath);
$Reader = new SpreadsheetReader($targetPath);
$sheetCount = count($Reader->sheets());
for($i=0;$i<$sheetCount;$i++){
$Reader->ChangeSheet($i);
foreach ($Reader as $Row){
$model = "";
if(isset($Row[0])) {
$model = mysqli_real_escape_string($conn,$Row[0]);
}
// and so on...