Forum Moderators: coopster

Message Too Old, No Replies

upload page with data validation fields

         

lindaonline15

6:33 pm on Sep 8, 2008 (gmt 0)

10+ Year Member



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>

eelixduppy

9:31 pm on Sep 8, 2008 (gmt 0)



Seems odd. Check the number of braces you have to see if they match. Wouldn't surprise me if it was something like this causing issues somewhere in the code.

lindaonline15

10:33 am on Sep 9, 2008 (gmt 0)

10+ Year Member



I've count it.. it exactly matches... so i can't undrestan where is the problem... should be with something else, right?

lindaonline15

4:08 pm on Sep 9, 2008 (gmt 0)

10+ Year Member



any ideas? anyone?

daveginorge

9:49 am on Sep 10, 2008 (gmt 0)

10+ Year Member



Hi all. I am a complete novice with php/MySql (Experienced programmer in other languages) and read theses groups as a way of my learning the languages, I also enjoy trying to solve the problems even when others have given the answer.

What I have seen here is that the lines:

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

} else {
show_form();
}

extract($_POST);

Actually sit outside any function. It is the only code that is not included in a function.

As I said I'm only a novice here but seems strange to me.

Maybe that helped. Hope so.

Dave

lindaonline15

11:31 am on Sep 10, 2008 (gmt 0)

10+ Year Member



actually, that if else doesnt need to be in a function. it is 2 differenct function calls... that if meets the condition, will call one function, if not, will call aother function...

its good to try;) im also novice anyway...

daveginorge

2:59 pm on Sep 10, 2008 (gmt 0)

10+ Year Member



Then like the others I'm stuck with ideas.

Good luck

lindaonline15

5:15 pm on Sep 10, 2008 (gmt 0)

10+ Year Member



thx...
anyone any help? :(

lindaonline15

12:42 pm on Sep 12, 2008 (gmt 0)

10+ Year Member



ok I got where was the error, I was using tag <? instead of <?php. I'd read somewhere before, that php sometimes cant handle this...
now, my problem is, the page comes with no error, but it does not work when I submit the data..

any suggestions?

eelixduppy

1:19 pm on Sep 12, 2008 (gmt 0)



Check your error log for something. You might be getting additional errors.

lindaonline15

1:36 pm on Sep 12, 2008 (gmt 0)

10+ Year Member



do you mean to check if all of them are ON or not? all errors are ON actually...

daveginorge

1:02 pm on Sep 13, 2008 (gmt 0)

10+ Year Member



Seems strange how the <? opening tag at line 1 of the script reflected the error down to line 82 passed another correct opening tag <?php at line 81. But that's life.

lindaonline15

1:49 pm on Sep 13, 2008 (gmt 0)

10+ Year Member



yes it is... and im still confused why the rest of the code does not work... :(

daveginorge

4:22 pm on Sep 13, 2008 (gmt 0)

10+ Year Member



What are the errors now being produced...?

lindaonline15

4:35 pm on Sep 13, 2008 (gmt 0)

10+ Year Member



the main problem here is there is not any error. the page just refreshesh to itself with no change and reaction.

i actually added this to my form:
action="<?php echo $_SERVER['PHP_SELF']; ?>"

before this I didnt have the action attribute
but still is the same..

daveginorge

4:41 pm on Sep 13, 2008 (gmt 0)

10+ Year Member



Just looking through your code again.
maybe just maybe you have forgotten the "action" from the <form> tag.

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

should be

<form name="upload" method="post" action="$_SERVER["PHP_SELF"]" enctype="multipart/form-data">.

Keep me posted please.
Dave

lindaonline15

5:03 pm on Sep 13, 2008 (gmt 0)

10+ Year Member



thanks dave for replies...

I did like this:
<form name="upload" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">

is it incorrect? should it be without PHP tags?

daveginorge

5:40 pm on Sep 13, 2008 (gmt 0)

10+ Year Member



It should be as I wrote it, it is a standard action for the <form> tag when you want to include the php script in the same page as the html

<form name="upload" method="post" action="$_SERVER["PHP_SELF"]" enctype="multipart/form-data">

Not using the <?php ?> tags

I have now loaded this onto my apache server on Fedora Core 8

Error log tells me you have a missing ; on the exit word I marked it with <--------

//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 <------- ;
}

After adding the "action" and the ; I get something in the web browser.

Good luck, please keep me posted.

Dave

lindaonline15

5:54 pm on Sep 13, 2008 (gmt 0)

10+ Year Member



ok, now im getting something, but its an error

Not Found

The requested URL /$_SERVER[ was not found on this server.

lindaonline15

12:06 pm on Sep 14, 2008 (gmt 0)

10+ Year Member



is this problem related to submittion method of the form?

daveginorge

5:41 pm on Sep 14, 2008 (gmt 0)

10+ Year Member



When a file is uploaded PHP puts it's information in the SUPERGLOBAL $_FILES

The code below works but it uses a separate php file to do the processing

uploadtest.html
<html>
<head></head>
<body>
<form name="upload" method="post" action="uploadscript.php" enctype="multipart/form-data">
<input name="userfile" type="file" id="userfile">
<input name="upload" type="submit" class="box" id="upload" value=" Upload ">
</body
</html>

uploadscript.php
<?php
$filename = $_FILES['userfile']['name'];
printf($filename);
?>

The error that you are getting on your main script is a "file not found" error. You need to look at your logic for file upoad.

Try creating a separate file test.php and get just the file upload working and then replace the code in the main file with the known good from the test file.

Also you need to be reading your error.log more all the information gets printed there..

If you have PHP installed on your local Windows PC, try using PHP Designer 2007 which is available free, just google for it, under the tools menu it has a DEBUG tool, I have never used the DEBUG as I run my PHP on a linux server.

Good luck
Dave