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 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制