0

I'm currently trying to get into php and MySQL and i'm using this little project as a learning curve, but I've hit a bump that I can't get through. I have the following code that is accessing my MySQL database and I am currently trying to echo the data out using the following search function:

<?php
ob_start();
require("config.php");
ob_end_clean();

$req=$_REQUEST['workingDate'];
$req2=$_REQUEST['location'];

mysql_connect("XXXXXXXXXXX",$username,$password);
mysql_select_db($database) or die( "Unable to select database");

if ($req!="all" && $req2!="all")  $query="SELECT * FROM TrackerTable WHERE workingDate='$req' AND location1='$req2'";
else if($req=="all" && $req2!="all" ) $query="SELECT * FROM TrackerTable WHERE location1='$req2'";
else if($req!="all" && $req2=="all" ) $query="SELECT * FROM TrackerTable WHERE make='$req'";
else if($req=="all" || $req2=="all" ) $query="SELECT * FROM TrackerTable";

$result=mysql_query($query);
$num=mysql_numrows($result);

mysql_query($result);
mysql_close();

$i=0;

for ($i; $i < $num; $i++){
        $f12=mysql_result($result,$i,"workingDate");
        $f13=mysql_result($result,$i,"location1");
echo $f12." ".$f13."<br />";
}
?>

The problem I have is that whenever I try to search the database using the following html form:

<form method="post" action="searchFunction.php"  name="input" id="searchform">
    <label class="section">Search Options</label><br />
    <input name="workingDate" type="text" id="workingDate" placeholder="Search by Date">
    <input name="location1" type="text" id="location1" placeholder="Search by Location">
    <input name="submit" type="submit" id="add" value="Find!">
</form>

I get this error thrown at me:

 Warning: mysql_query() expects parameter 1 to be string, resource given in /XXXXXX/XXXXXX/XXXXX/XXXX/XXXXX.com/XXXXXX/searchFunction.php on line 20

I have no idea what could be causing the problem as it all seems fairly in place to me. Is there an obvious mistake i'm making or is it all just completely messed up? I've been looking at it that long that I can't tell what makes sense anymore!

Any help would be greatly appreciated. Thanks guys!

Aaron Lee
  • 1,146
  • 3
  • 14
  • 29
  • 1
    Lovely [Mysql injection](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)... – VeeeneX Feb 11 '15 at 15:06
  • Excuse me? Not following what you mean? – Aaron Lee Feb 11 '15 at 15:06
  • 2
    @VeeeneX The guy's new at this clearly, leaving aside the injection, he's using deprecated functions. Anyway, you're passing in $result as the query to be executed. It's not a valid query, in fact the $result would be the return value from the previous query. – Andrei P. Feb 11 '15 at 15:08
  • See [Bobby Tables](http://bobby-tables.com/) and [How can I prevent SQL-injection in PHP?](http://stackoverflow.com/q/60174) – mario Feb 11 '15 at 15:08
  • 2
    @mario Unfortunately Bobby tables wouldn't work :( You can't run 2 queries in one statement anymore. But as always xkcd is always relevant. – Andrei P. Feb 11 '15 at 15:09
  • Try inserting this `1' OR '1'='1'` into #workingDate – VeeeneX Feb 11 '15 at 15:09
  • @mario thanks, i'll take a look soon, but i'm more interested in getting it working first! – Aaron Lee Feb 11 '15 at 15:09

2 Answers2

1

You are sure that variable $query is defined? This because in the statement if/elseif is where you define the variable $query.

   // first define default value for your $query variable
   $query="SELECT * FROM TrackerTable";

   // then use your if/else statement
if ($req!="all" && $req2!="all") .....................

You have an error

$result=mysql_query($query);
$num=mysql_numrows($result);

mysql_query($result); // error here

mysql_close();

$result is not a string, is resource. mysql_query($result);

Check http://php.net/manual/en/function.mysql-query.php for details

nicolae-stelian
  • 344
  • 3
  • 12
1

You're trying to run your query twice:

$result=mysql_query($query);
                    ^^^^^^--- your SQL
mysql_query($result);
            ^^^^^^^---- result handle (aka resource) from previous call

The second query call is utterly irrelevant/useless. You're passing in the wrong argument to the call, and you're not capturing the return value from the query anyways. Even if it was running properly, you're throwing away the query results.

You then close your DB connection, so when you try to call mysql_result() later on, there's no more DB connection to fetch your results from.

And on top of all this, you're vulnerable to sql injection attacks.

In short, this code is a total disaster.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Ok, is there any resource you can put me toward in terms of doing this properly? I'm now at a lose as to how to proceed with it. – Aaron Lee Feb 11 '15 at 15:19