2 回答
TA贡献1829条经验 获得超7个赞
您可以通过在语句||中使用运算符来实现if,如下所示,并使用三元运算符来获取用户是通过链接还是通过单击访问项目:
<?php
// If user access item through link
if(isset($_POST["v"]) || isset($_POST["itemid"])) {
require "connect.php";
//user ternary operator to check whether user accessed the item through link or click
$var = isset($_POST['v'])?$_POST['v']:$_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", $var);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
if (($row = mysqli_fetch_assoc($result)) && !preg_match("/[a-zA-Z]/", $var)) { ?>
<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();
}
}
}
?>
TA贡献1862条经验 获得超6个赞
是的,除了 POST 参数之外,两者似乎都很相似,为什么不使用一个可以处理两个 isset 条件的函数呢!
有点像这样,
function funcName($param){
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", $param);
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(isset($_POST["v"])) {
$param = $_POST["v"];
funcName($param);
}
if(isset($_POST["itemid"])) {
$param = $_POST["itemid"];
funcName($param);
}
- 2 回答
- 0 关注
- 95 浏览
添加回答
举报