0

I am currently working on a PHP script to upload an image to a mysql database and I am getting a syntax error on line 13 of my code that states Unexpected T_IF. I am relatively new to PHP and I am struggling to work out what is causing it. The code is as follows;

    <head>
        <title>File Upload To Database</title>
    </head>
    <body>
        <form action="index.php" method="POST" enctype="multipart/form-data">
            <input type="file" name="image"><Input type="submit" name="submit" value="Upload">
        </form>

        <?php>
        if(isset($_POST['submit']))
            {
                mysql_connect("localhost","appuser1","******");
                mysql_select_db("PHP_Sig_test_app");

                $imageName = mysql_real_escape_string($FILES["image"]["name"]);
                $imageData = mysql_real_escape_string(file_get_contents($_FILES["image"]["tmp_name"]));
                $imageType = mysql_real_escape_string($FILES["image"]["type""]);

                if(substr($imageType,0,5) == "image)
                {
                    mysql_query("INSERT INTO 'Data' Values('','$imageName','$imageData')";
                    echo "image uploaded";
                }
                else
                {
                    echo "Only Images can be Inserted"
                }    
            }
        ?>
    </body>
</html>

Thanks

  • There's an extra quote `$imageType = mysql_real_escape_string($FILES["image"]["type""]);` – nilobarp Dec 05 '14 at 10:46
  • Just look closely at how SO highlighted your code. Also, it's a good idea to use an editor that highlights syntax and underlines errors (there are plenty of options). – georg Dec 05 '14 at 10:52
  • Please stop using the ***Deprecated*** `mysql` extension, it's no longer supported, it's going to be removed, and is replaced with either one of 2 extensions: `PDO` and `mysqli` (the `i` stands for _improved_). PS: syntax errors (and other simple typographical errors) are deemed off-topic on SO – Elias Van Ootegem Dec 05 '14 at 10:55

3 Answers3

2

<?php> should be <?php

Thats causing the syntax error

exussum
  • 18,275
  • 8
  • 32
  • 65
2

Remove the closing bracket in this line:

<?php>

This starts a new command with the > sign (which makes of cause no sense) so > if will fail and this is the reason for your bug.

Your second if statment also has a missing quote.

if(substr($imageType,0,5) == "image)

should be

if(substr($imageType,0,5) == "image")
rekire
  • 47,260
  • 30
  • 167
  • 264
  • This seems to have worked, but the problem I am having now is that the HTML form section is not even being displayed in the browser, any ideas as to why? – Alex Walker-Ingham Dec 05 '14 at 11:10
2

Have some syntax errors -

<?php //removed >

    if(isset($_POST['submit']))
        {
            mysql_connect("localhost","appuser1","weeble180276");
            mysql_select_db("PHP_Sig_test_app");

            $imageName = mysql_real_escape_string($FILES["image"]["name"]);
            $imageData = mysql_real_escape_string(file_get_contents($_FILES["image"]["tmp_name"]));
            $imageType = mysql_real_escape_string($FILES["image"]["type"]); //removed "

            if(substr($imageType,0,5) == "image") //added "
            {
                mysql_query("INSERT INTO 'Data' Values('','$imageName','$imageData')";
                echo "image uploaded";
            }
            else
            {
                echo "Only Images can be Inserted"; // added ;
            }
        }
    ?>
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87