无法使用INSERT INTO写入数据库

I have written a form with server side validation using php and now my aim is to insert all the input's from my form into my database (which already has its tables). Below is my syntax:

//Example of one of my validations (for postcode input)

if (empty($_POST["postcode"])) {
 $postcodeErr = "";
   } else {
 $postcode = test_input($_POST["postcode"]);
    if(!preg_match("/^[0-9]*$/", $postcode)) { 
    $postcodeErr = "Only numeric characters";      
  }
else if (strlen($postcode) != 4) {
 $postcodeErr = "Must be 4 digits in length";
   }
  }
 }  

//Connect to database server

$conn = mysql_connect("localhost", "-----", "------"); 
mysql_select_db("-------", $conn) 
or die ('Database not found ' . mysql_error() ); 

// The SQL statement is built

$sql = "INSERT INTO Customer (name, address, suburb, state, postcode)
        VALUES ('$_POST[name]', '$_POST[address]', '$_POST[suburb]', '$_POST[$state]', '$_POST[postcode]')";

    if (!mysql_query($sql,$conn))
    {
    die('Error: ' . mysql_error());
    }
    echo "1 record added";

    mysql_close($conn)


function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}
?> //end of my php tag

When I run my form, I get a parse error saying that I have an unexpected T_FUNCTION. I know there is a lot above (tried to make it as simple as I can) but I can't seem to word around fixing the error and if I do, I just get another error. Am I writing the code correctly? Normally it's best when other people look at your work. Help will be much appreciated!

The quotes for $_POST['name'] and all other variables was missing in the post variable.

Try with

$name=$_POST['name'];
$address=$_POST['address'];
$suburb=$_POST['suburb'];
$state=$_POST['$state'];
$postcode=$_POST['postcode'];
$sql = "INSERT INTO Customer (name, Address, suburb, state, postcode)
        VALUES ('$name', '$address', '$suburb', '$state', '$postcode')";

Agreed with Fred, there seems to be an extra ending brace just above //Connect to database server which is breaking the code.

If that doesn't fix it, please copy/paste your full error message.

EDIT:

else if (strlen($postcode) != 4) {

needs to be

} else if (strlen($postcode) != 4) {

And there are two extra braces at the end of that if statement (just above the //Connect to database server)

mysql_close($conn) Needs to have a ; after it...

That's why the function after it is unexpected

you also have one extra brace above database connection, use mysqli prepared statements for better security.

$db = new mysqli('localhost', 'root', '', 'database');
if ($db->connect_errno) {
echo "failed to connect to the database"; die();
}

$name=$_POST['name'];
$address=$_POST['address'];
$suburb=$_POST['suburb'];
$state=$_POST['$state'];
$postcode=$_POST['postcode'];

$stmt = $db->prepare("insert into `Customer` (name, Address, suburb, state, postcode) VALUES (?,?,?,?,?)";
$stmt->bind_param('sssss', $name, $address, $suburb, $state, $postcode);
$stmt->execute();
echo $stmt->affected_rows."record added";