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

问题描述 投票:1回答:3

我有这个代码:

<?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'没有'ht'(几乎就像它们彼此分开一样)

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

未经测试的代码:

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>";

0
投票

你不能在div元素中使用p元素,并且你的代码打印数据库结果有一些错误,所以我建议: -

<!--the results-->

<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 '<p style="background-color:blue;padding-bottom:5%">'.$query_execute ['Title'].'</p><br/>' ;


    }

}

else
{
    echo"Sorry, something went wrong...";
}


?> 
</div>

-1
投票

因为你做错了。尝试以下方法

while($query_execute=mysql_fetch_assoc($is_query_run) )
    {
        echo '<p style="background-color:blue;padding-bottom:5%">'.$query_execute ['Title'].'</p><br>' ;
    }
© www.soinside.com 2019 - 2024. All rights reserved.