0

尝试使用 csv 文件更新我的数据库调查中的表,但它给了我错误:

Duplicate entry '1 (Q1_1)' for key '1(Q_1)' .

该列名为“1(Q_1)”,csv文件中的标题为“1(Q1_1)”,在“1(Q_1)”下输入

<?php 

//connect to the database
$connect = mysqli_connect("localhost","root","");
mysqli_select_db("genydb",$connect); //select the table
//
if (isset($_FILES['csv'])){
if ($_FILES['csv']['size'] > 0) {

    //get the csv file
    $file = $_FILES["csv"]['tmp_name'];
    $handle = fopen($file,"r");

    //loop through the csv file and insert into database
  while ($data = fgetcsv($handle,0,",","'")){

mysql_query("UPDATE `survey` SET `Record_ID`='".addslashes($data[0])."', `Agreement`= '".addslashes($data[1])."',`1(Q_1)`= '".addslashes($data[2])."', `3 (Q4)`='".addslashes($data[3])."', `2 (Q3)`='".addslashes($data[4])."', `4 (Q5)`='".addslashes($data[5])."', `5 (Q6)`='".addslashes($data[6])."', `7 (Q8)`='".addslashes($data[7])."', `6 (Q7)`='".addslashes($data[8])."', `8 (Q9)`='".addslashes($data[9])."', `9 (Q10)`='".addslashes($data[10])."', `10 (Q11)`='".addslashes($data[11])."', `11 (Q12)`='".addslashes($data[12])."', `13 (Q14_1)`= '".addslashes($data[13])."'")or die(mysql_error()); 

    }
    //

    //redirect
    header('Location: imports.php?success=1'); die;

}

}

?>

<!DOCTYPE html>
<head>

<title>Import a CSV File with PHP & MySQL</title>
</head>

<body>

<?php if (!empty($_GET['success'])) { echo "<b>Your file has been imported.</b><br><br>"; } //generic success notice ?>

<form action="" method="post" enctype="multipart/form-data" ">
  Choose your file: <br />
  <input type="file"  name="csv" id="csv" />
  <input type="submit" name="Submit" value="Submit" />
</form>

</body> 
</html>
4

0 回答 0