-4

I have a problem with PHP and, although I spent a whole hour trying to find what's wrong, I can't find anything in the code.

I'm working with a MySQL database and an input form. The form should be used to add entries to a specific table in the database. However, it gives a "Parse error: syntax error, unexpected '}'" whenever I hit the "Submit" button.

I tried everything but nothing worked.

This is the code:

<?php
include 'config.php';
session_start();
if (!isset($_SESSION['login'])){
    header("Location: index.html");
}
if(isset($_POST['submit']) && ($_POST['submit']=="Invia")){
    if(isset($_POST['section'])){
        $section = addslashes(filter_var($_POST['section'], FILTER_SANITIZE_STRING));
    } else {
        echo "Devi selezionare una sezione";
    }
    if(isset($_POST['offer'])){
        $offer = addslashes(filter_var($_POST['offer'], FILTER_SANITIZE_STRING));
    }
    if(isset($_POST['title'])){
        $title = addslashes(filter_var($_POST['title'], FILTER_SANITIZE_STRING));
    } else {
        echo "Devi impostare il nome del viaggio";
    }
    if(isset($_POST['datestart'])){
        $datestart = date('Ymd', strtotime($_POST['datestart']));
    } else {
        echo "Devi selezionare una data di inizio";
    }
    if(isset($_POST['dateend'])){
        $dateend = date('Ymd', strtotime($_POST['dateend']));
    } else {
        echo "Devi selezionare una data di fine";
    }
    if(isset($_POST['itinerary'])){
        $itinerary = addslashes(filter_var($_POST['itinerary'], FILTER_SANITIZE_STRING));
    } else {
        echo "Devi inserire un itinerario";
    }
    if(isset($_POST['price'])){
        $price = addslashes(filter_var($_POST['price'], FILTER_SANITIZE_STRING));
    } else {
        echo "Devi inserire un prezzo";
    }
    if(isset($_POST['includes'])){
        $includes = addslashes(filter_var($_POST['includes'], FILTER_SANITIZE_STRING));
    } else {
        echo "Devi inserire La quota comprende!";
    }
    if(isset($_POST['pdfname'])){
        $pdfname = addslashes(filter_var($_POST['pdfname'], FILTER_SANITIZE_STRING));
    } else {
        echo "Devi inserire un nome per il pdf";
    }

    if(isset($_POST['section']) && isset($_POST['title']) && isset($_POST['datestart']) && isset($_POST['dateend']) && isset($_POST['itinerary']) && isset($_POST['price']) && isset($_POST['includes']) && isset($_POST['pdfname'])){
    $data = new MysqlClass();
    $data->connetti();
     
    $query = "INSERT INTO " . $section . " (
    `id` ,
    `title` ,
    `offer` ,
    `datestart` ,
    `dateend` ,
    `itinerary` ,
    `price` ,
    `includes` ,
    `pdfname`) 

    VALUES (
    NULL,
    '".$title."',
    '".$offer."',
    '".$datestart."',
    '".$dateend."',
    '".$itinerary."',
    '".$price."',
    '".$includes."',
    '".$pdfname."');";

    $data->query($query);
    echo "Viaggio inserito con successo.";
    $data->disconnetti();
    }
} else {
    header("Location: index.html");
}
?>

EDIT: I'm using Wamp as a local server. I'm starting to think that's the problem, although it seems unlikely.

EDIT 2: I tried switching to EasyPHP, but I still have the same problem: Parse error: syntax error, unexpected '}' in C:\Program Files (x86)\EasyPHP-DevServer-13.1VC11\data\localweb\input.php on line 50

6
  • Check again, I had messed up the code while copying it here. Now it shows as it should (and there is no else on line 37). Commented Nov 15, 2013 at 16:39
  • so, check yourself by enabling errors at the top of your code. error_reporting(E_ALL);ini_set("display_errors", "on"); Commented Nov 15, 2013 at 16:43
  • Just as a side note, if you're going to filter_var on an input, look at filter_input. Secondly, addslashes is not adequate protection when taking input and using it in a query. You should be using real_esacpe_string instead. Commented Nov 15, 2013 at 16:44
  • It could be due to the fact I'm using Wamp, but I keep seeing the same exact error: ( ! ) Parse error: syntax error, unexpected '}' in C:\Program Files\wamp\www\input.php on line 52 Commented Nov 15, 2013 at 16:44
  • Did you checked in MysqlClass file? Commented Nov 15, 2013 at 17:15

2 Answers 2

1
if(isset($_POST['price'])){
    else { //<<---- missing } before else
        echo "Devi inserire un prezzo";
    }

There is also an "extra" closing bracket at the end of the script

Sign up to request clarification or add additional context in comments.

1 Comment

I had messed up the code while copy-pasting it here on StackOverflow. Now it shows the actual code.
0

when you use if statement you should do like this

if(){

}else{


}

and as he told you above

if(isset($_POST['price'])){
    else { 
        echo "Devi inserire un prezzo";
    }

Thank You .

1 Comment

As I said above, the one yuo saw was not the actual code. I had messed it up while posting here. Please check the actual code! Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.