-3
236 $query = "SELECT kategoria, destinacioni FROM pushimet WHERE (kategoria like '$_POST[kategoria]' and destinacioni like '$_POST[destinacioni]' )  ";
237 
238  // Connect to MySQL
239 if ( !( $database = mysql_connect( "localhost",
240 "root", "" ) ) )
241
242 die( "Could not connect to database </body></html>" );
243
244
245 if (!mysql_select_db( "phpdbadmin", $database ) )
246 die( "Could not open phpdbadmin database </body></html>" );
247
248 if (!( $result = mysql_query( $query, $database ) ) )
249 {

I get this error code how to fix that?

Notice: Undefined variable: destinacioni in C:\xampp\htdocs\agjensituristike\searchprova.php on line 236
Notice: Undefined variable: kategoria in C:\xampp\htdocs\agjensituristike\searchprova.php on line 236
Notice: Undefined variable: query in C:\xampp\htdocs\agjensituristike\searchprova.php on line 248
Could not execute query!
Query was empty

kba
  • 19,080
  • 5
  • 59
  • 87
  • 1
    Those $_POST variables obviously aren't set. You need to check if they are set before you try to use them. – John Conde Mar 16 '14 at 17:30
  • 3
    [Please, don't use `mysql_*` functions in new code](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [red box](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). **You are also wide open to [SQL injections](http://stackoverflow.com/q/60174)** – John Conde Mar 16 '14 at 17:31
  • 2
    You're also using `LIKE` poorly. You won't get expected results the way you are using it. You really need to rethink all of this code. – John Conde Mar 16 '14 at 17:32

1 Answers1

0

I assume $_POST variable is not set when you running the above code and you are submitting a form to the same page. So wrap the above code with a condition to check whether the $_POST variable is set like,

if(!empty($_POST))
{

    $query = "SELECT kategoria, destinacioni FROM pushimet WHERE (kategoria like '$_POST[kategoria]' and destinacioni like '$_POST[destinacioni]' )  ";

     // Connect to MySQL
    if ( !( $database = mysql_connect( "localhost",
    "root", "" ) ) )

    die( "Could not connect to database </body></html>" );


    if (!mysql_select_db( "phpdbadmin", $database ) )
    die( "Could not open phpdbadmin database </body></html>" );

    if (!( $result = mysql_query( $query, $database ) ) )
    {

    # ... rest of you code which need the variables from $_POST.
}

Hope this helps :)

Ijas Ameenudeen
  • 8,739
  • 3
  • 38
  • 51