>我有一段代码,将一些基本的用户选择存储在mysql表中。
$isACompany = $_POST['isACompany'];
$hasDifBilling = $_POST['hasDifBilling'];
$stmt = $link->prepare("
INSERT INTO user_info SET
id='',
isACompany = ?,
hasDifBilling = ?'
");
if (!$stmt)
{
$error = "{$link->errno} : {$link->error}";
include "$docRoot/html/main/error.html.php";
exit();
}
if (!$stmt->bind_param("ii", $isACompany, $hasDifBilling))
{
$error = "{$stmt->errno} : {$stmt->error}";
include "$docRoot/html/main/error.html.php";
exit();
}
if (!$stmt->execute())
{
$error = "{$stmt->errno} : {$stmt->error}";
include "$docRoot/html/main/error.html.php";
exit();
}
给定该代码,在这种情况下,我将如何获取新创建的 id?
我还有另一个与此代码有关的问题,尽管它本身不是问题的一部分......由于前两个变量绘制了这两个帖子的值,并且看到这些值被硬编码到 html 中并通过复选框("1"或"为 0)选择。在将信息添加到数据库之前,我是否仍需要清理它们?
正如 Michael 正确指出的那样,可以使用 $stmt->insert_id
属性检索您插入的行的 ID。
这个问题更重要的部分是第二部分,答案是一个大胖子,是的,你必须逃避来自用户输入的一切!仅仅因为您将其"硬编码"到HTML中,并不意味着用户无法对其进行操作。有一百万零一种方法可以做到这一点 - Javascript注入和"欺骗"HTTP请求仅举两个例子。
您必须转义来自客户端的任何内容。总是。故事结束。