Jump to content
Killersites Forums

RECORDSET SEARCH FORM, WHAT AM I DOING WRONG?


Recommended Posts

I'm creating a image gallery and I'm about 90% complete. One of my final objectives is to create a keyword search for my image gallery. I have 4 fields in my DB, (id,layout,description,keywords) I want to be able to enter keywords into the text field to make the images with there descriptions show up.

 

When I view my search page in firefox and type in a keyword in the text field, when the results page loads all I get is the field names in my DB table (id,layouts,desription,key_words) instead of the actual data...?? Someone told me to use LIKE instead of equal but that didn't work either. It would be greatly appreciated if someone could tell me what I'm doing wrong.

 

HERES MY CODE

 

SEARCH PAGE:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-…'>http://www.w3.org/TR/xhtml1/DTD/xhtml1-…

<html xmlns="http://www.w3.org/1999/xhtml">'>http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

</head>

 

<body>

<form id="form1" name="form1" method="get" action="search_results.php">

<label for="textfield"></label>

<input type="text" name="textfield" id="textfield" />

<input type="submit" name="button" id="button" value="search" />

</form>

</body>

</html>

 

 

RESULTS PAGE:

 

<?php require_once('Connections/test_db.php'); ?>

<?php

if (!function_exists("GetSQLValueString")) {

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")

{

if (PHP_VERSION < 6) {

$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

}

 

$theValue = function_exists("mysql_real_escape_strin… ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

 

switch ($theType) {

case "text":

$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

break;

case "long":

case "int":

$theValue = ($theValue != "") ? intval($theValue) : "NULL";

break;

case "double":

$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";

break;

case "date":

$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

break;

case "defined":

$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;

break;

}

return $theValue;

}

}

 

$maxRows_Recordset1 = 3;

$pageNum_Recordset1 = 0;

if (isset($_GET['pageNum_Recordset1'])) {

$pageNum_Recordset1 = $_GET['pageNum_Recordset1'];

}

$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;

 

$colname_Recordset1 = "-1";

if (isset($_GET['key_words'])) {

$colname_Recordset1 = $_GET['key_words'];

}

mysql_select_db($database_test_db, $test_db);

$query_Recordset1 = sprintf("SELECT * FROM images2 WHERE key_words = %s ORDER BY id DESC", GetSQLValueString($colname_Recordset1, "text"));

$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);

$Recordset1 = mysql_query($query_limit_Recordset1, $test_db) or die(mysql_error());

$row_Recordset1 = mysql_fetch_assoc($Recordset1);

 

if (isset($_GET['totalRows_Recordset1'])) {

$totalRows_Recordset1 = $_GET['totalRows_Recordset1'];

} else {

$all_Recordset1 = mysql_query($query_Recordset1);

$totalRows_Recordset1 = mysql_num_rows($all_Recordset1);

}

$totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Reco…

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-…

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

</head>

 

<body>

<table cellpadding="4" cellspacing="4">

<tr>

<td>id</td>

<td>Layouts</td>

<td>Descriptions</td>

<td>key_words</td>

</tr>

