simplexlxs的问题

时间:2018-07-20 14:08:27

标签: php excel pdo

我有一个带有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();
}

1 个答案:

答案 0 :(得分:0)

您正在准备语句$st

$st = $conn->prepare( "DELETE FROM procura");

但是不要执行它。您要么需要在语句句柄上调用execute()

$st = $conn->prepare( "DELETE FROM procura");
$st->execute();

或者因为只使用一次语句而准备没有好处,所以可以只使用query()

$conn->query("DELETE FROM procura");