简体   繁体   中英

PHP PDO INSERT INTO not inserting

I have a problem while inserting datas in MySQL database with PDO.
I have no error, it just seems to didn't have inserted datas in the table when I select them after execute the code with MySQL in terminal.

I've tried solutions in answers on stackoverflow, like wraping name and description in backticks, but it's still not working

Here is my sql columns:
sql 列

Here is the code:

$query = $pdo->prepare("INSERT INTO cities (`name`, departement, province, lat, long, `description`, poi_id) VALUES(`:name`, :departement, :province, :lat, :long, `:description`, :poi_id)");
    $query = $query->execute([
        "name" => $name,
        "departement" => $code_dpt,
        "province" => (int)$provinces[$departements[$code_dpt]],
        "lat" => $lat,
        "long" => $long,
        "description" => $description,
        "poi_id" => (int)$poi_id
    ]);
$query = $pdo->prepare("INSERT INTO cities (`name`, departement, province, lat, long, `description`, poi_id) VALUES(:name, :departement, :province, :lat, :long, :description, :poi_id)");
$query->execute([
        ':name' => $name,
        ':departement' => $code_dpt,
        ':province' => (int)$provinces[$departements[$code_dpt]],
        ':lat' => $lat,
        ':long' => $long,
        ':description' => $description,
        ':poi_id' => (int)$poi_id
    ]);

use placeholder in execute function.

I've found the solution. First, I've enabled errors like this:

$pdo = new PDO('mysql:host=localhost;dbname=bpfmgr', 'root', '', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));

I was using a property named long for longitude, but it's a MySQL reserved keyword, so it mades errors. I've added backticks to wrap long and that's working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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