Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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

share|improve this question
    
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). – LorDisturbia Nov 15 '13 at 16:39
    
so, check yourself by enabling errors at the top of your code. error_reporting(E_ALL);ini_set("display_errors", "on"); – Asenar Nov 15 '13 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. – Styphon Nov 15 '13 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 – LorDisturbia Nov 15 '13 at 16:44
    
Did you checked in MysqlClass file? – Krish R Nov 15 '13 at 17:15
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

share|improve this answer
    
I had messed up the code while copy-pasting it here on StackOverflow. Now it shows the actual code. – LorDisturbia Nov 15 '13 at 16:37

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 .

share|improve this answer
    
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 – LorDisturbia Nov 15 '13 at 16:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.