donglvlao8367 2018-08-11 11:30
浏览 83
已采纳

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

I have this code:

<?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>

It is basically an SQL code to retrieve data from a database in an order. Each record when retrieved should be stylised (beautified using CSS). It works well except that the first and the last result are malformed. e.g. the first result is shown without any CSS styling (while the second, third etc are ok) and the last is shown with CSS styling but the result itself is not there.

Screenshot:

enter image description here

As you can see, the first 'ht' has no blue styling and the last has blue styling WITHOUT 'ht' (almost as if they separated from each other)

  • 写回答

3条回答 默认 最新

  • duanbo19834 2018-08-11 17:38
    关注
    • mysql_ functions are deprecated from php5.5.
    • I will recommend, while upgrading to mysqli, that you learn about object-oriented syntax because it is less verbose than procedural syntax.
    • It is simple to nominate your targeted database while connecting, so roll that all into one line.
    • You are building a database query with user-supplied data, so a prepared statement is called for. When there is no submitted value you could avoid a prepared statement, but to reduce code complexity, I'm writing a prepared statement to handle all occurrences.
    • When designing your SELECT clause, if you don't need all of the columns for your task, only request the columns that you will use.
    • You should try to avoid inline styling, by writing style declarations in the <head> tag or even in a separate file. This will improve the overall readability and maintainability of your code.
    • Writing error checks into your code will be immensely helpful when you need to debug your work -- so get into the habit of doing it all of the time. As a matter of security, never show exact error details to your users.
    • $query_execute is not a great choice for a variable that contains data from a resultset row. It doesn't "execute" anything, so try not to confuse yourself and future code readers (humans) with such terminology.
    • As for you specific html issue, try to eliminate all unnecessary DOM elements (tags) in your output. It seems that you only need one containing div for your p elements.

    Untested Code:

    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>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题