eddstyson
eddstyson

Reputation: 25

upload csv comma delimited into mysql Parse error:

What could be wrong with my code?..Instead I get..Parse error: syntax error, unexpected ';'

here is my sample code pls may you assist me

<form enctype="multipart/form-data" method="POST" action="" > 
    <input type="file" name="file" /><br />
    <input type="submit" value="upload csv" name"submit"/>
</form>

<?php

    $connection = mysql_connect("localhost","root","")
    or die ("Couldn't connect to server");

    $db = mysql_select_db("demo", $connection)
    or die ("Couldn't select database");

    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 "Hello from PHP.";
    }
    }

?>

Upvotes: 0

Views: 114

Answers (3)

DreadPirateShawn
DreadPirateShawn

Reputation: 8412

You've got an extra brace in your PHP:

if($sql)
{
    echo "Hello from PHP.";
}
} // remove this

Also, your sql query is missing a closing parenthesis, and should be:

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

The syntax error that you received was due to the sql query issue in particular -- it was expected a closing parenthesis, but found a semicolon instead.

You should also consider moving the if ($sql) logic into the if (isset($_POST['submit'])) block, since it can only occur within that scope anyway. That might also resolve the "undefined variable" issue that you noted in a comment on Shankar's answer.

One more note... it looks like your while loop is only looping through the $firstname = $fileop[0]; line, since you don't have braces around the rest of the logic. Do you intend something like this?

if(isset($_POST['submit']))
{
    $file = $_FILES['file']['tmp_name'];
    $handle = fopen($file,"r");

    $processed = false;
    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')");

        $processed = true;
    }

    if($processed)
    {
        echo "CSV was processed.";
    }
    else
    {
        echo "CSV was not processed.";
    }
}
else
{
    echo "Submission was not found.";
}

Upvotes: 1

There is an extra brace here

if($sql)

{
    echo "Hello from PHP.";

} // here..... remove it
}

and change your mysql_query statement to

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

Upvotes: 0

Darren
Darren

Reputation: 13128

Your issue lies here:

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

This hasn't been closed off properly. It should look like this ( you missed the closing bracket):

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

Upvotes: 0

Related Questions