I already have a php file that catches data from an uploaded CSV file to MySQL but the problem is, every time I upload the csv file, it duplicates the data into the MySQL

What i really want is, if I uploaded the first csv file, and edit some data on the same file, it will UPDATE the MySQL data. But if there's a new entry it will ofcourse add..

<?php

$conn = mysql_connect("localhost", "root", "") or die (mysql_error());
mysql_select_db("sbhs",$conn);

if(isset($_POST['submit']))
{
    $file = $_FILES['file']['tmp_name'];

    $handle = fopen($file,"r");

    while(($fileop = fgetcsv($handle,1000, ",")) !== false )
    {
        $firstname = $fileop[0];
        $lastname = $fileop[1];
        $email = $fileop[2];

        $sql = mysql_query("INSERT INTO test (first_name,last_name,email) VALUES ('$firstname','$lastname','$email') ");

    }

    if($sql)
        echo "Uploaded Successfully";
}

?>

<html>
<head>
</head>

<body>
<div id="mainWrapper">
    <form method="post" action="test.php" enctype="multipart/form-data">
        <input type="file" name="file"/>
        <br/>
        <input type="submit" name="submit" value="submit"/>
    </form>
</div>
</body>

</html>

PLEASE I REALLY NEED THIS

By the way, anyone who has a GRADING SYSTEM using PHP?

thanks

Recommended Answers

All 2 Replies

What i really want is, if I uploaded the first csv file, and edit some data on the same file, it will UPDATE the MySQL data. But if there's a new entry it will ofcourse add.

How would you recognize the difference between a new entry and an existing one?

You could try with INSERT ... ON DUPLICATE KEY UPDATE provided that you have a unique index or primary key defined (you could use email for that or first_name+last_name).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.