我想在网页上的任何地方显示脚本的一部分,生成文本,告诉用户他们提交了一些东西。
我怎样才能修改我的脚本才能做到这一点?我的代码的哪一部分应该更改?
这是脚本的一部分,我想显示在网页的任何地方。

if (count($tags) == 1){
        echo $tags[0] . " has been entered";
} else {
        echo implode(", ", $tags) . " have been entered";
}

这是完整的剧本。
<?php
if (isset($_POST['submitted'])) {
    $mysqli = new mysqli("localhost", "root", "", "sitename");
    $dbc = mysqli_query($mysqli,"SELECT questions_tags.*, tags.* FROM questions_tags, tags");
    if (!$dbc) {
        print mysqli_error($mysqli);
    }

    $page = '3';

    $tag = mysqli_real_escape_string($mysqli, $_POST['tag']);

    $mysqli = new mysqli("localhost", "root", "", "sitename");
    $dbc = mysqli_query($mysqli,"SELECT questions_tags.*, tags.* FROM questions_tags INNER JOIN tags ON tags.id = questions_tags.tag_id WHERE questions_tags.users_questions_id='$page'");

    if(mysqli_num_rows($dbc) >= 0){

        if (isset($_POST['tag'])) {
                $tags = explode(",", $_POST['tag']);

                for ($x = 0; $x < count($tags); $x++){
                        $mysqli = new mysqli("localhost", "root", "", "sitename");
                        $clean_url = mysqli_real_escape_string($mysqli, $page);

                        $query1 = "INSERT INTO tags (tag) VALUES ('" . $tags[$x] . "')";

                        if (!mysqli_query($mysqli, $query1)) {
                                print mysqli_error($mysqli);
                                return;
                        }

                        $mysqli = new mysqli("localhost", "root", "", "sitename");
                        $dbc = mysqli_query($mysqli,"SELECT id FROM tags WHERE tag='" . $tags[$x] . "'");


                        if (!$dbc) {
                                print mysqli_error($mysqli);
                        }  else {
                                while($row = mysqli_fetch_array($dbc)){
                                        $id = $row["id"];
                                }
                        }
                        $query2 = "INSERT INTO questions_tags (tag_id, users_questions_id) VALUES ('$id', '$page')";

                        if (!mysqli_query($mysqli, $query2)) {
                                print mysqli_error($mysqli);
                                return;
                        }
                }

                        if (count($tags) == 1){
                                echo $tags[0] . " has been entered";
                        } else {
                                echo implode(", ", $tags) . " have been entered";

                        }
        }

        if (!$dbc) {
                print mysqli_error($mysqli);
        }
    }
    mysqli_close($mysqli);
}
?>

最佳答案

只要页面是PHP,就可以有任意数量的打开和关闭标记:

<?php
$count = count($tags);
if ($count == 0) {
   $str = 'No tags have been entered.';
else if ($count == 1) {
    $str = htmlentities($tags[0]) . " has been entered";
} else {
    $str = htmlentities(implode(", ", $tags)) . " have been entered";
}
?>

<p>HTML code here</p>
<?php echo $str; ?>
<p>More HTML</p>

请注意,为了安全起见(防止跨站点脚本),您应该采取措施筛选用户生成的任何页面输出。快速浏览一下htmlentities

关于php - PHP/MySQL-如何使用PHP在网页上的任何位置显示PHP生成的文本?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1873210/

10-13 00:54