无法将php字符串变量存储到mysql表中

无法将php字符串变量存储到mysql表中,php,mysql,Php,Mysql,地址存储在表中,但国家和城市不存储在表中。两列都是varchar类型 将显示NULL值 没有报告错误 为了测试,我将':country'=>$country,更改为':country'=>“USA”,它可以工作。您需要绑定参数- error_reporting(E_ALL); $address = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; $ip = getenv('REMOTE_ADDR'); $plugin='http://

地址
存储在表中,但
国家
城市
不存储在表中。两列都是
varchar
类型

将显示
NULL

没有报告错误


为了测试,我将
':country'=>$country
,更改为
':country'=>“USA”
,它可以工作。

您需要绑定参数-

error_reporting(E_ALL);
$address = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";    
$ip = getenv('REMOTE_ADDR');
$plugin='http://www.geoplugin.net/php.gp?ip='.$ip;
$geo = unserialize(file_get_contents($plugin)); 
$country = $geo['geoplugin_countryName'];
$city = $geo['geoplugin_city'];

var_dump($country) // string(6)"Serbia"
var_dump($city) // string(8)"Belgrade"

try
{
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO visits (country,city,address) VALUES (:country,:city,:address)";
$q = $conn->prepare($sql);
$q->execute(array(':country'=>$country,
                  ':city'=>$city,
                  ':address'=>$address
                  ));
} catch ( PDOException $exception )
{
    echo "PDO error :" . $exception->getMessage();
}

为什么要使用“var_dump”。这里并没有让老师认为这是引号的问题…@maysaghira,你在我的代码中看到了这个问题吗?你可以通过PDO准备的语句将一个关联数组传递到
execute
函数中,检查文档(例2)。这向我表明$country和$city并没有按照你的预期进行设置。
$sql = "INSERT INTO visits (country,city,address) VALUES (:country,:city,:address)";
$sql->bindParam(':country', $country, PDO::PARAM_STR);
$sql->bindParam(':city', $city, PDO::PARAM_STR);
$sql->bindParam(':address', $address, PDO::PARAM_STR);
$sql->execute();