0

Error log is throwing following error: Parse error: syntax error, unexpected ';' in line 11

Any idea why I'm getting this error ? As far as my knowledge of PHP goes all PHP statements ends with semicolon. What am I missing here ?

Providing full code as requested:

<?php

if(isset($_POST['submit'])) {
    $firstName = $middleName = $lastName = $address = $mobileNumber = "";
    $password = $email = $result = "";      


    test_input($data) {

    $data = stripcslashes($data);     // <-- this is where error occurs
    $data = htmlspecialchars($data);
    $data = htmlentities($data);

    return $data;
    }

    $link = mysqli_connect("localhost","root","ilovemyself","eshop");
    if(!$link) {
        echo "Could not connect to the database !";
    } else {

        $firstName = test_input($_POST['firstName']);

    ...
    }
 }
Martin
  • 22,212
  • 11
  • 70
  • 132
  • kindly share the entire code to understand the error. – Milan Chheda Jul 12 '17 at 10:27
  • 8
    `test_input($data) {` is not valid, unless there's a "function" keyword before it? – Niet the Dark Absol Jul 12 '17 at 10:29
  • You should have `test_input` outside of the `if` loop. – Milan Chheda Jul 12 '17 at 10:31
  • @Niet You're right dammit ... I completely left out the function keyword. Kinda misleading though. Why it complains about semicolon ? –  Jul 12 '17 at 10:32
  • @MarkyMark Because it tries to evaluate `test_input($data)` as function **call** instead of a **definition** which would only be valid like `test_input($data);` if it finds further code. – Xatenev Jul 12 '17 at 10:33
  • @Xatenev Thank you for explanation ! –  Jul 12 '17 at 10:36
  • @MilanChheda 1. `if` is no `loop` 2. It doesn't have to be outside of the `if` – Xatenev Jul 12 '17 at 10:37
  • Mark, do you realise having `htmlspecialchars` immediately followed by `htmlentities` will ruin your HTML code; because `"` becomes `"` *then* becomes `&quot;` which will not be understood by your HTML parser. Remove one of those two functions. – Martin Jul 12 '17 at 10:38
  • @Martin Wow you're so right mate ! I just checked it on w3schools.com and they recommend using: `function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; }` –  Jul 12 '17 at 10:46
  • @Martin How can I upvote your suggestion ? It's really good and I just wanna appreciate it :) I was going to sabotage my website without knowing. –  Jul 12 '17 at 10:52
  • You can tick the up arrow next to my comment. I would suggest **not** using W3fools and instead using the [PHP Manual](http://php.net/manual/en/function.htmlentities.php) or the [Mozilla Developers Network (MDN)](https://developer.mozilla.org/en-US/docs/Glossary/PHP) – Martin Jul 12 '17 at 10:55
  • @Martin I don't see any arrows next to comments. I see arrows only next to Answers but not next to comments. Odd hm ... –  Jul 12 '17 at 11:00
  • Then you may not have enough reputation points to do that, yet – Martin Jul 12 '17 at 12:16

1 Answers1

1

Basically you are defining function in the wrong way.

Just replace your "test_input" block with this one:

    function test_input($data) {
        $data = stripcslashes($data);
        $data = htmlspecialchars($data);
        $data = htmlentities($data);
        return $data;
    }
Amrinder Singh
  • 5,300
  • 12
  • 46
  • 88
  • Yep. Already fixed it. You're sharing credit for a correct answer with @Niet The Dark Absol. –  Jul 12 '17 at 10:38
  • 1
    I have no idea why you rollbacked your version, even tho @NiettheDarkAbsol fixed your answer according to MCVE guidelines: https://stackoverflow.com/help/mcve – Xatenev Jul 12 '17 at 10:38
  • Ah! i did it by mistake, have rolled back again :) – Amrinder Singh Jul 12 '17 at 10:45