0

Happy New Year Everyone!

I have this existing php code I did when I first started and I was wondering if there a way to make it OOP or if I should find another way of recreating it. All opinions welcome.

<?php
require("local_php.inc");
DatabaseConnect();

//Variables to post to results.php

$keyword=$_POST['keyword'];
$location=$_POST['location'];
$type=$_POST['type'];

//Query

$q1 = "SELECT ssearch.id, ssearch.Company, ssearch.Address, ssearch_state.state_location, ssearch.City, ssearch.Zip, ssearch.Phone, ssearch.Description, ssearch.Email, 
ssearch_event.Event_name FROM 

(ssearch LEFT OUTER JOIN ssearch_state ON ssearch.State = ssearch_state.id) 

LEFT OUTER JOIN  ssearch_event ON ssearch.Event_id = ssearch_event.id 

WHERE ssearch.Company LIKE '%$keyword%'";

if($location != "all")
{
 $q1 .= "AND ssearch_state.state_location ='$location' "; 
}
if($type != "all")
{
 $q1 .= "AND ssearch_event.Event_name ='$type' "; 
}
$q1 .= "ORDER BY ssearch.Company DESC LIMIT 0, 3;";
$result = mysql_query($q1);
$q1_total_rows = mysql_num_rows($result);

?>
<div id="content"> 
 <?php if($q1_total_rows >= 1) { ?>
 <?php while ($record = mysql_fetch_assoc($result)) { ?>
 <div id="table">
  <table width="379" height="64" border="0" align="left" bordercolor="#FFFF00" bgcolor="#FEFFD5">
   <tr>
    <td width="187"><p align="center" class="contentfontsmallsearch"><a href="display.php?ID=<?php echo $record["id"];?>"><?php echo $record["Company"];?></a></p></td>
    <td width="182"><p align="center" class="contentfontsmallsearch"><?php echo $record["Event_name"];?></p></td>
   </tr>
   <tr>
    <td><p align="center" class="contentfontsmallsearch"><?php echo $record["state_location"];?></p></td>
    <td><p align="center" class="contentfontsmallsearch"><?php echo $record["Description"];?></p></td>
   <tr>
  </table>
 </div>
 <?php } ?>
 <? } else { ?>
 <div id="table2">
 <h3 align="center" class="noresults">No Results</h3><br />
 <p align="center" class="contentfontsmallsearch">Please Try your Search Again</p><br />
 <form action="results.php" method="post" name="myform" id="myform">
  <table width="379" height="190" border="0" align="left" bordercolor="#FFFF00" bgcolor="#FEFFD5">
   <tr>
    <td width="143"><p align="center" class="contentfontsmallsearch">Keyword</p></td>
    <td width="226">
     <label>
       <input name="keyword" type="text" id="keyword" size="30" />
     </label>
    </td>
   </tr>
   <tr>
    <td><p align="center" class="contentfontsmallsearch">Location</p></td>
    <td>
     <select name="location">
       <option value="all" selected="selected">All Locations</option>
       <option value="Pennsylvania">Pennsylvania</option>
       <option value="New Jersey">New Jersey</option>
       <option value="Delaware">Delaware</option>
     </select>
    </td>
   <tr>
    <td width="143"><p align="center" class="contentfontsmallsearch">Event Type </p></td>
    <td>
     <select name="type">
       <option value="all" selected="selected">All Events</option>
       <option value="Rally Event">Rally Event</option>
       <option value="Awareness Event">Awareness Event</option>
       <option value="Donation Event">Donation Event</option>
     </select>
    </td>
   </tr>
   <tr>
    <td></td>
    <td><input type="submit" name="Submit2" value="Search" /></td>
   </tr>
  </table>
 </form>
 </div>
 <? } ?>
</div>
hakre
  • 178,314
  • 47
  • 389
  • 754
blackbull77
  • 281
  • 1
  • 5
  • 14
  • 1
    What's your acute reason for wanting to rewrite it? – Pekka Jan 01 '11 at 15:28
  • 1
    One thing that you should urgently fix though is [SQL injection vulnerability](http://stackoverflow.com/questions/60174/best-way-to-stop-sql-injection-in-php) – Pekka Jan 01 '11 at 15:28
  • first move, discard use of mysql_* function, mysqli_* is supporting both procedural & OO – ajreal Jan 01 '11 at 15:28
  • @Pekka I just think it looks too beginner. Plus this was something I did in college before I knew SQL injections. – blackbull77 Jan 01 '11 at 16:05

1 Answers1

2

I would prefer if you first made it secure. Add mysql_real_escape_string when reading the $_POST input variables. As it stands now, your code relies on magic_quotes to work at all.

As second step, you could make this code procedural. Devise a location_query() function or something. I see no immediate structure that would benefit to jump to "making it OOP".

mario
  • 138,064
  • 18
  • 223
  • 277