嗨,我正在尝试将新的csvfile添加到文件夹中时,它将插入到数据库中,但是我不知道该怎么做。 这是我的csv文件https://i.gyazo.com/5f97c964c12f9f38fe4f6fbc901158a8.png
的eksample我将首先展示我的<?php
class DB extends \PDO
{
/**
* @var
*/
private $host;
/**
* @var
*/
private $user;
/**
* @var
*/
private $pass;
/**
* @var PDO
*/
private $conn;
/**
* @var
*/
private $query;
/**
* @var bool
*/
private $debug = false;
/**
* @param bool $bool
*/
public function debug($bool = true)
{
$this->debug = $bool;
}
/**
* @param $sql
*/
private function setQuery($sql)
{
$this->query = $this->prepare($sql);
}
/**
* DB constructor.
* @param $dbhost
* @param $dbuser
* @param $dbpass
* @param array $options
*/
public function __construct($dbhost, $dbuser, $dbpass, $options = [])
{
try {
$this->conn = parent::__construct($dbhost, $dbuser, $dbpass, $options);
} catch (\PDOException $e) {
print "Fejl!: " . $e->getMessage() . "<br/>";
die();
}
}
/**
* @param string $sql
* @param bool $params
* @param int|mixed|null $returnType
* @return mixed
*/
public function query($sql, $params = false, $returnType = \PDO::FETCH_OBJ)
{
$this->setQuery($sql);
$this->execute($params);
$this->count = $this->query->rowCount();
return $this->query->fetchAll($returnType);
}
/**
* @param $params
*/
private function execute($params)
{
if($params){
$this->query->execute($params);
} else {
$this->query->execute();
}
if($this->debug){
echo '<pre id="debug_params">',$this->query->debugDumpParams(),'</pre>';
}
}
/**
* @param $sql
* @param bool $params
* @return mixed
*/
public function single($sql, $params = false){
$data = $this->query($sql, $params);
if(sizeof($data) === 1){
return $data[0];
} else {
return false;
}
}
/**
* @param $sql
* @param bool $params
* @return mixed
*/
public function first($sql, $params = false){
return $this->query($sql, $params)[0];
}
/**
* @param $sql
* @param bool $params
* @return mixed
*/
public function last($sql, $params = false){
return $this->query($sql, $params)[$this->count - 1];
}
/**
* @param $sql
* @param bool $params
* @return mixed
*/
public function toList($sql, $params = false){
return $this->query($sql, $params);
}
/**
* @param $sql
* @param bool $params
* @return mixed
*/
public function lastId($sql, $params = false){
$this->query($sql, $params);
return $this->lastInsertId();
}
}
类,我将使用它来执行我的数据。
我知道这段代码中显示了2个表,用于测试我是否获得了正确的数据
csvtable.php
这是我的<?php
if ($user->is_loggedin() == true) {
// File selector
$path = "./assets/csv";
$latest_ctime = 0;
$latest_filename = '';
$d = dir($path);
while (false !== ($entry = $d->read())) {
$filepath = "{$path}/{$entry}";
// could do also other checks than just checking whether the entry is a file
if (is_file($filepath) && filectime($filepath) > $latest_ctime){
$latest_ctime = filectime($filepath);
$latest_filename = $entry;
}
}
// File selector end
echo'<h1 class="text-center">CSV Table</h1>';
echo'<h6 class="text-center">'.$latest_filename.'</h6>';
echo '<table class=" table table-striped table-bordered table-hover" style="width:100%">';
echo'<tbody>';
$f = fopen("$path/$latest_filename", "r");
while (($row = fgetcsv($f, 1000, ";")) !== false) {
//Create table using the filename as tablename, if table already exist insert data.
$csv->createCsvTable($row);
echo '<tr>';
foreach ($row as $cell) {
echo '<td>' .htmlspecialchars($cell, ENT_COMPAT). '</td>';
}
echo '</tr>';
}
fclose($f);
echo'</tbody>';
echo '</table>';
//csv table end
?><table id="example" class="table-hover table table-striped table-bordered" style="width:100%">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Datum</th>
<th scope="col">Property</th>
<th scope="col">Criterion</th>
<th scope="col">Type</th>
<th scope="col">Nominal</th>
<th scope="col">Actual</th>
<th scope="col">Tol-</th>
<th scope="col">Tol+</th>
<th scope="col">Dev</th>
</tr>
</thead>
<tbody>
<?php foreach($csv->getCsv() as $csv) { ?>
<tr>
<td><?= $csv->Name; ?></td>
<td><?= $csv->Datum; ?></td>
<td><?= $csv->Property; ?></td>
<td><?= $csv->Criterion; ?></td>
<td><?= $csv->Type; ?></td>
<td><?= $csv->Nominal; ?></td>
<td><?= $csv->Actual; ?></td>
<td><?= $csv->Tolminus; ?></td>
<td><?= $csv->Tolplus; ?></td>
<td><?= $csv->Dev; ?></td>
</tr>
<?php }?>
</tbody>
</table>
<?php }
我的索引文件,这是用户会看到的。
createCsvTable
这是我的CSV类,在其中我不知道如何编写<?php
class Csv extends \PDO {
private $db = null;
/**
* Settings constructor.
*/
public function __construct($db)
{
$this->db = $db;
}
/**
* Menu & Admin Menu
* @return mixed
*/
public function getCsv()
{
return $this->db->toList("SELECT * FROM Test");
}
public function createCsvTable()
{
return $this->db->toList("CREATE TABLE IF NOT EXISTS Test(
id int (11),
Name VARCHAR(30),
Datum VARCHAR(30),
Property VARCHAR(30),
Criterion VARCHAR(30),
Type VARCHAR(30),
Nominal DECIMAL(10,2),
Actual DECIMAL(10,2),
Tolminus DECIMAL(10,2),
Tolplus DECIMAL(10,2),
Dev DECIMAL(10,2)
);
/* INSERT QUERY */
INSERT INTO Testeses(
id, Name, Datum, Property, Criterion, Type,
Nominal, Actual, Tolminus, Tolplus, Dev
)
VALUES
(
?,?,?,?,?,?,
?,?,?,?,?
);");
}
}
,我要做的就是将csv文件数据插入到我创建的表中。
baseBranch curBranch curRevision
1.36.1.5 1.36 1.36.1. 5
1.31 <empty> 1. 31
1.14.2.21.1.16.1.13 1.14.2.21.1.16 1.14.2.21.1.16.1. 13
1.31.34 <no match (illegal number - always have to be pairs)>
1.31.34.2.4 <no match (illegal number - always have to be pairs)>
1.31.34.2.4.4.5 <no match (illegal number - always have to be pairs)>
我知道如果我更换了?与一些实际的数据将被插入,但我需要它来自csv文件。这是主要问题
因此,为了清楚起见,我的问题是“如何将csv文件中的数据插入数据库?”
如果您知道如何用文件名命名表,我将不胜感激。
答案 0 :(得分:0)
CSV是一种“逗号分隔值”格式,在您的数据库中具有“索引”。因此,要保存一个csv文件,您需要加载它,遍历每一行,然后在查询中插入它。因此您的代码可能如下所示:
$stmt = $pdo->prepare('INSERT INTO Testeses(id, Name, Datum, ...)
VALUES (
:id, :name, :date, ...
);');
foreach($csvLines as $line) {
$stmt->bindValue(':id', $line[0]);
$stmt->bindValue(':name', $line[1]);
$stmt->bindValue(':date', $line[2]);
$stmt->execute();
}
您需要知道特定值位于哪个位置。
答案 1 :(得分:0)
我在下面发布了一个解决方案。也许阅读此书以了解PDO和PreparedStatements,以便您了解如何将它们绑定到列。 您需要更改执行方法并为您准备pdo对象,因为我不知道您如何在其中管理它来执行语句。
FormArray
答案 2 :(得分:0)
经过一些测试和head头之后,我发现了一个不错的解决方案。这是什么
给那些可能想使用它的人的笔记。注意这个“;”您 可能必须将其更改为“,”,具体取决于文件的方式 分开。我的文件使用“;”分隔我正在使用
utf8_encode
,以便我可以使用useØÅ之类的特殊字符 您必须进行的更改位于此时间。
while ($getdata = fgetcsv($openfile, 1000, ";")) {
$getdata = array_map("utf8_encode", $getdata);
$total = count($getdata);
// echo "<b>Row no:-</b>$row\n";
// echo "<b>Total fields in this row:-</b>$total\n";
$row++;
for ($c=0; $c < $total; $c++) {
$csvdata = implode(";", $getdata);
$fncsvdata = explode(";", $csvdata);
}
我还创建了一个表,以显示如何在网站上获得输出。
这是答案
这是用户看到的文件。在我的情况下,它称为csvtable.php
<?php
// File selector
$path = "./assets/csv";
$sourcepath = "assets/csv";
$copy = "assets/proceskontrol-csv";
$latest_ctime = 0;
$latest_filename = '';
$d = dir($path);
while (false !== ($entry = $d->read())) {
$filepath = "{$path}/{$entry}";
// could do also other checks than just checking whether the entry is a file
if (is_file($filepath) && filectime($filepath) > $latest_ctime){
$latest_ctime = filectime($filepath);
$latest_filename = $entry;
}
}
if (1+1 == 2){
copy("$sourcepath/$latest_filename","$copy/$latest_filename");
$csv->createCsvTable($latest_filename);
$row = 1;
if (($openfile = fopen("$copy/$latest_filename", "r")) !== FALSE) {
while ($getdata = fgetcsv($openfile, 1000, ";")) {
$getdata = array_map("utf8_encode", $getdata);
$total = count($getdata);
// echo "<b>Row no:-</b>$row\n";
// echo "<b>Total fields in this row:-</b>$total\n";
$row++;
for ($c=0; $c < $total; $c++) {
$csvdata = implode(";", $getdata);
$fncsvdata = explode(";", $csvdata);
}
$csv->insertCsvTable($latest_filename ,$fncsvdata);
}
}
}
echo'<h1 class="text-center">CSV Table</h1>';
echo'<h6 class="text-center">'.$latest_filename.'</h6>';
echo '<table class=" table table-striped table-bordered table-hover">';
echo'<tbody>';
?>
<table class="table-hover table table-striped table-bordered">
<thead>
<tr>
<th scope="col">id</th>
<th scope="col">Name</th>
<th scope="col">Datum</th>
<th scope="col">Property</th>
<th scope="col">Criterion</th>
<th scope="col">Type</th>
<th scope="col">Nominal</th>
<th scope="col">Actual</th>
<th scope="col">Tol-</th>
<th scope="col">Tol+</th>
<th scope="col">Dev</th>
</tr>
</thead>
<tbody>
<?php foreach($csv->getCsv($latest_filename) as $csv) { ?>
<tr>
<td><?= $csv->id; ?></td>
<td><?= $csv->Name; ?></td>
<td><?= $csv->Datum; ?></td>
<td><?= $csv->Property; ?></td>
<td><?= $csv->Criterion; ?></td>
<td><?= $csv->Type; ?></td>
<td><?= $csv->Nominal; ?></td>
<td><?= $csv->Actual; ?></td>
<td><?= $csv->Tolminus; ?></td>
<td><?= $csv->Tolplus; ?></td>
<td><?= $csv->Dev; ?></td>
</tr>
<?php }?>
</tbody>
</table>
这是我的数据库类文件DB.php
。在CSV.php
<?php
class DB extends \PDO
{
/**
* @var
*/
private $host;
/**
* @var
*/
private $user;
/**
* @var
*/
private $pass;
/**
* @var PDO
*/
private $conn;
/**
* @var
*/
private $query;
/**
* @var bool
*/
private $debug = false;
/**
* @param bool $bool
*/
public function debug($bool = true)
{
$this->debug = $bool;
}
/**
* @param $sql
*/
private function setQuery($sql)
{
$this->query = $this->prepare($sql);
}
/**
* DB constructor.
* @param $dbhost
* @param $dbuser
* @param $dbpass
* @param array $options
*/
public function __construct($dbhost, $dbuser, $dbpass, $options = [])
{
try {
$this->conn = parent::__construct($dbhost, $dbuser, $dbpass, $options);
} catch (\PDOException $e) {
print "Fejl!: " . $e->getMessage() . "<br/>";
die();
}
}
/**
* @param string $sql
* @param bool $params
* @param int|mixed|null $returnType
* @return mixed
*/
public function query($sql, $params = false, $returnType = \PDO::FETCH_OBJ)
{
$this->setQuery($sql);
$this->execute($params);
$this->count = $this->query->rowCount();
return $this->query->fetchAll($returnType);
}
/**
* @param $params
*/
private function execute($params)
{
if($params){
$this->query->execute($params);
} else {
$this->query->execute();
}
if($this->debug){
echo '<pre id="debug_params">',$this->query->debugDumpParams(),'</pre>';
}
}
/**
* @param $sql
* @param bool $params
* @return mixed
*/
public function single($sql, $params = false){
$data = $this->query($sql, $params);
if(sizeof($data) === 1){
return $data[0];
} else {
return false;
}
}
/**
* @param $sql
* @param bool $params
* @return mixed
*/
public function first($sql, $params = false){
return $this->query($sql, $params)[0];
}
/**
* @param $sql
* @param bool $params
* @return mixed
*/
public function last($sql, $params = false){
return $this->query($sql, $params)[$this->count - 1];
}
/**
* @param $sql
* @param bool $params
* @return mixed
*/
public function toList($sql, $params = false){
return $this->query($sql, $params);
}
/**
* @param $sql
* @param bool $params
* @return mixed
*/
public function lastId($sql, $params = false){
$this->query($sql, $params);
return $this->lastInsertId();
}
}
这是CSV.php
,这是我创建表并插入数据的地方。 (SQL调用)
<?php
class Csv extends \PDO {
private $db = null;
/**
* Settings constructor.
*/
public function __construct($db)
{
$this->db = $db;
}
/**
* Menu & Admin Menu
* @return mixed
*/
public function getCsv($latest_filename)
{
return $this->db->toList("SELECT * FROM `$latest_filename`");
}
public function createCsvTable($latest_filename)
{
return $this->db->toList("CREATE TABLE IF NOT EXISTS `$latest_filename`(
id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(50),
Datum VARCHAR(50),
Property VARCHAR(50),
Criterion VARCHAR(50),
Type VARCHAR(50),
Nominal DECIMAL(10,2),
Actual DECIMAL(10,2),
Tolminus DECIMAL(10,2),
Tolplus DECIMAL(10,2),
Dev DECIMAL(10,2))
");
}
public function insertCsvTable($latest_filename ,$fncsvdata)
{
return $this->db->toList("INSERT INTO `$latest_filename` (`Name`, `Datum`, `Property`, `Criterion`, `Type`, `Nominal`, `Actual`,`Tolminus`,`Tolplus`,`Dev`)
VALUES (:Name, :Datum, :Property, :Criterion, :Type, :Nominal, :Actual, :Tolminus, :Tolplus, :Dev)",
[
':Name' => $fncsvdata[0],
':Datum'=> $fncsvdata[1],
':Property'=> $fncsvdata[2],
':Criterion'=> $fncsvdata[3],
':Type'=> $fncsvdata[4],
':Nominal'=> $fncsvdata[5],
':Actual'=> $fncsvdata[6],
':Tolminus'=> $fncsvdata[7],
':Tolplus'=> $fncsvdata[8],
':Dev'=> $fncsvdata[9]
]);
}
}
我真的希望有人可以使用它。