Javascript 突出显示PHP SQL的语法

Javascript 突出显示PHP SQL的语法,javascript,php,html,sql,syntax,Javascript,Php,Html,Sql,Syntax,我有一个搜索数据库的页面。我希望获得用户在搜索框中输入的数据,以便在搜索结果中突出显示 来自用户的数据存储在变量“$criteria”中,我打算突出显示该变量的所有内容 我的PHP如下所示: <?php session_start(); if ($_SESSION['loggedin'] != 1) { header("Location: ../"); } include("../php_includes/connection.php"); $queryErrorMessage

我有一个搜索数据库的页面。我希望获得用户在搜索框中输入的数据,以便在搜索结果中突出显示

来自用户的数据存储在变量“$criteria”中,我打算突出显示该变量的所有内容

我的PHP如下所示:

<?php 
session_start();
if ($_SESSION['loggedin'] != 1) {
    header("Location: ../");
}
include("../php_includes/connection.php");

$queryErrorMessage = "";
?>
<html>
<head>
    <title>Search Tasks</title>

    <link rel="stylesheet" href="../css/bootstrap.min.css">
    <script src="../js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="../css/style.css">
</head>

<body>
    <?php include("header.php"); ?>
    <?php include("sidebar.php"); ?>
    <div class="row">
        <div class="col-md-9">
            <div class="well well-lg">
                <h3>Search Tasks</h3>
                <hr>

                <form method="post" action="">
                <div class="input-group">
                    <input name="criteria" type="text" class="form-control" placeholder="What would you like to search for?...">
                    <div class="input-group-btn">
                      <button class="btn btn-default" type="submit">
                        Submit
                      </button>
                    </div>
                  </div>
                </form>



 <?php 
            //Run section of code if the POST criteria is provided for "criteria"
            if(isset($_POST['criteria'])){
                $criteria = mysqli_real_escape_string($db,$_POST['criteria']);
                //If the user does not provide criteria to search, an error will be displayed.
                if($criteria == ""){
                    $queryErrorMessage = "You did not provide any criteria";
                    GoTO errorMsg;
                }
                //Prepare sql statement checking each column with the criteria that is provided.
                $sql = "SELECT * FROM taskinfo WHERE clientCompany LIKE '%".$criteria."%' OR clientName LIKE '%".$criteria."%' OR email LIKE '%".$criteria."%' OR contactNo LIKE '%".$criteria."%' OR details LIKE '%".$criteria."%'";

                //Run sql query storing the records in $result
                if($result = mysqli_query($db, $sql)){
                    if(mysqli_num_rows($result)>0){
                        //Get each row and put it into the array $row
                        echo '<h4>Displaying results for search criteria: <b>'.$criteria.'</b></h4>';
                        while($row = mysqli_fetch_array($result)){
                            $queryErrorMessage = "";
                            //Display the query results
                            ?>

                            <div class="panel panel-default">
                                <div class="panel-body">
                                    <h6><b>Task Details:</b></h6>
                                    <?php echo $row['details'];?>
                                    <h6><b>Task Information</b></h6>
                                    <ul>
                                        <li>Client Name: <?php echo $row['clientName'];?></li>
                                        <li>Email: <a href="mailto:<?php echo $row['email'];?>"><?php echo $row['email'];?></a></li>
                                        <li>Phone: <?php echo $row['contactNo'];?></li>
                                        <li>Posted by <b><?php echo $row['postedBy'];?></b></li>
                                        <li>Posted On: <b><?php echo $row['timeAdded']; ?></b></li>
                                    </ul>
                                </div>
                            </div>
            <?php   
                        }
                    } else {
                        $queryErrorMessage = "No records found with your criteria.";
                    }
                } else {
                    $queryErrorMessage = "Failed to perform query.";
                }
            }

            //Check if there is an error message
            //If true, echo the error to the user.
            //
            //Section of code is called errorMsg to 
            //Allow it to be called in the code above.
            errorMsg:
            if($queryErrorMessage != ""){
                echo 'An Error Occurred: ';
                echo $queryErrorMessage;
            }
            ?>


            </div>
        </div>
    </div>
</body>
</html>

搜索任务
搜索任务

提交 任务详细信息: 任务信息
  • 客户名称:
  • 电邮:
  • 电话:
  • 邮寄人
  • 张贴于:
如果有人能帮忙,我们将不胜感激。

这可能会有所帮助

    <?php echo preg_replace_callback('/'.$criteria.'/i', function($matches){
        return '<strong>' . $matches[0] . '</strong>';
    }, $row['clientName']); ?>


这会使内容中的搜索关键字变为粗体

您是否从数据库中获得正确的值?为什么要对所有这些列使用LIKE?这没有道理。顺便说一句,你从来没有说过问题出在哪里。你也没有检查错误,一切都很好。我知道我没有检查错误,它仍有待实现。在比较来自用户和数据库的数据时,有些列我不想使用,因此我指定了它们。谢谢!那么这就是我在呼应数据的地方了?