【发布时间】:2014-04-07 08:50:10
【问题描述】:
我使用 PHP 在数据库中插入整数值。
我是这样使用的
$postcode = $_POST['postcode'];
$mysql_user_resultset = mysqli_query($con, "INSERT into user (postcode) VALUES ($postcode)");
我在数据库中有几个字段。像姓名,用户名等都定义为varchar,但邮政编码只定义为int。如果不输入邮政编码的值,则不会插入数据库
【问题讨论】:
-
可以在插入前加一个var_dump($postcode)吗?
-
从您的示例中:
$postcode = isset($_POST['postcode'])?$_POST['postcode']:0;,因此如果未设置邮政编码,它将被设置为 0。此外,您应该转义您传递的参数。 -
您可以使用
intval或is_numeric使您的邮政编码为整数。 -
@Prasanth
is_numeric将不确保某些东西是整数;它支持各种格式,例如'0.1e-05'。要仅检查数字,您需要ctype_digit() -
您对此有何疑问?为什么不先检查要输入的数据,然后然后插入呢?
标签: php