PHP List based on Form Selection: MDB2 Error: syntax error

谁都会走 提交于 2019-12-12 03:49:03

问题


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

回答1:


In your line "SELECT * FROM $country WHERE country_id='$country_id'", the variable $country is not defined, so it will render as e.g. "SELECT * FROM WHERE country_id='1'", hence the SQL error.

It looks like you meant $table_name, which has the value '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...



来源:https://stackoverflow.com/questions/15536365/php-list-based-on-form-selection-mdb2-error-syntax-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!