|
|
|
Import csv เข้าใน database แต่ row แรกจะมีเลข 0 ติดมาด้วยทุกครั้งค่ะ ต้องแก้ไขอย่างไรคะ |
|
|
|
|
|
|
|
Code (PHP)
if (isset($_POST['submit']))
{
$fileMimes = array(
'text/x-comma-separated-values',
'text/comma-separated-values',
'application/octet-stream',
'application/vnd.ms-excel',
'application/x-csv',
'text/x-csv',
'text/csv',
'application/csv',
'application/excel',
'application/vnd.msexcel',
'text/plain'
);
// Validate selected file is a CSV file or not
if (!empty($_FILES['file']['name']) && in_array($_FILES['file']['type'], $fileMimes))
{
// Open uploaded CSV file with read-only mode
$csvFile = fopen($_FILES['file']['tmp_name'], 'r');
// Skip the first line
fgetcsv($csvFile);
// Parse data from CSV file line by line
while (($getData = fgetcsv($csvFile, 10000, ",")) !== FALSE)
{
// Get row data
$idno = $getData[2];
$name = $getData[3];
$birth = $getData[4];
$tel = $getData[5];
$claim = $getData[6];
$subclaim = $getData[7];
$claimcode = $getData[9];
$datewalk = $getData[15];
//If user already exists in the database with the same email
/*$query = "SELECT id FROM users WHERE email = '" . $getData[1] . "'";
$check = mysqli_query($con, $query);
if ($check->num_rows > 0)
{
mysqli_query($conn, "UPDATE users SET name = '" . $name . "', created_at = NOW() WHERE email = '" . $email . "'");
}
else
{*/
$strSQL1 = "INSERT INTO AUTHEN (AUNO, AUNAME, AUBIRTH ,
AUTEL ,AUCLAIM ,AUSUBCLAIM ,AUCODE ,AUDATE) VALUES ('" . $idno . "',
'" . $name . "',
'" . $birth . "',
'" . $tel . "',
'" . $claim . "',
'" . $subclaim . "',
'" . $claimcode . "',
'" . $datewalk . "')";
$datasearch = odbc_exec($c_odbc, $strSQL1) or die("ผิดพลาด ค้นหาไม่ได้");
/*mysqli_query($con, "INSERT INTO AUTHEN (AUID, AUNO, AUNAME, AUBIRTH ,
AUTEL ,AUCLAIM ,AUSUBCLAIM ,AUCODE ,AUDATE) VALUES ('" . $idno . "', '" . $name . "','" . $birth . "','" . $tel . "','" . $claim . "','" . $subclaim . "','" . $claimcode . "','" . $datewalk . "'");
}*/
}
// Close opened CSV file
fclose($csvFile);
header("Location: f16.php");
}
else
{
echo "Please select valid file";
}
}
Tag : PHP, Excel (Excel.Application)
|
|
|
|
|
|
Date :
2023-06-26 15:55:50 |
By :
mookmixxwipwap |
View :
353 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1. กำหนด field type เป็น numeric โดยไม่ใช้ ZEROFILL
หรือ
2. ใน PHP ใช้ ltrim() เพื่อตัด 0 ก่อนบันทึก
|
|
|
|
|
Date :
2023-06-27 11:16:38 |
By :
009 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณค่ะ สามารถแก้ไขได้แล้วค่ะ
|
|
|
|
|
Date :
2023-06-29 09:34:35 |
By :
mookmixxwipwap |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 03
|