搜索尝试语法错误

时间:2015-04-25 13:10:45

标签: php mysql search syntax

我无法弄清楚我是否正确行事。我试图从我的搜索输出结果,但我不确定这是否正确完成。另外我收到了这个错误。

  

解析错误:语法错误,第152行/usr/local/www/project/OlegarioJW/largecoursework/tobakuhome.php中的意外''''(T_CONSTANT_ENCAPSED_STRING)

此外,我打算将它和我的两个结果放在<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>

3 个答案:

答案 0 :(得分:1)

是的!你在$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>

答案 1 :(得分:0)

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

$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。

中每行的末尾

答案 2 :(得分:-1)

我认为问题在于:

ID : 2

你不能只是将字符串放在彼此旁边,你需要将它们与 $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>'; 连接起来。或者根本不结束和开始字符串。

.