我嘗試上傳一個Excel工作表(CSV)到我的數據庫在PHP,但它表明成功。但是,我去mysql確認,我注意到只有Excel表中的最後一行顯示在MySQL表中。請需要幫助。我不知道還有什麼要做。上傳Excel工作表在PHP到數據庫只給最後一行
<?php
include ("base.php");
if (isset($_POST['submit'])) {
//inserting into db
//getting the file and inserting it in db
$file = $_FILES['file']['tmp_name'];
$handle = fopen($file, "r");
$tot = 0;
while(($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
for ($c=0; $c < 1; $c++) {
//only run if the first column if not equal to name
if ($data[0] != 'name') {
$name = mysqli_real_escape_string($con, $data[0]);
$reg_no = mysqli_real_escape_string($con, $data[1]);
$lab = mysqli_real_escape_string($con, $data[2]);
$test = mysqli_real_escape_string($con, $data[3]);
$exam = mysqli_real_escape_string($con, $data[4]);
$total = mysqli_real_escape_string($con, $data[5]);
$grade = mysqli_real_escape_string($con, $data[6]);
$remark = mysqli_real_escape_string($con, $data[7]);
//inserting into db
$sql = "INSERT INTO imt303 (name, reg_no, lab, test, exam, total, grade, remark)
VALUES ('$name', '$reg_no', '$lab', '$test', '$exam', '$total', '$grade', '$remark')";
}
//incrementing
$tot++;
}
}
if (mysqli_query($con, $sql)) {
echo '<script type="text/javascript">alert("Uploaded Successfully!");</script>';
} else {
echo '<script type="text/javascript">alert("There is a problem uploading file!");</script>';
}
}
?>
<html>
<form method="POST" action="test.php" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="submit" value="Submit">
</form>
</html>
你可以使用加載數據infile。 – e4c5
你是否明白在每個'while'迭代中你__overwrite__'$ sql'? –
'mysqli_query($ con,$ sql)'應該在循環體中 – cske