我得到以下代码:
<?php
// If user access item through link
if(isset($_POST["v"])) {
require "connect.php";
$v = $_POST['v'];
$sql = "SELECT * FROM videos WHERE videoID=?;";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo'Sql Error';
exit();
}
else {
mysqli_stmt_bind_param($stmt, "i", $v);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
if (($row = mysqli_fetch_assoc($result)) && !preg_match("/[a-zA-Z]/", $v)) { ?>
<div class="img" style="background-image: url('<?php echo $row['link']; ?>');"></div>
<center><h1>Title <?php echo $row['Title']; ?> exists</h1></center>
<?php exit();
}
else { ?>
<center><h1>Title does not exist</h1></center>
<?php exit();
}
}
}
// If user clicks on item
if(isset($_POST["itemid"])) {
require "connect.php";
$itemid = $_POST['itemid'];
$sql = "SELECT * FROM videos WHERE videoID=?;";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo'Sql Error';
exit();
}
else {
mysqli_stmt_bind_param($stmt, "i", $itemid);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
if (($row = mysqli_fetch_assoc($result)) && !preg_match("/[a-zA-Z]/", $itemid)) { ?>
<div class="img" style="background-image: url('<?php echo $row['link']; ?>');"></div>
<center><h1>Title <?php echo $row['Title']; ?> exists</h1></center>
<?php exit();
}
else { ?>
<center><h1>Title does not exist</h1></center>
<?php exit();
}
}
}
正如你所看到的两个 Isset if 非常相似,我想知道是否有一个解决方案将两者结合起来,这样我就不必两次编写相同的结果,如果有人想提供有关 sql 注入安全性的反馈我的代码将受到欢迎!
这也是我在这里提出的第一个问题,所以如果您需要我详细说明某些内容,请告诉我。
千巷猫影
阿波罗的战车