未定义的变量if(!($ result = mysql_query($ query,$ database)))

时间:2014-03-16 17:29:36

标签: php

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 {

我收到此错误代码如何修复?

  

注意:未定义的变量:第236行的C:\ xampp \ htdocs \ agjensituristike \ searchprova.php中的destinacioni
  注意:未定义的变量:第236行的C:\ xampp \ htdocs \ agjensituristike \ searchprova.php中的kategoria
  注意:未定义的变量:第248行的C:\ xampp \ htdocs \ agjensituristike \ searchprova.php中的查询
  无法执行查询!
  查询为空

1 个答案:

答案 0 :(得分:0)

我认为在您运行上述代码并且您要将表单提交到同一页面时,未设置 $_POST 变量。因此请将上述代码用条件包装,以检查 $_POST 变量是否设置为,

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.
}

希望这会有所帮助:)