0

Beginner in php here. I have programmed a form in an html format and then included some php code to direct the form submission to process.php file. The code in process.php will then insert the form submission into MySQL.

The first time I ran the code, it worked perfectly fine. However, I have no idea what happened later as subsequent attempts to run the code results in failure.

After i submit the form, it begins directing me to a blank process.php file. I tried check my code for errors but everything seems to be the same. The error it gives me is:

Parse error: syntax error, unexpected end of file in D:\XAMPP\htdocs\inthoughts\process.php on line 31

I did a search online and realize that the error usually comes from some minor error in the code where i didn't include a ; or placed the { in a wrong place. But when i looked into the code, it seems fine, i went through line by line and couldn't find any error.

May I know what is wrong? Thank you.

Below is the form coding for index.php:

<div class="jquery"/>
  <form method="post" action="process.php"/>
    <ul class="lists">
    <li class="title">Personal Details</li>
    <li class="fields">
        <label>Your Name:</label><br>
        <input type="text" name="name" /><br>
        <label>Contact Number:</label><br>
        <input type="text" name="contact"/><br>
        <label>Email:</label><br>
        <input type="text" name="email" /><br>
        <label>Address:</label><br>
        <input type="text" name="address" /><br>
    </li>
    <li class="title" id="student">Student Details<span style="color:red" id="fontred">(Click Me To Expand!)</span></li>
    <li class="fields">
        <label>Student Name:</label><br />
        <input type="text" name="studentname"/><br />
        <label>Student Level:</label><br />
        <input type="text" name="level"/><br />
        <label>Subject:</label><br />
        <input type="text" name="subject"/><br />
        <label>Student Stream:</label><br />
        <input name="stream" type="text" /><br />
    </li>
</ul>
<div class="submit1">
<input type="submit" value="Find me a tutor now!" name="submit1" />
</div>
</form>

Here is the code for process.php:

<?php
include('database.php');

if(isset($_POST['submit1'])){
$name=mysqli_real_escape_string($con,$_POST['name']);
$contact=mysqli_real_escape_string($con,$_POST['contact']);
$email=mysqli_real_escape_string($con,$_POST['email']);
$address=mysqli_real_escape_string($con,$_POST['address']);
$studentname=mysqli_real_escape_string($con,$_POST['studentname']);
$level=mysqli_real_escape_string($con,$_POST['level']);
$subject=mysqli_real_escape_string($con,$_POST['subject']);
$stream=mysqli_real_escape_string($con,$_POST['stream']);

if(!isset($name) || $name=='' || !isset($contact) || $contact=='' || !isset($email) || $email==''
    || !isset($address) || $address==''|| !isset($studentname) || $studentname==''|| !isset($level) || $level==''|| !isset($subject) 
    || $subject==''|| !isset($stream) || $stream==''
){
    $error="Please fill in all required fields before submission.";
    header("Location:index.php?error=".urlencode($error));
    exit(); 
}else{
    $sql = "INSERT INTO assignments(name,contact,email,address,studentname,level,subject,stream) VALUES('$name','$contact','$email','$address','$studentname','$level','$subject','$stream')";      
}

if(!mysqli_query($con,$sql)){
    die("Error".mysqli_error($con));
}else{
    header('Location:index.php?status=thanks');
    exit();
}

I have edited it and included a } at the end but it still doesn't work. The new php code for process.php now is:

<?php
include('database.php');

