我有一个带有simplexlsx的代码,但是当我上传.xlsx文件时,这会将文件两次插入到我的mysql数据库中,并且我试图在插入新文件之前删除数据库中的所有行,但这不是发生了,我该如何避免呢?
if (isset($_FILES['file'])) {
require_once __DIR__ . '/simplexlsx.class.php';
if ( $xlsx = SimpleXLSX::parse( $_FILES['file']['tmp_name'] ) ) {
$st = $conn->prepare( "DELETE FROM procura");
$stmt = $conn->prepare( "INSERT INTO paises (item, codigo_sap, descripcion, und, menor_precio, peso, volumen, provmenor, paisprov, nproceso, emprescon, econtra, fecha) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->bindParam( 1, $item);
$stmt->bindParam( 2, $codsap);
$stmt->bindParam( 3, $desc);
$stmt->bindParam( 4, $und);
$stmt->bindParam( 5, $menor);
$stmt->bindParam( 6, $peso);
$stmt->bindParam( 7, $volumen);
$stmt->bindParam( 8, $provme);
$stmt->bindParam( 9, $provpis);
$stmt->bindParam( 10, $proceso);
$stmt->bindParam( 11, $econcur);
$stmt->bindParam( 12, $econtr);
$stmt->bindParam( 13, $fecha);
foreach ( $xlsx->rows() as $fields ) {
if($fields== 1){ $fields++; continue; }
$item = $fields[0];
$codsap = $fields[1];
$desc = $fields[2];
$und = $fields[3];
$menor = $fields[4];
$peso = $fields[5];
$volumen = $fields[6];
$provme = $fields[7];
$provpis = $fields[8];
$proceso = $fields[9];
$econcur = $fields[10];
$econtr = $fields[11];
$fecha = $fields[12];
$stmt->execute();
}
} else {
echo SimpleXLSX::parse_error();
}