如何将两个相似的 php 代码合并为一个?

我得到以下代码:


<?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 注入安全性的反馈我的代码将受到欢迎!


这也是我在这里提出的第一个问题,所以如果您需要我详细说明某些内容,请告诉我。


翻过高山走不出你
浏览 96回答 2
2回答

千巷猫影

您可以通过在语句||中使用运算符来实现if,如下所示,并使用三元运算符来获取用户是通过链接还是通过单击访问项目:<?php// If user access item through linkif(isset($_POST["v"]) || isset($_POST["itemid"])) {&nbsp; &nbsp; require "connect.php";&nbsp; &nbsp; //user ternary operator to check whether user accessed the item through link or click&nbsp; &nbsp; $var = isset($_POST['v'])?$_POST['v']:$_POST['itemid'];&nbsp; &nbsp; $sql = "SELECT * FROM videos WHERE videoID=?;";&nbsp; &nbsp; $stmt = mysqli_stmt_init($conn);&nbsp; &nbsp; if (!mysqli_stmt_prepare($stmt, $sql)) {&nbsp; &nbsp; &nbsp; &nbsp; echo'Sql Error';&nbsp; &nbsp; &nbsp; &nbsp; exit();&nbsp; &nbsp; }&nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; mysqli_stmt_bind_param($stmt, "i", $var);&nbsp; &nbsp; &nbsp; &nbsp; mysqli_stmt_execute($stmt);&nbsp; &nbsp; &nbsp; &nbsp; $result = mysqli_stmt_get_result($stmt);&nbsp; &nbsp; &nbsp; &nbsp; if (($row = mysqli_fetch_assoc($result)) && !preg_match("/[a-zA-Z]/", $var)) { ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="img" style="background-image: url('<?php echo $row['link']; ?>');"></div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <center><h1>Title <?php echo $row['Title']; ?> exists</h1></center>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php exit();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else { ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <center><h1>Title does not exist</h1></center>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php exit();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}?>

阿波罗的战车

是的,除了 POST 参数之外,两者似乎都很相似,为什么不使用一个可以处理两个 isset 条件的函数呢!有点像这样,function funcName($param){&nbsp; &nbsp;require "connect.php";//$v = $_POST['v'];$sql = "SELECT * FROM videos WHERE videoID=?;";$stmt = mysqli_stmt_init($conn);if (!mysqli_stmt_prepare($stmt, $sql)) {&nbsp; &nbsp; echo'Sql Error';&nbsp; &nbsp; exit();}else {&nbsp; &nbsp; mysqli_stmt_bind_param($stmt, "i", $param);&nbsp; &nbsp; mysqli_stmt_execute($stmt);&nbsp; &nbsp; $result = mysqli_stmt_get_result($stmt);&nbsp; &nbsp; if (($row = mysqli_fetch_assoc($result)) && !preg_match("/[a-zA-Z]/", $v)) { ?>&nbsp; &nbsp; &nbsp; &nbsp; <div class="img" style="background-image: url('<?php echo $row['link']; ?>');"></div>&nbsp; &nbsp; &nbsp; &nbsp; <center><h1>Title <?php echo $row['Title']; ?> exists</h1></center>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <?php exit();&nbsp; &nbsp; }&nbsp; &nbsp; else { ?>&nbsp; &nbsp; &nbsp; &nbsp; <center><h1>Title does not exist</h1></center>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <?php exit();&nbsp; &nbsp; }}}然后,&nbsp; &nbsp; if(isset($_POST["v"])) {&nbsp; &nbsp; &nbsp;$param = $_POST["v"];&nbsp; &nbsp; &nbsp;funcName($param);}if(isset($_POST["itemid"])) {&nbsp;$param = $_POST["itemid"];&nbsp; &nbsp; &nbsp;funcName($param);}
打开App,查看更多内容
随时随地看视频慕课网APP