if(isset($_POST['submit1'])){
$name=mysqli_real_escape_string($con,$_POST['name']);
$contact=mysqli_real_escape_string($con,$_POST['contact']);
$email=mysqli_real_escape_string($con,$_POST['email']);
$address=mysqli_real_escape_string($con,$_POST['address']);
$studentname=mysqli_real_escape_string($con,$_POST['studentname']);
$level=mysqli_real_escape_string($con,$_POST['level']);
$subject=mysqli_real_escape_string($con,$_POST['subject']);
$stream=mysqli_real_escape_string($con,$_POST['stream']);

if(!isset($name) || $name=='' || !isset($contact) || $contact=='' || !isset($email) || $email==''
    || !isset($address) || $address==''|| !isset($studentname) || $studentname==''|| !isset($level) || $level==''|| !isset($subject) 
    || $subject==''|| !isset($stream) || $stream==''
){
    $error="Please fill in all required fields before submission.";
    header("Location:index.php?error=".urlencode($error));
    exit(); 
}else{
    $sql = "INSERT INTO assignments(name,contact,email,address,studentname,level,subject,stream) VALUES('$name','$contact','$email','$address','$studentname','$level','$subject','$stream')";      
}

if(!mysqli_query($con,$sql)){
    die("Error".mysqli_error($con));
}else{
    header('Location:index.php?status=thanks');
    exit();
}
gfcoding
  • 17
  • 5
  • possible duplicate of [Parse Error: syntax error: unexpected '{'](http://stackoverflow.com/questions/7059753/parse-error-syntax-error-unexpected) – Saty Jun 27 '15 at 06:52
  • code still doesnt work after adding curly brackets on godaddy hosting, but it works on localhost. Called godaddy tech support and they verified that the php connection to sql is working fine – gfcoding Jun 27 '15 at 07:54

2 Answers2

1

You should have an indention so it would be clearer for you to see which are starting and ending brackets. As pointed out in the comment you miss the ending bracket.

This should be your code with the ending bracket inserted:

<?php
include('database.php');

if(isset($_POST['submit1'])) {
  $name=mysqli_real_escape_string($con,$_POST['name']);
  $contact=mysqli_real_escape_string($con,$_POST['contact']);
  $email=mysqli_real_escape_string($con,$_POST['email']);
  $address=mysqli_real_escape_string($con,$_POST['address']);
  $studentname=mysqli_real_escape_string($con,$_POST['studentname']);
  $level=mysqli_real_escape_string($con,$_POST['level']);
  $subject=mysqli_real_escape_string($con,$_POST['subject']);
  $stream=mysqli_real_escape_string($con,$_POST['stream']);

  if(!isset($name) || $name=='' || !isset($contact) || $contact=='' || !isset($email) || $email==''
      || !isset($address) || $address==''|| !isset($studentname) || $studentname==''|| !isset($level) || $level==''|| !isset($subject) 
      || $subject==''|| !isset($stream) || $stream==''
  ) {
      $error="Please fill in all required fields before submission.";
      header("Location:index.php?error=".urlencode($error));
      exit(); 
  } else{
      $sql = "INSERT INTO assignments(name,contact,email,address,studentname,level,subject,stream) VALUES('$name','$contact','$email','$address','$studentname','$level','$subject','$stream')";      
  }

  if(!mysqli_query($con,$sql)){
      die("Error".mysqli_error($con));
  } else{
      header('Location:index.php?status=thanks');
      exit();
  }
} // missing bracket
Robin Carlo Catacutan
  • 13,249
  • 11
  • 52
  • 85
  • Hi, tried adding the `}` but it still doesnt work? I have no idea what is wrong? – gfcoding Jun 27 '15 at 07:13
  • @gfcoding Where did you add the `}` ? Seems in your updated code there still isn't. – Robin Carlo Catacutan Jun 27 '15 at 08:42
  • i did changed it in process.php. But that wasn't the problem, i tried changing the host_name from "localhost" to my website domain in database.php, it didn't work, but when i changed it back to "localhost" again, the code magically worked and now i am able to submit the form normally? Some bug in Godaddy maybe? But nevertheless problem solved..thx alot for your help... – gfcoding Jun 27 '15 at 08:52
0

The first expression has no closing }. It pays to format your code, like"

<?php
include('database.php');

if(isset($_POST['submit1'])){
    $name=mysqli_real_escape_string($con,$_POST['name']);
    $contact=mysqli_real_escape_string($con,$_POST['contact']);
    $email=mysqli_real_escape_string($con,$_POST['email']);
    $address=mysqli_real_escape_string($con,$_POST['address']);
    $studentname=mysqli_real_escape_string($con,$_POST['studentname']);
    $level=mysqli_real_escape_string($con,$_POST['level']);
    $subject=mysqli_real_escape_string($con,$_POST['subject']);
    $stream=mysqli_real_escape_string($con,$_POST['stream']);
    if(!isset($name) || $name=='' || !isset($contact) || $contact=='' || !isset($email) || $email=='' || !isset($address) || $address==''|| !isset($studentname) || $studentname==''|| !isset($level) || $level==''|| !isset($subject) || $subject==''|| !isset($stream) || $stream==''){
        $error="Please fill in all required fields before submission.";
        header("Location:index.php?error=".urlencode($error));
        exit();
    }else{
        $sql = "INSERT INTO assignments(name,contact,email,address,studentname,level,subject,stream) VALUES('$name','$contact','$email','$address','$studentname','$level','$subject','$stream')";
    }
} // end $_POST['submit1']
if(!mysqli_query($con,$sql)){
    die("Error".mysqli_error($con));
}else{
    header('Location:index.php?status=thanks');
    exit();
}
TimD
  • 462
  • 6
  • 18