Forum Moderators: coopster
<html>
<head>
<title>Search in Final exam papers</title>
</head>
<body>
<?php
mysql_connect("localhost", "admin", "admin") or die(mysql_error());
mysql_select_db("ueros_db") or die(mysql_error());
$subject_code = empty($_POST['subject_code'])? die ("ERROR: Incorrect subject code") : mysql_escape_string($_POST['subject_code']);
$subject_name = empty($_POST['subject_name'])? die ("ERROR: Incorrect subject name") : mysql_escape_string($_POST['dubject_name']);
$lecturer = empty($_POST['lecturer'])? die ("ERROR: Incorrect lecturer name") : mysql_escape_string($_POST['lecturer']);
$department = empty($_POST['department'])? die ("ERROR: Incorrect department") : mysql_escape_string($_POST['department']);
$semester = empty($_POST['semester'])? die ("ERROR: Incorrect semester") : mysql_escape_string($_POST['semester']);
$year = empty($_POST['year'])? die ("ERROR: Incorrect year of exam") : mysql_escape_string($_POST['year']);
?>
<?php
$query = "SELECT * FROM exam_papers WHERE
(subject_code = '$subject_code'OR
subject_name = 'subject_name'OR
lecturer = '$lecturer'OR
department = '$department'OR
semester = '$semester'OR
year_of_exam = '$year')"
or die (mysql_error());
$result = mysql_query($query) or die (mysql_error());
$num=mysql_numrows($result);
?>
<table border="1">
<tr>
<td>Subject Code</td>
<td>Subject Name</td>
<td>Lecturer</td>
<td>Department</td>
<td>Semester</td>
<td>Year</td>
<td>File</td>
</tr>
<?php
$count = 0;
while ($row = mysql_fetch_row( $result ))
{
$sc = $row[subject_code];
$sn = $row[subject_name];
$lec = $row[lecturer];
$dep = $row[department];
$sem = $row [semester];
$y = $row[year_of_exam];
$link = $row[file_path];
print("<tr>");
print("<td>$sc</td>");
print("<td>$sn</td>");
print("<td>$lec</td>");
print("<td>$dep</td>");
print("<td>$sem</td>");
print("<td>$y</td>");
print("<td>$link</td>");
print("</tr>");
$count++;
}
?>
</table>
<br />Your search yielded <strong>
<?php print("$count") ?> results.</strong> <br/> <br />
<?php
mysql_close();
?>
</body>
</html>
Oh, and Welcome to WebmasterWorld! :)
Next, with an associative array, you should surround your array index with quotes. Instead of this:
$sc = $row[subject_code];
do this:
$sc = $row['subject_code'];
The former generates warnings.
Your code has some other minor issues but I think that will get you results.
with this one:
$query = "SELECT * FROM exam_papers WHERE
(subject_code = ".$subject_code."AND
subject_name = ".$subject_name."AND
lecturer = ".$lecturer."AND
department = ".$department."AND
semester = ".$semester."AND
year_of_exam = ".$year.")"
or die (mysql_error());
and I got this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND lecturer = Mr.alex AND department = SN AND semester = 2 AND ' at line 3
then when I changed back this block to what it was, and made the changes CAMERAMAN said, again.. I have no error, with no result. meanse i get a clean table:
Subject Code Subject Name Lecturer Department Semester Year File
Your search yielded 0 results.
Im totally lost...:(
The error was because you need single quotes around string values like Mr.alex like you had in your first post.
How about you divide and conquer - pick a field, any field, and try to get results on that, then add in the other stuff. For example, comment out the block that forces you to have values for all the form fields, then shorten your query to:
$query = "SELECT * FROM exam_papers WHERE
(subject_code = '$subject_code')";
Problem is, in the table consisting of all information (table of the page, not database) I just get the value for one column which is the path of the file and others are blank. The thing is, this variable for the path of the file is working different from others. Others get the value from search fields, and then from the related row, the path value is retrieved from the table of database.
What would be the problem here..?
This is how I get the result now (the link is under "File" column):
Subject Code Subject Name Lecturer Department Semester Year File
C:/wamp/www/uploads/cisb2222003special.pdf
Your search yielded 1 results.
P.S: should I use extract() function to validate data coming from search fields?
<?php
$count = 0;
while ($row = mysql_fetch_row( $result ))
{
$sc = $row[subject_code];
$sn = $row[subject_name];
$lec = $row[lecturer];
$dep = $row[department];
$sem = $row [semester];
$y = $row[year_of_exam];
$link = $row[file_path];
print("<tr>");
print("<td>$sc</td>");
print("<td>$sn</td>");
print("<td>$lec</td>");
print("<td>$dep</td>");
print("<td>$sem</td>");
print("<td>$y</td>");
print("<td>$link</td>");
print("</tr>");
$count++;
}
?>
with an arrey, getting each data and move pointer:
<?php
for ( $counter = 0; $row = mysql_fetch_row($result); $counter++)
{
print("<tr>");
foreach($row as $key => $value)
print("<td>$value</td>");
print("<tr>");
}
?>
but I'm still using just
$query = "SELECT * FROM exam_papers WHERE
(subject_code = '$subject_code')";
and it wont work if i put other fields as well... how should I make this query so I can have all fields checked for the search?
to this:
while ($row = mysql_fetch_assoc( $result ))
{
$sc = $row['subject_code'];
$sn = $row['subject_name'];
$lec = $row['lecturer'];
$dep = $row['department'];
$sem = $row ['semester'];
$y = $row['year_of_exam'];
$link = $row['file_path'];
then you should get the results you're getting with your foreach.
If you want the user to enter any field, you build the query with the fields that are populated. One way to do that is to set up an array and add only populated fields to it:
$clause = array();
if(!empty($_POST['subject_code'])) $clause[] = "subject_code='" . mysql_real_escape_string($_POST['subject_code']) . "'";
if(!empty($_POST['subject_name'])) $clause[] = "subject_name='" . mysql_real_escape_string($_POST['subject_name']) . "'";
if(!empty($_POST['lecturer'])) $clause[] = "lecturer='" . mysql_real_escape_string($_POST['lecturer']) . "'";
.
.
Now you need to turn the $clause array into a string, and you do that with implode() [us.php.net].
$where = implode(") AND (",$clause); // This gives us the middle
$where = "WHERE (" . $where . ")"; // This adds on the "ends"
Now you combine it with your query:
$query = "SELECT * FROM exam_papers $where";
echo $query;
One other note, where you've got this:
$num=mysql_numrows($result);
You're missing an underscore. It should be:
$num=mysql_num_rows($result);
Then you won't need your counter anymore.
I changed it to what you said, it is working properly, however, when showing the results, again, I just have the value under the column of path of the file and others is not displaying... :(
I really don’t know what happens when I change it to the while loop this happens... and I need this while loop because as you can see, I should make the path of the file as a link, with previous one I just had the path of the file, but when I change it to while loop I can hyperlink it. But now I don’t have results for columns subject code, name, lecturer, etc...
this is the full code after changes:
<html>
<head>
<title>Search in Final exam papers</title>
</head>
<body>
<?php
mysql_connect("localhost", "admin", "admin") or die(mysql_error());
mysql_select_db("ueros_db") or die(mysql_error());
?>
<?php
$clause = array();
if(!empty($_POST['subject_code'])) $clause[] = "subject_code='" . mysql_real_escape_string($_POST['subject_code']) . "'";
if(!empty($_POST['subject_name'])) $clause[] = "subject_name='" . mysql_real_escape_string($_POST['subject_name']) . "'";
if(!empty($_POST['lecturer'])) $clause[] = "lecturer='" . mysql_real_escape_string($_POST['lecturer']) . "'";
if(!empty($_POST['department'])) $clause[] = "department='" . mysql_real_escape_string($_POST['department']) . "'";
if(!empty($_POST['semester'])) $clause[] = "semester='" . mysql_real_escape_string($_POST['semester']) . "'";
if(!empty($_POST['year'])) $clause[] = "year_of_exam='" . mysql_real_escape_string($_POST['year']) . "'";
$where = implode(") AND (",$clause); // This gives the middle
$where = "WHERE (" . $where . ")"; // This adds on the "ends"
$query = "SELECT * FROM exam_papers $where"
or die (mysql_error());
echo $query;
$result = mysql_query($query) or die (mysql_error());
$num=mysql_num_rows($result);
?>
<table border="1">
<tr>
<td>Subject Code</td>
<td>Subject Name</td>
<td>Lecturer</td>
<td>Department</td>
<td>Semester</td>
<td>Year</td>
<td>File</td>
</tr>
<?php
while ($row = mysql_fetch_assoc( $result ))
{
$sc = $row['subject_code'];
$sn = $row['subject_name'];
$lec = $row['lecturer'];
$dep = $row['department'];
$sem = $row ['semester'];
$y = $row['year_of_exam'];
$link = $row['file_path'];
print("<tr>");
print("<td>$sc</td>");
print("<td>$sn</td>");
print("<td>$lec</td>");
print("<td>$dep</td>");
print("<td>$sem</td>");
print("<td>$y</td>");
print("<td><a href = \"$link\" target=\"_blank\">View</a></td>");
print("</tr>");
}
?>
</table>
<br />Your search yielded <strong>
<?php print("$num") ?> result(s).</strong> <br/> <br />
<?php
mysql_close();
?>
</body>
</html>
look at the result, it'll be a bit messy, but make sure you're getting the data and that it's with the field names you're using.
array(7) {
["Subject_code"]=>
string(7) "cisb222"
["Subject_name"]=>
string(7) "sub"
["Lecturer"]=>
string(8) "Mrs. something"
["Department"]=>
string(2) "SN"
["Semester"]=>
string(1) "2"
["Year_of_exam"]=>
string(4) "2003"
["file_path"]=>
string(42) "C:/wamp/www/uploads/cisb2222003special.pdf"
}
and when I use this for loop:
for ( $counter = 0; $row = mysql_fetch_row($result); $counter++)
{
print("<tr>");
foreach($row as $key => $value)
print("<td>$value</td>");
print("<tr>");
}
everything is ok. so there should be something wrong with the while loop... and I need to use that to get data one by one so I make the file path as a link...
Anyways... I need one more help. as you see, this is a search page that looks up for a file.
I made a hyperlink to the file, which the exact path is stored in database and file is in a folder in the server.
When I get the link as a result, nothing happens when I click on it. But if I right-click and save the target, I get the file correctly. Why this happens? And how should I make a link to view the file in the browser?
this is the code for that page:
<html>
<head>
<title>upload</title></head>
<body>
<form method="post" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td>
Subject Code: </td><td> <input type="text" name="subject_code" id="subject_code">
</td></tr><tr><td>
Subject Name: </td><td><input type="text" name="subject_name" id="subject_name">
</td></tr><tr><td>
Lecturer: </td><td> <input type="text" name="lecturer" id="lecturer">
</td></tr><tr><td>
Department: </td><td>
<select name= "department" id="department">
<option value = "IS" SELECTED>IS
<option value = "GM">GM
<option value = "SN">SN
<option value = "SE">SE
</select>
</td></tr><tr><td>
Year of exam: </td><td> <input type="text" name="year">
</td></tr><tr><td>
Semester: </td><td>
<select name= "semester" id="semester">
<option value = "1" SELECTED>1
<option value = "2">2
<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>
</tr>
</table>
</form>
<?php
mysql_connect("localhost", "admin", "admin") or die(mysql_error());
mysql_select_db("ueros_db") or die(mysql_error());
$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 "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('Error, query failed : ' . mysql_error());
echo "<br>Files uploaded<br>";
}
?>
</body>
</html>
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\wamp\tmp\php49A4.tmp' to 'http://localhost/uploads/cisb11120051.pdf' in C:\wamp\www\upload.php on line 87
Error uploading file
and line 87 is this part:
$result = move_uploaded_file($tmpName, $filePath);
What you actually should do is to move the uploaded the file in to "C:/wamp/www/uploads/" but insert in the database the address "http://localhost/uploads/cisb11120051.pdf".
Your new code and sql query should be something like:
$urlPath = 'http://localhost/uploads/'. $uniqueName . $ext;
$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]', '$urlPath')";