php - 嵌套的 html 元素显示意外的样式

标签 php html css styling

我有这个代码:

<?php $search =$_GET['search']?>

<p>Displaying search results for : <font style="font-weight:bolder;font-style:italic"><?php echo $search?></font></p>

<?php
$mysql_host='localhost';
$mysql_user='root';
$mysql_password='can't see my password..sorry';

mysql_connect($mysql_host,$mysql_user,$mysql_password);
@mysql_select_db('galaxall');
?>

<!--the results-->

<p style="background-color:rgb(250,250,250)">
<div style="background-color:rgb(250,250,250);padding-bottom:3%;margin-right:20%">
<?php
$hello='hello everybody!!!';

$query="SELECT * FROM `galaxall_uploads` WHERE Title LIKE '%$search%' ";
if($is_query_run=mysql_query($query) )
{  
    while($query_execute=mysql_fetch_assoc($is_query_run) )
    {
        echo $query_execute ['Title'].'<br>'.''.'<p style="background-color:blue;padding-bottom:5%">'.'<br>' ;
    }
} else {
    echo"Sorry, something went wrong...";
}
?> 
</p>

它基本上是一个 SQL 代码,用于按顺序从数据库中检索数据。检索到的每条记录都应该风格化(使用 CSS 美化)。除了第一个和最后一个结果格式不正确外,它运行良好。例如第一个结果显示时没有任何 CSS 样式(而第二个、第三个等都可以),最后一个结果显示有 CSS 样式但结果本身不存在。

截图:

enter image description here

如您所见,第一个“ht”没有蓝色样式,最后一个有蓝色样式但没有“ht”(几乎就像它们彼此分开一样)

最佳答案

  • mysql_函数 are deprecated from php5.5 .
  • 我会推荐,同时升级到mysqli ,您将了解面向对象的语法,因为它比过程语法更简洁。
  • 在连接时指定您的目标数据库很简单,因此将所有这些都集中在一行中。
  • 您正在使用用户提供的数据构建数据库查询,因此需要准备好的语句。当没有提交值时,您可以避免准备语句,但为了降低代码复杂性,我正在编写准备语句来处理所有事件。
  • 在设计您的 SELECT 时子句,如果您的任务不需要所有列,则只请求您将使用的列。
  • 您应该尽量避免内联样式,方法是在 <head> 中编写样式声明。标签甚至在一个单独的文件中。这将提高代码的整体可读性和可维护性。
  • 当您需要调试您的工作时,在您的代码中写入错误检查将非常有用——因此请养成一直这样做的习惯。出于安全考虑,切勿向您的用户显示确切的错误详细信息。
  • $query_execute对于包含来自结果集行的数据的变量来说不是一个很好的选择。它不会“执行”任何东西,因此请尽量不要将您自己和 future 的代码阅读者(人类)与此类术语混淆。
  • 至于您的特定 html 问题,请尝试消除输出中所有不必要的 DOM 元素(标签)。看来你只需要一个包含 div 的为你的p元素。

未经测试的代码:

echo "<div style=\"background-color:rgb(250,250,250); padding-bottom:3%; margin-right:20%\">";
if (!$conn = new mysqli("localhost", "root", "", "galaxall")) {
    echo "<font style=\"color:red;\">Database Connection Error</font>"; // don't show this to the public--> $conn->connect_error;
} else {
    if (!isset($_GET['search'])) {
        $search = "";
        echo "<p>No search keyword received.  Showing all rows<p>";
    } else {
        $search = strip_tags($_GET['search']);  // perform whatever sanitizing/filtering/preparation techniques in this line
        echo "<p>Displaying search results for: <font style=\"font-weight:bolder; font-style:italic\">$search</font></p>";
    }
    if (!$stmt = $conn->prepare("SELECT Title FROM galaxall_uploads WHERE Title LIKE ? ORDER BY Title")) {
        echo "<font style=\"color:red;\">Prepare Syntax Error</font>"; // don't show this to the public--> $conn->error;
    } else {
        if (!$stmt->bind_param("s", "%$search%") || !$stmt->execute() || !$result = $stmt->get_result()) {
            echo "<font style=\"color:red;\">Statement Error</font>"; // don't show this to the public--> $stmt->error;
        } elseif (!$result->num_rows){
            echo "<p>No matches in the database for \"$search\"</p>";
        } else {
            while ($row = $result->fetch_assoc()) {
                echo "<p style=\"background-color:blue; padding-bottom:5%\">{$row['Title']}</p>";
            }
        }
        $stmt->close();
    }
    $conn->close();
}
echo "</div>";

关于php - 嵌套的 html 元素显示意外的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51799217/

相关文章:

php - 无法在 HTML 中正确使用 <img src =""onclick ='window.open(' anotherpage.html' ,'_blank");'/> 属性

javascript - 使用 FormData Javascript 发送 bool 值 - Vuejs + Laravel 应用程序

php - Ajax 分页不会更改事件链接

javascript - YouTube API nextPageToken

jquery - 居中 JQuery 无法正常工作

javascript - 无法将 div 的显示样式设置为在事件监听器中阻塞

php - SELECT 多行 WHERE 匹配两个条件

html - CSS :before & position: relative issue in IE

javascript - 如何使用带有身份验证的 url 从 javascript 解析 JSON

javascript - Jquery 选择的插件样式在 Ajax 调用后丢失