joshgarrod Posted June 29, 2007 Share Posted June 29, 2007 Hi all, I have a script which searches my stock database which works fine thanks to some of u guys, (u no who u r) however I want to log all of the searches made so we know what the demand is. How would I go about doing this? Please could you help me. Thanks in advance. <!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=iso-8859-1" /> <title>Search for spares</title> <style type="text/css"> <!-- .style1 {font-family: Arial, Helvetica, sans-serif} --> </style> </head> <body> <h2 class="style1">Search</h2> <form action="<?=$PHP_SELF?>" method="post" name="search" class="style1"> Seach for: <input type="text" name="find" /> in <Select NAME="field"> <Option VALUE="StockDescription">Stock description</option> <Option VALUE="ID">Stock ID</option> <Option VALUE="StockNumber">Stock number</option> </Select> <input type="hidden" name="searching" value="yes" /> <input type="submit" name="search" value="Search" /> </form> <p class="style1"> <?php //This is only displayed if they have submitted the form if ($searching =="yes") { echo "<h2>Results</h2><hr>"; //If they did not enter a search term we give them an error if ($find == "") { echo "<p>You forgot to enter a search term"; exit; } // Otherwise we connect to our Database mysql_connect("localhost", "dhhfd", "idhdh") or die(mysql_error()); mysql_select_db("sdhdhdh") or die(mysql_error()); // We preform a bit of filtering $find = strtoupper($find); $find = strip_tags($find); $find = trim ($find); //Now we search for our search term, in the field the user specified $find_array = explode(" ", $find); $find_sql = ""; // init foreach ($find_array as $temp) $find_sql .= "upper($field) LIKE '%$temp%' OR "; // note only need to do the substr once, the second time was removing the other %' $find_sql = substr($find_sql, 0, -3); // throw away last OR statement $sql = "SELECT * FROM stock WHERE $find_sql"; $data = mysql_query($sql) OR DIE("SQL Was: " . $sql . "<br />ERROR: " . mysql_error()); $sql = "SELECT * FROM stock"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { // It is now safe to use $result. } else { // No results found. } } else { // Query failed. Good place to debug, eg; echo "Query failed<br />" . $sql . "<br />" . mysql_error(); } //And we display the results while($result = mysql_fetch_array( $data )) { echo "<b>Stock ID: </b>"; echo $result['ID']; echo "<br>"; echo "<b>Stock number: </b>"; echo $result['StockNumber']; echo "<br>"; echo "<b>Stock description: </b>"; echo $result['StockDescription']; echo "<br>"; echo "<b>Price: </b>"; echo $result['ItemPrice']; echo "<br>"; echo "<hr>"; } //This counts the number or results - and if there wasn't any it gives them a little message explaining that $anymatches=mysql_num_rows($data); if ($anymatches == 0) { echo "Sorry, but we can not find an entry to match your query<br><br>"; } //And we remind them what they searched for echo "<b>Searched For:</b> " .$find; } ?> </p> </body> </html> Link to comment https://forums.phpfreaks.com/topic/57693-solved-how-would-i-do-this/ Share on other sites More sharing options...
phpknight Posted June 29, 2007 Share Posted June 29, 2007 After cleaning up the searches for dangerous stuff, just write the search to a .txt or .log file in append mode. $fileName="yourfilename"; $fp=fopen($fileName, "a"); $search="your search stuff\n";//with endline char $bytesWritten = fwrite ($fp, $search); //send email to yourself with errors on $bytesWritten Link to comment https://forums.phpfreaks.com/topic/57693-solved-how-would-i-do-this/#findComment-285634 Share on other sites More sharing options...
joshgarrod Posted June 29, 2007 Author Share Posted June 29, 2007 rite, so say i called my search log, searchLog.txt and the email address was [email protected] what would it look like? Link to comment https://forums.phpfreaks.com/topic/57693-solved-how-would-i-do-this/#findComment-285652 Share on other sites More sharing options...
phpknight Posted June 29, 2007 Share Posted June 29, 2007 $fileName="searchLog.txt"; $fp=fopen($fileName, "a"); $search="your search stuff\n";//with endline char $bytesWritten = fwrite ($fp, $search); if ($bytesWritten < 1) mail ("[email protected]", "problem writing to log", "eom");//if this fails, check directory permissions Link to comment https://forums.phpfreaks.com/topic/57693-solved-how-would-i-do-this/#findComment-285655 Share on other sites More sharing options...
joshgarrod Posted June 29, 2007 Author Share Posted June 29, 2007 where you have written "your search stuff" i have put the names of the variables that the data is stored in but it just prints the names, I want to log what they searched for Link to comment https://forums.phpfreaks.com/topic/57693-solved-how-would-i-do-this/#findComment-285671 Share on other sites More sharing options...
phpknight Posted June 29, 2007 Share Posted June 29, 2007 I am not sure what you mean. Give me an example of what the search is, what you want, and what you actually get. Link to comment https://forums.phpfreaks.com/topic/57693-solved-how-would-i-do-this/#findComment-285674 Share on other sites More sharing options...
joshgarrod Posted June 29, 2007 Author Share Posted June 29, 2007 ah its ok i have sorted it now, i was quoting the variable names wrong. it works fine thanks for your help. Link to comment https://forums.phpfreaks.com/topic/57693-solved-how-would-i-do-this/#findComment-285675 Share on other sites More sharing options...
phpknight Posted June 29, 2007 Share Posted June 29, 2007 Great stuff! Link to comment https://forums.phpfreaks.com/topic/57693-solved-how-would-i-do-this/#findComment-285677 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.