Welcome to WebmasterWorld Guest from 18.205.176.85

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

creating a search page, no error, no results!

     
5:18 am on Aug 20, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Aug 20, 2008
posts: 79
votes: 0


hello everyone,
I am creating a search page, so it searches the database and shows the rows wanted in a table.
I dont have any error, but it doesnt show any results to me.
can anyone tell me whats the problem?:(
this is the code for php page:
something else, what should I do to create the search engine that anyone can fill any fields they want, not all compulsory... im using AND operators right now, because I cant use OR because it shows every row that matches each fields and the search will not be correct. but how to create a correct one that user can enter any field they want?

<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>

5:59 am on Aug 20, 2008 (gmt 0)

Senior Member

joined:Nov 12, 2005
posts:5967
votes: 0


Echo the query that you are using in this script to the browser, then run the query in phpmyadmin or through the command line manually to see if the query is returning any results. If it's not, then you know where the error is. If it does, then you should take a look at how you are echoing the results from the query.

Oh, and Welcome to WebmasterWorld! :)

6:10 am on Aug 20, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 16, 2007
posts:914
votes: 0


Welcome to WebmasterWorld, lindaonline15.
The most pressing issue is you're using mysql_fetch_row() [us.php.net] to retrieve the table rows, but that function returns a numeric indexed array. Try changing to mysql_fetch_assoc() [us.php.net].

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.

6:42 am on Aug 20, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Aug 20, 2008
posts: 79
votes: 0


Thank both of you for welcome and rapid reply...
I know my source has some problems. Im quite very new to this:(
actually, I have done some changes:
first, I did what EELIXDUPPY said, and I changed this part:
$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());

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...:(

7:13 am on Aug 20, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 16, 2007
posts:914
votes: 0


Im quite very new to this
Every single one of us was at some time!

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')";

7:16 am on Aug 20, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 16, 2007
posts:914
votes: 0


Another trick is to echo $query to the browser so you can copy & paste it into phpMyAdmin like eelix suggested.
8:09 am on Aug 20, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Aug 20, 2008
posts: 79
votes: 0


I shorten the query to only one object, and now Iím getting some results...

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?

12:30 pm on Aug 20, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Aug 20, 2008
posts: 79
votes: 0


I got the whole results with changing this block:

<?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?

6:42 pm on Aug 20, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 16, 2007
posts:914
votes: 0


If you change this:
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];

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.

4:37 am on Aug 21, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Aug 20, 2008
posts: 79
votes: 0


Thanks a lot cameraman... I got the search engine I wanted...

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>

5:40 am on Aug 21, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 16, 2007
posts:914
votes: 0


This is going to wind up being something really silly because there's just nothing tragically wrong here.
Try this: comment out the whole while loop (put /* before while and */ at its closing brace).
Then just before the while, do this:
$row = mysql_fetch_assoc( $result );
echo "<pre>";
var_dump($row);
echo "</pre>";

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.

6:39 am on Aug 21, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Aug 20, 2008
posts:79
votes: 0


yes, everything is exactly correct. I got this:

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...

6:46 am on Aug 21, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 16, 2007
posts:914
votes: 0


Alrightie, that's the problem. The array elements are case sensitive - you need to match the case that's in the field names:
$sc = $row['Subject_code'];
$sn = $row['Subject_name'];
.
.
6:55 am on Aug 21, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Aug 20, 2008
posts: 79
votes: 0


Oh... yes! Thank you...
Since SQL itself is not case sensitive, I did the same way there:D

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?

7:34 am on Aug 21, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 16, 2007
posts:914
votes: 0


Are you usually able to view PDFs in your browser? The only reason I can think of that it wouldn't work is if you don't have a pdf viewer plugin.
7:51 am on Aug 21, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Aug 20, 2008
posts: 79
votes: 0


yes. I always can see any pdf file in my browser. I'm using IE by the way..
8:10 am on Aug 21, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Aug 20, 2008
posts: 79
votes: 0


i tried to open this with firefox, I got this msg:
Firefox doesn't know how to open this address, because the protocol (c) isn't associated with any program.

with firefox I even cant save the file, but in IE I can..

11:40 am on Aug 21, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Aug 7, 2007
posts:103
votes: 0


The problem probably is that your PDF-file path is "C:/wamp/www/uploads/cisb2222003special.pdf". Basically your trying to access a file in your own system thru internet.
Try changing the path to something like "http://www.example.com/uploads/cisb2222003special.pdf" or just "/uploads/cisb2222003special.pdf".
2:58 am on Aug 22, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Aug 20, 2008
posts: 79
votes: 0


thanks deMorte for that, but the problem is, I'm using another page to upload the files in upload directory and save the file's path in database. in that page, if i do like /uploads/cisb...pdf, it won't upload the file and I have to put the whole path so it knows the path and uploads it in the upload directory.

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>

7:21 am on Aug 22, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Aug 7, 2007
posts:103
votes: 0


WAMP is an Apache server that runs on a local machine as test environment, right?
Try changing the $uploadDir to: "http://localhost/uploads/". This works with XAMPP (a similar software).
7:18 pm on Aug 22, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Aug 20, 2008
posts: 79
votes: 0


actually I tried that but I got these warnings:
Warning: move_uploaded_file(http://localhost/uploads/cisb11120051.pdf) [function.move-uploaded-file]: failed to open stream: HTTP wrapper does not support writeable connections in C:\wamp\www\upload.php on line 87

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);

11:27 am on Aug 26, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Aug 7, 2007
posts:103
votes: 0


Sorry, I misread my own code.

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')";