Jump to content

upload page with data validation fields


lindaonline15

Recommended Posts

hello all, im trying to make an upload page. the upload page itself works very well but, I'm applying data validation now, and some problems causes...

the validation will validate the fields, if empty or incorrect data input, will show the form again with red color fields.

the error currently im getting is this:

Parse error: syntax error, unexpected '}' in C:\wamp\www\upload-functions.php on line 82

 

I've signed below where is line 82.

I dont know why I get this error since everything seems to be clear and curly brakets are well.

 

 

<html>

<head>

<title>upload</title></head>

<body>

 

<?

function error_bool($error, $field) {

if($error[$field]) {

print("<td style=color:red>");

}

else {

print("<td>");

}

}

 

function show_form() {

global $print_again, $error;

?>

 

<form name="upload" method="post" enctype="multipart/form-data">

<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">

 

<tr>

<td>

 

<?php error_bool($error, "subject_code"); ?> Subject Code: </td><td><input type="text" name="subject_code" id="subject_code" value="<?php echo $_POST["subject_code"]; ?>">

</td></tr><tr><td>

 

<?php error_bool($error, "subject_name"); ?>Subject Name: </td><td><input type="text" name="subject_name" id="subject_name" value="<?php echo $_POST["subject_name"]; ?>">

</td></tr><tr><td>

 

<?php error_bool($error, "lecturer"); ?>Lecturer: </td><td> <input type="text" name="lecturer" id="lecturer" value="<?php echo $_POST["lecturer"]; ?>">

</td></tr><tr><td>

 

Department: </td><td>

<select name= "department" id="department" compulsory="yes">

<option value = "" SELECTED>Select

<option value = "IS">Information System

<option value = "GM">Graphics & Multimedia

<option value = "SN">Systems Networking

<option value = "SE">Software Engineering

</select>

 

</td></tr><tr><td>

 

Year of exam: </td><td>

<select name= "year" id="year" compulsory="yes">

<option value = "" SELECTED>Select

<option value = "2004">2005-06

<option value = "2005">2005-06

<option value = "2006">2006-07

<option value = "2007">2007-08

<option value = "2008">2008-09

 

</select>

</td></tr><tr><td>

 

Semester: </td><td>

<select name= "semester" id="semester" compulsory="yes">

<option value = "" SELECTED>Select

<option value = "1">One

<option value = "2">Two

<option value = "special">Special

</select>

</td>

 

</tr><tr><td><input type="hidden" name="MAX_FILE_SIZE" value="2000000">

<input name="userfile" type="file" id="userfile">

</td>

<td><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>

 

77 </tr>

78 </table>

79 </form>

80

81 <?php

82 }

83 if(isset($_POST["Submit"])) {

84 check_form();

 

} else {

show_form();

}

 

 

extract($_POST);

 

function subject_code($subject_code)

{

if(!preg_match("/[^a-zA-Z0-9]+$/s",$subject_code))

return TRUE;

else

return FALSE;

}

 

function subject_name($subject_name)

{

if(!preg_match("/[^a-zA-Z\ ]+$/s",$subject_name))

return TRUE;

else

return FALSE;

}

 

function lecturer($lecturer)

{

if(!preg_match("/[^a-zA-Z\ ]+$/s",$lecturer))

return TRUE;

else

return FALSE;

}

 

function check_form()

{

global $error, $print_again;

$error['subject_code'] = false;

$error['subject_name'] = false;

$error['lecturer'] = false;

$error['department'] = false;

$error['semester'] = false;

$error['year'] = false;

 

if($_POST["subject_code"]=="") {

$error['subject_code'] = true;

$print_again = true;

$message="The subject code field is empty<br>";

}

 

if($_POST["subject_name"]=="") {

$error['subject_name'] = true;

$print_again = true;

$message="The subject name field is empty<br>";

}

 

if($_POST["lecturer"]=="") {

$error['lecturer'] = true;

$print_again = true;

$message="The lecturer field is empty<br>";

}

 

if($_POST["department"]=="") {

$error['department'] = true;

$print_again = true;

$message="The department field is empty<br>";

}

 

if($_POST["semester"]=="") {

$error['semester'] = true;

$print_again = true;

$message="The semester field is empty<br>";

}

 

if($_POST["year"]=="") {

$error['year'] = true;

$print_again = true;

$message="The year field is empty<br>";

}

 

if(!subject_code($_POST['subject_code'])) {

$error['subject_code'] = true;

$print_again = true;

$message="Invalid subject code <br>";

}

 

if(!subject_name($_POST['subject_name'])) {

$error['subject_name'] = true;

$print_again = true;

$message="Invalid name <br>";

}

 

if(!lecturer($_POST['lecturer'])) {

$error['lecturer'] = true;

$print_again = true;

$message="Invalid lecturer <br>";

}

if($print_again) {

show_form();

 

} else {

show_form();

 

include_once "connection.php";

 

$uploadDir = 'C:/wamp/www/uploads/';

 

if(isset($_POST['upload']))

{

 

$fileName = $_REQUEST[subject_name];

$tmpName = $_FILES['userfile']['tmp_name'];

$fileSize = $_FILES['userfile']['size'];

$fileType = $_FILES['userfile']['type'];

 

// get the file extension first

$ext = ".pdf";

 

// make the file name

$uniqueName = $_REQUEST[subject_code] ."-". $_REQUEST[year] ."-". $_REQUEST[semester];

 

//the unique file name for the upload file

$filePath = $uploadDir . $uniqueName . $ext;

 

$result = move_uploaded_file($tmpName, $filePath);

if (!$result) {

echo "<br>Error uploading file";

exit

}

 

if(!get_magic_quotes_gpc())

{

$fileName = addslashes($fileName);

$filePath = addslashes($filePath);

}

 

$query = "INSERT INTO exam_papers (subject_code, subject_name, lecturer, department, semester, year_of_exam, file_path) ".

"VALUES ('$_REQUEST[subject_code]','$fileName','$_REQUEST[lecturer]','$_REQUEST[department]','$_REQUEST[semester]','$_REQUEST[year]', '$filePath')";

 

mysql_query($query) or die('<br>Error, query failed : ' . mysql_error());

 

echo

"

You have uploaded 1 exam paper with following information:<p>

Subject Code: $subject_code<br>

Subject Name: $subject_name<br>

Lecturer: $lecturer<br>

Department: $department<br>

Semester: $semester<br>

Year Of Exam: $year<br>

Location: $filePath<br>

";

 

}

 

$message=" You have uploaded 1 exam paper with following information:<p>

Subject Code: $subject_code<br>

Subject Name: $subject_name<br>

Lecturer: $lecturer<br>

Department: $department<br>

Semester: $semester<br>

Year Of Exam: $year<br>

Location: $filePath<br>";

}

echo "$message";

}

 

 

?>

 

</body>

</html>

Link to comment
https://forums.phpfreaks.com/topic/123593-upload-page-with-data-validation-fields/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.