Jump to content

PHP SQL Search Function


ellifant
Go to solution Solved by requinix,

Recommended Posts

I'm really new to PHP & SQL. I'm trying to create a search function for a research database. I keep getting the following error:  request "Could not execute SQL query" SELECT * title GROUP BY keyword ORDER BY id.

 

How do I fix it?!

<?php

error_reporting(0);
include("/config2.php");
?>
<!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>MySQL table search</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<style>
BODY, TD {
	font-family:Arial, Helvetica, sans-serif;
	font-size:12px;
}
</style>
</head>


<body>

<form id="form1" name="form1" method="post" action="file:///C|/Users/lmfish89.Laura-PC/AppData/Local/Temp/Temp1_mysql-search-table.zip/search.php">
<label for="title">Title</label>
<input name="title" type="text" id="title" size="10" value="<?php echo $_REQUEST["title"]; ?>" />
<label for="author">Author</label>
<input name="author" type="text" id="author" size="10" value="<?php echo $_REQUEST["author"]; ?>"/>
 <label>Abstract:</label>
<input type="text" name="abstract" id="abstract" value="<?php echo stripcslashes($_REQUEST["abstract"]); ?>" />
<label>Keyword</label><input type="text" name="keyword" id="keyword" value="<?php echo stripcslashes($_REQUEST["keyword"]); ?>" />
<?php
	$sql = "SELECT * title ".$SETTINGS["database"]." GROUP BY keyword ORDER BY id";
	$sql_result = mysql_query ($sql, $config2) or die ('request "Could not execute SQL query" '.$sql);
	while ($row = mysql_fetch_assoc($sql_result)) {
		echo "<option value='".$row["keyword"]."'".($row["keyword"]==$_REQUEST["keyword"] ? " selected" : "").">".$row["keyword"]."</option>";
	}
?>
</select>
<input type="submit" name="butauthorn" id="butauthorn" value="Filter" />
  </label>
  <a href="file:///C|/Users/lmfish89.Laura-PC/AppData/Local/Temp/Temp1_mysql-search-table.zip/search.php"> 
  reset</a>
</form>
<br /><br />
<table width="700" border="1" cellspacing="0" cellpadding="4">
  <tr>
    <td width="90" bgcolor="#CCCCCC"><strong>Title </strong></td>
    <td width="95" bgcolor="#CCCCCC"><strong>Author(s)</strong></td>
    <td width="159" bgcolor="#CCCCCC"><strong>Abstract</strong></td>
    <td width="191" bgcolor="#CCCCCC"><strong>Keywords</strong></td>
    <td width="113" bgcolor="#CCCCCC"><strong>Link</strong></td>
  </tr>
<?php
if ($_REQUEST["string"]<>'') {
	$search_string = " AND (keyword LIKE '%".mysql_real_escape_string($_REQUEST["string"])."%' OR abstract LIKE '%".mysql_real_escape_string($_REQUEST["string"])."%')";	
}
if ($_REQUEST["keyword"]<>'') {
	$search_keyword = " AND keyword='".mysql_real_escape_string($_REQUEST["keyword"])."'";	
}

if ($_REQUEST["title"]<>'' and $_REQUEST["author"]<>'') {
	$sql = "SELECT * title ".$SETTINGS["database"]." WHERE title_date >= '".mysql_real_escape_string($_REQUEST["title"])."' AND author_date <= '".mysql_real_escape_string($_REQUEST["author"])."'".$search_string.$search_keyword;
} else if ($_REQUEST["title"]<>'') {
	$sql = "SELECT * title ".$SETTINGS["database"]." WHERE title_date >= '".mysql_real_escape_string($_REQUEST["title"])."'".$search_string.$search_keyword;
} else if ($_REQUEST["author"]<>'') {
	$sql = "SELECT * title ".$SETTINGS["database"]." WHERE author_date <= '".mysql_real_escape_string($_REQUEST["author"])."'".$search_string.$search_keyword;
} else {
	$sql = "SELECT * title ".$SETTINGS["database"]." WHERE id>0".$search_string.$search_keyword;
}

$sql_result = mysql_query ($sql, $config2) or die ('request "Could not execute SQL query" '.$sql);
if (mysql_num_rows($sql_result)>0) {
	while ($row = mysql_fetch_assoc($sql_result)) {
?>
  <tr>
    <td><?php echo $row["title"]; ?></td>
    <td><?php echo $row["author"]; ?></td>
    <td><?php echo $row["abstract"]; ?></td>
    <td><?php echo $row["keyword"]; ?></td>
    <td><?php echo $row["link"]; ?></td>
  </tr>
<?php
	}
} else {
?>
<tr><td colspan="5">No results found.</td>
<?php	
}
?>
</table>
</body>
</html>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.