0

我想知道使用 $_POST['name'] 时的数据类型是什么,假设我正在绑定参数:

$unsafe_variable1=$_POST['name'];
$unsafe_variable2=$_POST['email'];
$unsafe_variable3=$_POST['city'];

$stmt=$con->prepare("INSERT INTO mytable (name, email, city) VALUES ('$bname', '$email', '$city')");

$obj->bind_param('sss', $unsafe_variable1, $unsafe_variable3, $unsafe_variable3);

我的猜测是字符串的's'。


第二件事是当我使用 sss 时会收到警告:

Number of variables doesn't match number of parameters in prepared statement

这让我觉得“s”可能不是正确的数据类型。:o

4

2 回答 2

0

$_POST 和 $_GET 参数将始终是字符串。

您可以使用 gettype() 来检查,

echo gettype($_POST['variable_from_form']);

仅举一个例子,您将看到它将识别1为字符串、1.11字符串、any text字符串和true/false字符串而不是布尔值。那是因为 php 使用 PHP 的类型杂耍系统将非字符串值转换为字符串。

于 2016-04-07T15:13:45.233 回答
-1

请注意,您正在直接使用变量:

[...] VALUES ('$bname', '$email', '$city')

只需将其更改为:

[...] VALUES (?, ?, ?)

POST 变量通常是字符串。因此,您的sss绑定是完美的。为了避免将来出现问题,您可以:

$stmt = $con->prepare("INSERT INTO mytable (name, email, city) VALUES (:name, :email, :city)");
$params['name'] = $_POST['name'];
$params['email'] = $_POST['email'];
$params['city'] = $_POST['city'];
$stmt->execute($params);

它将自动检测变量的类型并绑定它。

于 2013-03-11T21:28:04.560 回答