0

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

?>
Drew
  • 24,851
  • 10
  • 43
  • 78
eddstyson
  • 25
  • 7
  • http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – Ryan Oct 18 '13 at 05:31

3 Answers3

1

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.";
}
DreadPirateShawn
  • 8,164
  • 4
  • 49
  • 71
  • the page has gone..white? – eddstyson Oct 18 '13 at 06:07
  • Hard to say -- I've updated my example code with some better debug output. It's often wise to make sure that every possible code path results in visible output clearly noting which path was followed, until you've got it working reliably. – DreadPirateShawn Oct 18 '13 at 06:18
  • perfect..the page now loads..now i am uplodin excelcsv but it returns Submission was not found. ...may you solve? – eddstyson Oct 18 '13 at 06:38
  • At this point, your best bet is probably to Google "php post submit is not set" or a variation of the same, and spend some time debugging. Such as asking the question, "is $_POST set at all?" The PHP $_POST docs (http://php.net/manual/en/reserved.variables.post.php) include a comment with `if (!empty($_POST))`, that would be useful to check. Things like that. – DreadPirateShawn Oct 18 '13 at 06:42
  • thanks alot.. i have never chat with a guru.. all this took me 2weeks but you secs am now checking the link..or maybe its the soccerer .r joomla 2.5.11 error..i feel helpless coz cant jus upload csv into the mysql..could you know any software that upload csv into mysql through joomla.. – eddstyson Oct 18 '13 at 06:56
  • perfect this works....but when i click upload without csv it takes too long,,,how can i cure that? – eddstyson Oct 20 '13 at 04:23
  • You should probably select an answer to this question in order to wrap it up, then post your new question, in order to get fresh attention. Otherwise you're reducing your audience from "all of StackOverflow" down to pretty much just me, not to mention losing clarity as the question focus drifts. In your new question, I'd advise including your current code, as well as details on how long "too long" is, compared to how long it takes when you do upload a csv, perhaps the timing for both a small csv and a large csv for reference. – DreadPirateShawn Oct 20 '13 at 09:07
0

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')");
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
  • For what it's worth, you added the closing parenthesis outside of the string but also removed the closing parenthesis from the "values()" section of the query, which simply shifts the bug. – DreadPirateShawn Oct 18 '13 at 05:25
  • Notice: Undefined variable: sql in C:\xampp\htdocs\csv\plugins\system\sourcerer\helper.php(507) : runtime-created function on line 22 – eddstyson Oct 18 '13 at 05:37
  • That could be a scoping issue -- I've updated my answer to suggest a solution for that as well. – DreadPirateShawn Oct 18 '13 at 05:48
0

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')");
Darren
  • 13,050
  • 4
  • 41
  • 79