<?php do { ?>

<tr>

<td><?php echo $row_Recordset1['id']; ?></td>

<td><?php echo $row_Recordset1['Layouts']; ?></td>

<td><?php echo $row_Recordset1['Descriptions']; ?></td>

<td><?php echo $row_Recordset1['key_words']; ?></td>

</tr>

<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>

</table>

</body>

</html>

<?php

mysql_free_result($Recordset1);

?>

Link to post
Share on other sites

you aren't even getting the value from $_GET['textfield']. Since that is the name of your input text box... what is $_GET['pageNum_Recordset1']?

 


<body>
<form id="form1" name="form1" method="get" action="search_results.php">
<label for="textfield"></label>
<input type="text" name="textfield" id="textfield" />
<input type="submit" name="button" id="button" value="search" />
</form>
</body>
</html>

 

method="get"

name="textfield"

 

your form method is using GET so you would use $_GET and your text field is name="textfield" so it would be $_GET['textfield'] and you might want to use htmlentities() function to keep ppl from injecting unwanted code into your database.

 

If you are trying to search for something in your database by using a text field you have to use LIKE...

 


$search = $_GET['textfield'];

"SELECT * FROM table LIKE '%$search%'"

 

Or you could use a while() and put an if() inside of the while statement but that would be unneeded code. Just use LIKE i've given you numerous example and if you search google.com for MySQLi LIKE you will get many more.

Edited by Archadian
Link to post
Share on other sites

Are you sure you aren't just seeing your table header row, with "id", "layouts", etc, and you are getting no results from your query?

 

That's exactly whats happening, How do I get this to work. I've followed this step by step: http://livedocs.adobe.com/en_US/Dreamweaver/9.0/help.html?content=WScbb6b82af5544594822510a94ae8d65-78ae.html but I still get no results

Link to post
Share on other sites

Well the first place I would start is with your search form. (Basically, I'm just repeating what Archadian said above.)

 

In the results page, this line gets the keywords you are looking for:

 

$colname_Recordset1 = $_GET['key_words'];

 

But this line requires the $_GET['key_words'] variable to be set within the URL, which it isn't currently. So I would start by changing your search form input where you enter in the keywords you are searching for from:

 

<input type="text" name="textfield" id="textfield" />

 

to

 

<input type="text" name="key_words" id="textfield" />

 

I'm sure there are more tweaks you will need to make, but start with that, and then report back.

Link to post
Share on other sites

Well the first place I would start is with your search form. (Basically, I'm just repeating what Archadian said above.)

 

In the results page, this line gets the keywords you are looking for:

 

$colname_Recordset1 = $_GET['key_words'];

 

But this line requires the $_GET['key_words'] variable to be set within the URL, which it isn't currently. So I would start by changing your search form input where you enter in the keywords you are searching for from:

 

<input type="text" name="textfield" id="textfield" />

 

to

 

<input type="text" name="key_words" id="textfield" />

 

 

I'm sure there are more tweaks you will need to make, but start with that, and then report back.

 

 

I changed the search form, but as you suspected It still won't return my db data.

search page

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'>http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">'>http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

</head>

 

<body>

<form action="search_results.php" method="get" name="form1" id="form1">

<label for="textfield"></label>

<input type="text" name="key_words" id="textfield" />

<input type="submit" name="button" id="button" value="search" />

</form>

</body>

</html>

results page

 

<?php require_once('Connections/test_db.php'); ?>

<?php

if (!function_exists("GetSQLValueString")) {

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")

{

if (PHP_VERSION < 6) {

$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

}

 

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

 

switch ($theType) {

case "text":

$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

break;

case "long":

case "int":

$theValue = ($theValue != "") ? intval($theValue) : "NULL";

break;

case "double":

$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";

break;

case "date":

$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

break;

case "defined":

$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;

break;

}

return $theValue;

}

}

 

$maxRows_Recordset1 = 3;

$pageNum_Recordset1 = 0;

if (isset($_GET['pageNum_Recordset1'])) {

$pageNum_Recordset1 = $_GET['pageNum_Recordset1'];

}

$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;

 

$colname_Recordset1 = "-1";

if (isset($_GET['key_words'])) {

$colname_Recordset1 = $_GET['key_words'];

}

mysql_select_db($database_test_db, $test_db);

$query_Recordset1 = sprintf("SELECT * FROM images2 WHERE key_words = %s ORDER BY id DESC", GetSQLValueString($colname_Recordset1, "text"));

$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);

$Recordset1 = mysql_query($query_limit_Recordset1, $test_db) or die(mysql_error());

$row_Recordset1 = mysql_fetch_assoc($Recordset1);

 

if (isset($_GET['totalRows_Recordset1'])) {

$totalRows_Recordset1 = $_GET['totalRows_Recordset1'];

} else {

$all_Recordset1 = mysql_query($query_Recordset1);

$totalRows_Recordset1 = mysql_num_rows($all_Recordset1);

}

$totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1;

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

</head>

 

<body>

<table cellpadding="4" cellspacing="4">

<tr>

<td>id</td>

<td>Layouts</td>

<td>Descriptions</td>

<td>key_words</td>

</tr>

<?php do { ?>

<tr>

<td><?php echo $row_Recordset1['id']; ?></td>

<td><?php echo $row_Recordset1['Layouts']; ?></td>

<td><?php echo $row_Recordset1['Descriptions']; ?></td>

<td><?php echo $row_Recordset1['key_words']; ?></td>

</tr>

<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>

</table>

</body>

</html>

<?php

mysql_free_result($Recordset1);

?>

Link to post
Share on other sites

Unfortunately, I really don't have the time at the moment to sit down and work through any issues with this code. Personally, this isn't the way I would write search functionality, and you may be best off finding a different tutorial (maybe see http://www.joedolson.com/Search-Engine-in-PHP-MySQL.php or http://www.codeforest.net/simple-search-with-php-jquery-and-mysql) that does a better job of explaining things better / actually working.

 

If you do want to stick this one out and try to get it working, the first place I would start with is by opening up PHPMyAdmin and testing to make sure you don't have any errors in your SQL syntax. Make sure you are getting the proper results in PHPMyAdmin first, and then come back to your script.

 

I would also go back to whatever tutorial you are currently using, and check to make sure this part of the code is accurate:

 

$query_Recordset1 = sprintf("SELECT * FROM images2 WHERE key_words = %s ORDER BY id DESC", GetSQLValueString($colname_Recordset1, "text"));
$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);
$Recordset1 = mysql_query($query_limit_Recordset1, $test_db) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);

 

I usually use MySQLi and prepared statements to handle database queries, and the syntax is slightly different, but my impression is that you aren't ever actually querying the database for $query_Recordset1. I only see a mysql_query() line for the limit statement.

Link to post
Share on other sites

It isn't working if, for one, you are using something someone else wrote and you copied it. If the columns in the DB don't match up to the code, its not going to work. If you have the wrong username, host, password and DB name, its not going to work. If your PHP code is not able to retrieve the data your form is passing through...guess what, its not going to work. All you need is your form and write your PHP code which Ben and I explained above to retrieve the value from the textbox then do a SELECT to your DB and use the...yes the LIKE %$textbox% to pull the records of everything "$textbox" ($textbox = the name of the textbox in the form and the $_GET or $_POST) matches in your database then display it to a table. If Ben doesn't understand whats going on in that code its time to get a new script :P.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...