【问题标题】:why multi query runs only once in a form? [closed]为什么多查询在一个表单中只运行一次? [关闭]
【发布时间】:2021-10-09 15:32:45
【问题描述】:

这是 Ryham,我是代码编写的新手,我需要知道是什么使它成功运行,但我的 csv 文件中只有一行。代码如下。感谢您对此的任何帮助。 (:

<?php
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
ini_set('max_execution_time',0);
ini_set('upload_max_filesize', '50M');
ini_set('post_max_size', '50M');
ini_set('max_input_time', 0);
ini_set("memory_limit", "-1");
set_time_limit(0);
$ip = getenv('REMOTE_ADDR');
$conn=mysqli_connect("somehost","someusr","somepassword", "somebd") or die("Could not connect");



if(isset($_POST["submit_file"]))
{
 $op = $_POST['op'];
 $month = $_POST['month'];
 $year = $_POST['year'];
 $file = $_FILES["file"]["tmp_name"];
 $file_open = fopen($file,"r");
 while(($csv = fgetcsv($file_open,1000, ",")) !== false)
 {
  $ct = $csv[0];
  $ts = $csv[1];
  $cd = $csv[2];
  $pc = $csv[3];
  $query="INSERT INTO anew(`ct`, `ts`, `cd`, `pc`, `uploadedby`, `op`, `month`, `year`) VALUES ('$ct','$ts','$cd','$pc','$ip', '$op', '$month', '$year'); update anew set wtd = true";
  $result= $conn->multi_query($query);
  if($result)
                {

                    echo "<script type=\"text/javascript\">
                            alert(\"file is upload successfully\");
                            window.location.href = '/insights/datauploader/';
                            
                        </script>";
                
                }
                else{echo"<script type=\"text/javascript\">
                            alert(\"there is some error\");
                            window.location.href = '/insights/datauploader/';
                        </script>";}
 }
}
?>

【问题讨论】:

  • 也许$csv在第一次迭代后返回false,也许您应该调试并在调试后告诉我们更多信息。
  • 有什么问题?
  • @Jaquarh 有什么具体的调试方法吗?
  • @MRDev 问题是我的测试 csv 文件的 10 行中只有一行被插入到我的数据库表中
  • @MRDev 这不会产生影响,服务器不执行 JavaScript,浏览器执行。可能的问题是在某处抛出异常或$csv 在第一次迭代后返回 false .. 需要更多调试来回答这个问题。

标签: php csv mysqli


【解决方案1】:

如果multi_query 是问题所在。您可以将wtd 传递到INSERT 中,插入后无需更新行。

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
ini_set('max_execution_time',0);
ini_set('upload_max_filesize', '50M');
ini_set('post_max_size', '50M');
ini_set('max_input_time', 0);
ini_set("memory_limit", "-1");
set_time_limit(0);

if(array_diff([ # all keys should be checked, not just a submit button
    'op',
    'month',
    'year',
    'file'
], array_keys($_POST))) die('Missing required parameters.');

$ip = getenv('REMOTE_ADDR');

$db = new PDO('mysql:host=somehost;dbname=somedb;port=3306', 'someuser', 'somepass', [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
]);

# transaction block for integrity in case of large CSV files
$db->beginTransaction();

while(($csv = fgetcsv(file_get_contents($_FILES['file']['tmp_name']), 1000, ',')) !== false)
{
    list($ct, $ts, $cd, $pc) = array_splice($csv, 0, 4);
    
    ($db->Prepare('INSERT INTO anew(`ct`, `ts`, `cd`, `pc`, `uploadedby`, `op`, `month`, `year`, `wtd`) VALUES (?, ?, ?, ?, ?, ?, ? ,?, ?)'))
    ->execute([
        $ct,
        $ts,
        $cd,
        $pc,
        $_POST['op'],
        $_POST['month'],
        $_POST['year'],
        true # Why the update? You can override default values in an insert
    ]);
}

$db->commit();

我使用 Prepared statements 为您防止 SQLi...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-17
    • 1970-01-01
    相关资源
    最近更新 更多