PDA

View Full Version : Search Engine


greg.farmer
07-23-2014, 03:41 PM
Hi everybody

I have been following this tutorial to build a search and return page

http://help.adobe.com/en_US/dreamweaver/cs/using/WScbb6b82af5544594822510a94ae8d65-78b0a.html

I am having trouble in that I can't get my results to display.

Below is my code. I would appreciate any help anyone can offer. Thanks

Search page



<!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>
<p>Search</p>
<form id="form1" name="form1" method="get" action="returns.php">
<label for="name">Name</label>
<input type="text" name="name" id="name" />
<input type="submit" name="submit" id="submit" value="Submit" />
</form>
</body>
</html>




RETURNS PAGE

<?php require_once('Connections/connTSweb.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;
}
}
$colname_Recordset1 = "-1";
if (isset($_GET['form1'])) {
$colname_Recordset1 = $_GET['form1'];
}
mysql_select_db($database_connTSweb, $connTSweb);
$query_Recordset1 = sprintf("SELECT id, firstname, lastname, `alias`, `role`, ext, ddi, mobile, fax, email, chargeoutrate, secretary, secext FROM staff WHERE firstname = %s ORDER BY lastname ASC", GetSQLValueString($colname_Recordset1, "text"));
$Recordset1 = mysql_query($query_Recordset1, $connTSweb) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
$colname_Recordset1 = "-1";
if (isset($_GET['form1'])) {
$colname_Recordset1 = $_GET['form1'];
}
mysql_select_db($database_connTSweb, $connTSweb);
$query_Recordset1 = sprintf("SELECT firstname, lastname, `alias`, ext, ddi, mobile, fax, email, chargeoutrate, secretary, secext FROM staff WHERE firstname = %s ORDER BY lastname ASC", GetSQLValueString($colname_Recordset1, "text"));
$Recordset1 = mysql_query($query_Recordset1, $connTSweb) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
?>
<!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>
<p>Return </p>
<p>&nbsp;</p>
<table border="0" cellpadding="4" cellspacing="0">
<tr>
<td>firstname</td>
<td>lastname</td>
<td>alias</td>
<td>ext</td>
<td>ddi</td>
<td>mobile</td>
<td>fax</td>
<td>email</td>
<td>chargeoutrate</td>
<td>secretary</td>
<td>secext</td>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row_Recordset1['firstname']; ?></td>
<td><?php echo $row_Recordset1['lastname']; ?></td>
<td><?php echo $row_Recordset1['alias']; ?></td>
<td><?php echo $row_Recordset1['ext']; ?></td>
<td><?php echo $row_Recordset1['ddi']; ?></td>
<td><?php echo $row_Recordset1['mobile']; ?></td>
<td><?php echo $row_Recordset1['fax']; ?></td>
<td><?php echo $row_Recordset1['email']; ?></td>
<td><?php echo $row_Recordset1['chargeoutrate']; ?></td>
<td><?php echo $row_Recordset1['secretary']; ?></td>
<td><?php echo $row_Recordset1['secext']; ?></td>
</tr>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

Ricky55
07-23-2014, 03:50 PM
What happens when you run a search?

Have you set up the database and are you running a local environment to test PHP?

greg.farmer
07-23-2014, 05:24 PM
Thanks for the reply Ricky55. Yes I am running a local PHP test environment. Everything else is working OK jus this.

My returns should be in a table but all I see are the table headers and that is all.

Ricky55
07-23-2014, 10:38 PM
Just making sure it wasn't anything obvious.

TBH with you my PHP isn't strong enough to spot an error just by looking at the code. Ed may be able to help you. I'm a front end guy, HTML, CSS and JS.

If you want a quick answer you could try on the PHP Freaks forum many of their users use PHP daily and I'm sure will be able to point you in the right direction.

Good luck.

edbr
07-24-2014, 03:44 AM
<input type="text" name="name" id="name" /> is the value you are passing.
the select is based on WHERE firstname = %s
so i can only guess that this is the wrong parameter?

Frankly it would be better to use POST as the action i would think, bit mostly do not use dreamweaver wizard for this, way too code soup to help y0u get the basics.

use a form to select the name ( method post)
on the results page colect the passed value as (for instance) $name= $_POST['name]

use a connection and a select
"SELECT firstname, lastname, `alias`, ext, ddi, mobile, fax, email, chargeoutrate, secretary, secext FROM staff WHERE firstname = $name ORDER BY lastname ASC"

then echo the results. you can find code examples on tizag.com or we can help you

edbr
07-25-2014, 02:14 AM
here a simple example using PDO, ill post a mysqli example later time permitting

Connect

<?php
$hostname = "localhost";
$username= "root";
$password = "pass";
$db="dbname";
try {
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
// echo "Connected to database"; // check for connection
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>

select

<?php
$sql = "SELECT * FROM tablename ORDER BY Rand() LIMIT 2";

foreach ( $dbh->query($sql) as $row) { ?>
<?php echo $row['comment']."&nbsp"."&nbsp"."&nbsp"."<strong>". $row['name']."</strong>"."<br>"."<br>";

} ?>

now if you wanted to select using a passed value
$news= $_POST['name']; //or $_GET
$sql = "SELECT * FROM tablename WHERE lastname =$name;