![](/img/trans.png)
[英]how to update database table each time csv file get uploaded in codeigniter?
[英]How to update mysql database table each time csv file get uploaded - problem here is I cannot update the "date column"
我的代码中的主要问题是我无法使用 php 将“日期”从 csv 文件更新到 mysql 数据库表。代码行 $date = mysqli_real_escape_string($connect, $data[1]); 是这里的主要问题。 我正在寻找此特定代码行的任何替代查询。
这是 csv 文件: https://drive.google.com/file/d/1EdMKo-XH7VOXS5HqUh8-m0uWfomcYL5T/view?usp=sharing
这是完整的代码:
<?php
//index.php
$connect = mysqli_connect("localhost", "root", "1234", "ml_database");
$message = '';
if(isset($_POST["upload"])) {
if($_FILES['product_file']['name']) {
$filename = explode(".", $_FILES['product_file']['name']);
if(end($filename) == "csv") {
$handle = fopen($_FILES['product_file']['tmp_name'], "r");
while($data = fgetcsv($handle)) {
$data_id = mysqli_real_escape_string($connect, $data[0]);
$date = mysqli_real_escape_string($connect, $data[1]); //My Problem
$births = mysqli_real_escape_string($connect, $data[2]);
$query = "UPDATE my_table
SET date = '$date',
births = '$births',
WHERE data_id = '$data_id'";
mysqli_query($connect, $query);
}
fclose($handle);
header("location: index.php?updation=1");
} else {
$message = '<label class="text-danger">Please Select CSV File only</label>';
}
} else {
$message = '<label class="text-danger">Please Select File</label>';
}
}
if(isset($_GET["updation"])) {
$message = '<label class="text-success">Product Updation Done</label>';
}
$query = "SELECT * FROM my_table";
$result = mysqli_query($connect, $query);
?>
<!DOCTYPE html>
<html>
<head>
<title>Update Mysql Database through Upload CSV File using PHP</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<br />
<div class="container">
<h2 align="center">Update Mysql Database through Upload CSV File using PHP</a></h2>
<br />
<form method="post" enctype='multipart/form-data'>
<p><label>Please Select File(Only CSV Formate)</label>
<input type="file" name="product_file" /></p>
<br />
<input type="submit" name="upload" class="btn btn-info" value="Upload" />
</form>
<br />
<?php echo $message; ?>
<h3 align="center">Birthss</h3>
<br />
<div class="table-responsive">
<table class="table table-bordered table-striped">
<tr>
<th>Date</th>
<th>Births</th>
</tr>
<?php
while($row = mysqli_fetch_array($result))
{
echo '
<tr>
<td>'.$row["date"].'</td>
<td>'.$row["births"].'</td>
</tr>
';
}
?>
</table>
</div>
</div>
</body>
</html>
首先,您需要读取并丢弃 CSV 中的 Title 行。 然后使用适当的、准备好的和参数化的查询,您可以正确更新数据库。 由于 .csv 文件中的日期格式正确,因此无需在此处进行任何操作,但其他 CSV 文件可能并非总是如此,通常必须重新格式化日期才能正确存储在表中。
<?php
//index.php
$connect = mysqli_connect("localhost", "root", "1234", "ml_database");
$message = '';
if(isset($_POST["upload"])) {
if($_FILES['product_file']['name']) {
$filename = explode(".", $_FILES['product_file']['name']);
if(end($filename) == "csv") {
$handle = fopen($_FILES['product_file']['tmp_name'], "r");
// read and ignore the TITLE line
$data = fgetcsv($handle, 1000, ",");
// prepare the query ONCE
$query = "UPDATE my_table
SET date = ?,
births = ?
WHERE data_id = ?";
$stmt = $connect->prepare($query);
// loop over the remaining rows of the csv
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$stmt->bind_param('sss', $data[0],$data[1],$data[2]);
$stmt->execute();
}
fclose($handle);
header("location: index.php?updation=1");
exit; // always after a header('Location:.....')
} else {
$message = '<label class="text-danger">Please Select CSV File only</label>';
}
} else {
$message = '<label class="text-danger">Please Select File</label>';
}
}
注意:我假设所有 3 列都是绑定中的文本...
$stmt->bind_param('sss', $data[0],$data[1],$data[2]);
^^^
如果date_id
是 integer 您可以将其更改为'ssi'
尽管 3 s
几乎肯定会起作用。
参考:
这是我在工作区@@RiggsFolly 中的当前代码,也许我把代码放错了/排列错误,这就是为什么我不能反映我的表的任何更新。 你能通过先生看到吗?
<?php
//index.php
$connect = mysqli_connect("localhost", "root", "1234", "ml_database");
$message = '';
if(isset($_POST["upload"])) {
if($_FILES['product_file']['name']) {
$filename = explode(".", $_FILES['product_file']['name']);
if(end($filename) == "csv") {
$handle = fopen($_FILES['product_file']['tmp_name'], "r");
// read and ignore the TITLE line
$data = fgetcsv($handle, 1000, ",");
// prepare the query ONCE
$query = "UPDATE my_table
SET date = ?,
births = ?
WHERE data_id = ?";
$stmt = $connect->prepare($query);
// loop over the remaining rows of the csv
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$stmt->bind_param('sss', $data[0],$data[1],$data[2]);
$stmt->execute();
}
fclose($handle);
header("location: index.php?updation=1");
exit; // always after a header('Location:.....')
}
else {
$message = '<label class="text-danger">Please Select CSV File only</label>';
}
} else {
$message = '<label class="text-danger">Please Select File</label>';
}
}
if(isset($_GET["updation"]))
{
$message = '<label class="text-success">Product Updation Done</label>';
}
$query = "SELECT * FROM my_table";
$result = mysqli_query($connect, $query);
?>
<!DOCTYPE html>
<html>
<head>
<title>Update Mysql Database through Upload CSV File using PHP</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<br />
<div class="container">
<h2 align="center">Update Mysql Database through Upload CSV File using PHP</a></h2>
<br />
<form method="post" enctype='multipart/form-data'>
<p><label>Please Select File(Only CSV Formate)</label>
<input type="file" name="product_file" /></p>
<br />
<input type="submit" name="upload" class="btn btn-info" value="upload" />
</form>
<br />
<?php echo $message; ?>
<h3 align="center">Birthss</h3>
<br />
<div class="table-responsive">
<table class="table table-bordered table-striped">
<tr>
<th>Date</th>
<th>Births</th>
</tr>
<?php
while($row = mysqli_fetch_array($result))
{
echo '
<tr>
<td>'.$row["date"].'</td>
<td>'.$row["births"].'</td>
</tr>
';
}
?>
</table>
</div>
</div>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.