搜索尝试语法错误

search attempt syntax error

我不知道我这样做是否正确。我正在尝试从我的搜索中输出结果,但是我不确定这是否正确完成。另外我收到了这个错误。

Parse error: syntax error, unexpected '''' (T_CONSTANT_ENCAPSED_STRING) in /usr/local/www/project/OlegarioJW/largecoursework/tobakuhome.php on line 152

此外,我打算将其放入 div 以及我的两个结果放入 <a> 标记中,因为它们将具有链接并且它们都在一个变量中。这是怎么做到的?

下面是我的代码:

<html>
<header></header>

<body>

<?php
    $serverName = "example.com";
    $dbName = "abc";
    $user = "abc";
    $pass = "abc";

    $connection = mysqli_connect($serverName, $user, $pass, $dbName);
    if (!$connection){
        die("Connection failure" . mysqli_connect_error());
    }

?>

<form id ="search" action="tobakuhome.php" method="post">
        <input type="text" name="search" placeholder="Search Game titles here"/>
        <input type="submit" value="Go" />
</form>

<?php print("$output"); ?>

<?php

$output = '';
if (isset($_POST['search'])){
    $searching = $_POST['search'];
    $searching = preg_replace("#[^0-9a-z]#i","", $searching);
} 

    $query = "SELECT * FROM Software WHERE name LIKE '%searching%' OR description LIKE '%$searching%' OR exclusivity LIKE '%$searching%' OR format LIKE '%$searching%'";
    $result = mysqli_query($connection, $query) or die("no results found");
    $count = mysqli_num_rows($query);
    if($count ==0){
        $output = 'Sorry, No results was found.';

    }else{
        while($row = mysqli_fetch_array($query)){
            $Tname = $row['Name'];
            $Tdes = $row['description'];
            $Timg = $row['image'];
            $Texcl = $row['exclusivity'];
            $Tform = $row['format'];
            $Tprice = $row['price'];
            $id = $row['id'];


            $output .= '<div id="data">''<ul id="itemgal">'
            '<li id = "softitem">'
            '<a id= "row" href = "displaysoftware.php?id=" .$id." '.$Tname.' />' '</a>'
            '<a id= "row" href = "displaysoftware.php?id=" .$id." '.$Timg.' />'    '</a>'
            '<br />'
            '<h3>'.$Tform . '</h3>''</td>'
            '<br />'
            '<h4>'.$Texcl . '</h4>''</td>'
            '<h5>' '£' . $Tprice.'</h5>'
            '</li>'
            '</ul>'
            '</div>';
        }
    }

?>
</body>
</html>

您在 $output 变量中忘记了 $id 周围的单引号,然后,当您将字符串分布在更多行上时,您需要在每个末尾使用点 .行,不要用引号关闭字符串。

$output .= '<div id="data"><ul id="itemgal">' .
                                              ^
            '<li id = "softitem">' .
                                   ^
            '<a id= "row" href = "displaysoftware.php?id="' .$id. '" '.$Tname.' /></a>' .
               ^                                          ^       ^                     ^
            '<a id= "row" href = "displaysoftware.php?id="' .$id. '" '.$Timg.' />'    '</a>'
//                                                        ^       ^
Etc. 

. 放在 $output var.

中每一行的末尾

是的!你在 $output 中有字符串连接问题,看看这段代码:

<html>
<header></header>

<body>

<?php
    $serverName = "example.com";
    $dbName = "abc";
    $user = "abc";
    $pass = "abc";

    $connection = mysqli_connect($serverName, $user, $pass, $dbName);
    if (!$connection){
        die("Connection failure" . mysqli_connect_error());
    }

?>

<form id ="search" action="tobakuhome.php" method="post">
        <input type="text" name="search" placeholder="Search Game titles here"/>
        <input type="submit" value="Go" />
</form>

<?php print("$output"); ?>

<?php

$output = '';
if (isset($_POST['search'])){
    $searching = $_POST['search'];
    $searching = preg_replace("#[^0-9a-z]#i","", $searching);
} 

    $query = "SELECT * FROM Software WHERE name LIKE '%searching%' OR description LIKE '%$searching%' OR exclusivity LIKE '%$searching%' OR format LIKE '%$searching%'";
    $result = mysqli_query($connection, $query) or die("no results found");
    $count = mysqli_num_rows($query);
    if($count ==0){
        $output = 'Sorry, No results was found.';

    }else{
        while($row = mysqli_fetch_array($query)){
            $Tname = $row['Name'];
            $Tdes = $row['description'];
            $Timg = $row['image'];
            $Texcl = $row['exclusivity'];
            $Tform = $row['format'];
            $Tprice = $row['price'];
            $id = $row['id'];


            $output .= '<div id="data"><ul id="itemgal">'
            .'<li id = "softitem">'
            .'<a id= "row" href = "displaysoftware.php?id="' .$id. '" '.$Tname.' /></a>'
            .'<a id= "row" href = "displaysoftware.php?id="' .$id. '" '.$Timg.' /></a>'
            .'<br />'
            .'<h3>'.$Tform . '</h3></td>'
            .'<br />'
            .'<h4>'.$Texcl . '</h4></td>'
            .'<h5>£' . $Tprice. '</h5>'
            .'</li>'
            .'</ul>'
            .'</div>';
        }
    }

?>
</body>

我认为问题出在这里:

        $output .= '<div id="data">''<ul id="itemgal">'
        '<li id = "softitem">'
        '<a id= "row" href = "displaysoftware.php?id=" .$id." '.$Tname.' />' '</a>'
        '<a id= "row" href = "displaysoftware.php?id=" .$id." '.$Timg.' />'    '</a>'
        '<br />'
        '<h3>'.$Tform . '</h3>''</td>'
        '<br />'
        '<h4>'.$Texcl . '</h4>''</td>'
        '<h5>' '£' . $Tprice.'</h5>'
        '</li>'
        '</ul>'
        '</div>';

你不能像那样把字符串并排放置,你需要用 . 连接它们。或者根本不结束和开始字符串。

        $output .= '<div id="data"><ul id="itemgal">
        <li id = "softitem">
        <a id= "row" href = "displaysoftware.php?id="' .$id.'" '.$Tname.' /> </a>
        <a id= "row" href = "displaysoftware.php?id="' .$id.'" '.$Timg.' /> </a>
        <br />
        <h3>'.$Tform . '</h3></td>
        <br />
        <h4>'.$Texcl . '</h4></td>
        <h5>£' . $Tprice.'</h5>
        </li>
        </ul>
        </div>';