首页 > 解决方案 > 我在我的代码数组中不断收到此错误( [0] => 01000 [1] => 1265 [2] => 第 1 行的列 'lon' 的数据被截断)

问题描述

这是我的代码,错误不断出现。一切都符合表格,但我仍然得到同样的错误。我已经尝试过,在 sql 表中,lon 和 lan 是基于数字的,所以我不知道这是否是问题所在:

<?php

$n = $_POST["name"];
$t = $_POST["type"];
$c = $_POST["country"];
$r = $_POST["region"];
$lon = $_POST["longitude"];
$lat = $_POST["latitude"];
$des = $_POST["description"];

$conn = new PDO("mysql:host=localhost;dbname=hjaved;", "hjaved","queeTh1d");
$conn->query("INSERT INTO pointsofinterest (name, type, country, 
    region, lon, lat, description) VALUES ('$n', '$t', '$c', '$r', 
    '$lon',
    '$lat', '$des')");

print_r($conn->errorinfo());
?>

这是 shcema 表:
ID 名称 类型 国家 地区 lon lat 描述

标签: php

解决方案


这是一个例子,如何摆脱 mysql 注入,使用准备 + 执行方法(来自 PDO 对象)

<?php
$conn = new PDO("mysql:host=localhost;dbname=hjaved;", "hjaved","queeTh1d");
$conn->prepare("
    INSERT INTO pointsofinterest (
        `name`,
        `type`,
        `country`,
        `region`,
        `lon`,
        `lat`,
        `description`
    ) VALUES (
        :name,
        :type,
        :country,
        :region,
        :lon,
        :lat,
        :description
    );");
$conn->execute(array(
    ':name' => $_POST["name"],
    ':type' => $_POST["type"],
    ':country' => $_POST["country"],
    ':region' => $_POST["region"],
    ':lon' => $_POST["longitude"],
    ':lat' => $_POST["latitude"],
    ':description' => $_POST["description"],
));

print_r($conn->errorinfo());

推荐阅读