简体   繁体   English

基于表单选择的PHP列表:MDB2错误:语法错误

[英]PHP List based on Form Selection: MDB2 Error: syntax error

I'm creating a table that outputs a list of country details based on a form selection but I keep getting this error: MDB2 Error: syntax error . 我正在创建一个表格,该表格根据表单选择输出国家/地区详细信息列表,但我不断收到此错误: MDB2 Error: syntax error How can I fix this type of error? 如何解决这种类型的错误?

Here is my code: 这是我的代码:

<?php
    $db =& MDB2::connect($dsn); 
    if(PEAR::isError($db)){ 
        die($db->getMessage());
    }
    $table_name="country";
    $db->setFetchMode(MDB2_FETCHMODE_ASSOC);

    $country_id = mysql_real_escape_string($_GET["country_id"]);

    // collect values from a form sent with method=get
    $gdp = mysql_real_escape_string($_GET["gdp"]);
    $population = mysql_real_escape_string($_GET["population"]);
    $country_name = mysql_real_escape_string($_GET["country_name"]);
    $gold = mysql_real_escape_string($_GET["gold"]);
    $bronze = mysql_real_escape_string($_GET["bronze"]);
    $silver = mysql_real_escape_string($_GET["silver"]);
    $total = mysql_real_escape_string($_GET["total"]);

    $sql = "SELECT * FROM $country WHERE country_id='$country_id'";

    $res =& $db->query($sql);      //MDB2 Error: syntax error

    if (PEAR::isError($res)) {
        die($res->getMessage());    //error printed here
    }
?>

In your line "SELECT * FROM $country WHERE country_id='$country_id'" , the variable $country is not defined, so it will render as eg "SELECT * FROM WHERE country_id='1'" , hence the SQL error. 在您的"SELECT * FROM $country WHERE country_id='$country_id'" ,未定义变量$country ,因此它将呈现为"SELECT * FROM WHERE country_id='1'" ,因此出现SQL错误。

It looks like you meant $table_name , which has the value 'country' . 看起来您的意思是$table_name ,其值为 'country'

Since that appears to be defined just a few lines up, it would probably make more sense to just write it in the SQL statement directly, rather than having a variable, but maybe you have plans for that variable later... 由于似乎只是几行而已定义,因此直接在SQL语句中编写而不是使用变量可能会更有意义,但也许以后会有该变量的计划...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM