[英]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.