PHP/MySQL - How to display PHP generated text any where on a web page using PHP?
I want to display part of the script any where on the web page that generates the text to tell the user they submitted something.
How can I fix my script in-order to do this? And what part of my code should be changed?
Here is the part of the script that I want to display anywhere on a web page.
if (count($tags) == 1){
echo $tags[0] . " has been entered";
} else {
echo implode(", ", $tags) . " have been entered";
}
Here is the full script.
<?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);
开发者_如何学C
$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);
}
?>
As long as the page is PHP you can have any number of opening and closing tags:
<?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>
Please note that for security purposes (to prevent cross-site scripting) you should take measures to filter any page output that was generated by the user. Take a quick glance over at htmlentities.
精彩评论