我有一个包含这些数据的csv文件:
SUBJ_CODE描述时间天数单位 EC-26765 FORLAN EL 9:30 - 10:30 MWF 203 3 EC-26773 IT 421 L 11:30 - 12:30 MWF CISC 1 EC-26781 IT 421 5:01 - 6:31 PM TTH CISC 2 EC-26823 IT 420 8:30 - 11:30 AM SAT MA1 3 EC-26807 IT 423 3:00 - 4:30 PM TTH M1 3 EC-26799 IT 422 8:01 - 9:01 PM TTH 211 2 EC-26815 IT 422 L 6:31 - 8:01 PM TTH C1 1 ED-08326 FIL 38 7:30 - 9:00 TTH EDTE 3 IS-24331 MATH 16 12:00 - 1:30 PM TTH 203 3
我想将此csv文件保存到我的phpadmin中,如下所示: ID_NO SUBJ_CODE描述时间天数单位 08329 EC-26765 FORLAN EL 9:30 - 10:30 MWF 203 3 08329 EC-26773 IT 421 L 11:30 - 12:30 MWF CISC 1 08329 EC-26781 IT 421 5:01 - 6:31 PM TTH CISC 2 08329 EC-26823 IT 420 8:30 - 11:30 AM SAT MA1 3 08329 EC-26807 IT 423 3:00 - 4:30 PM TTH M1 3 08329 EC-26799 IT 422 8:01 - 9:01 PM TTH 211 2 08329 EC-26815 IT 422 L 6:31 - 8:01 PM TTH C1 1 0832 ED-08326 FIL 38 7:30 - 9:00 TTH EDTE 3 08329 IS-24331 MATH 16 12:00 - 1:30 PM TTH 203 3
这是我的代码:
if(isset($_POST['submit']))
{
if($_FILES["file"]["type"] != "application/vnd.ms-excel")
{
echo "This is not a CSV file.";
}
elseif(is_uploaded_file($_FILES['file']['tmp_name'])){
//Process the CSV file
$handle = fopen($_FILES['file']['tmp_name'], "r");
$data = fgetcsv($handle, 10000, ","); //Remove if CSV file does not have column headings
while (($data = fgetcsv($handle, 10000, ",")) !== FALSE)
{
//$att = $myrow['STUD_ID'];
$att0 = mysql_real_escape_string($data[0]);
$att1 = mysql_real_escape_string($data[1]);
$att2 = mysql_real_escape_string($data[2]);
$att3 = mysql_real_escape_string($data[3]);
$att4 = mysql_real_escape_string($data[4]);
$att5 = mysql_real_escape_string($data[5]);
$sql = "INSERT INTO schedule (IDNO,SUBJ_CODE,DESCRIPTION,TIME,DAYS,ROOM,UNITS)
VALUES ('$att','$att0','$att1','$att2','$att3','$att4','$att5')";
mysql_query($sql);
}
mysql_close($evancon);
echo "<script>alert('CSV file successfully imported.');</script>";
}
else{
die("You shouldn't be here");
}
}
?>
希望你能回答这个问题....谢谢
答案 0 :(得分:0)
我假设ID_NO
列是主键,因此您不能重复。根据您的示例数据的外观,您可以创建来自ID_NO
和SUBJ_CODE
的复合主键,这将是唯一的。