在sql中通过表单向枚举字段添加数据

ep6jt1vc  于 2021-06-23  发布在  Mysql
关注(0)|答案(0)|浏览(307)

我目前有一个表单,其中提交值以将数据添加到我的表中。我有一个领域的 property_id “这是一个 ENUM . 每次尝试提交字段时,都会收到错误消息:

Fatal error: Uncaught mysqli_sql_exception: Data truncated for column 'property_id' at row 1 in ...

有没有什么方法可以把数据添加到 ENUM 通过我的表格填写字段?下面是我将表单连接到数据库的代码:

ini_set('display_errors', 1);
ini_set('log_errors', 1);
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

$property_id       = filter_input(INPUT_POST, 'property_id');
$firstline_address = filter_input(INPUT_POST, 'firstline_address');
$postcode          = filter_input(INPUT_POST, 'postcode');
$vacant            = filter_input(INPUT_POST, 'vacant');

if (!empty($property_id)) {
    if (!empty($postcode)) {
        $host       = "";
        $dbusername = "";
        $dbpassword = "";
        $dbname     = "";

        //new connection
        $conn = new mysqli($host, $dbusername, $dbpassword, $dbname);

        if (mysqli_connect_error()) {
            die('Connect Error (' . mysqli_connect_errno() . ')'
                . mysqli_connect_error());
        } else {
            $sql = "INSERT INTO property (property_id, firstline_address, postcode, vacant)
      values ('$property_id', '$firstline_address', '$postcode', '$vacant')";

            if ($conn->query($sql)) {
                echo "New property has been successfully added. Refresh page to view new property in the table below.";

            } else {
                echo "Error: ";
                //. $sql ."<br>".$conn->error;
            }
            $conn->close();
        }

    } else {
        echo "All fields are required!";
        die();
    }
} else {
    echo "Please fill out all fields to continue.";
    die();
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题