为什么多查询只在一个表单中运行一次?
why multi query runs only once in a form?
我是 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>";}
}
}
?>
如果 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...
我是 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>";}
}
}
?>
如果 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...