-7

I create search.php and create database war and table product.

My code doesn't work:

<!DOCTYPE html>
<html>
<head>
    <title>Search</title>
</head>
<body>
    <form method="get" action="">
        <table>
            <tr>
                <td>متن برای جستجو</td>
                <td><input type="text" name="text"></td>
            </tr>
            <tr>
                <td><input type="submit" value="Search"></td>
                <td>
                <?php
                    if(isset($_GET['text']) && !empty($_GET['text'])){
                        $body=$_GET['text'];
                        $con=mysql_connect("localhost","root","");
                        if(!$con){die("mysql Error");}
                        if (!mysql_select_db("war",$con)){die("mysql select error");}
                        $res=mysql_query("SELECT * FROM product WHERE LIKE pname='%$body%'");
                        $count=mysql_num_rows($res);
                        if ($count <= 0){
                            die("Your product Not found");

                        }else{
                            while ($row=mysql_fetch_array($res)){
                                echo $row['pname'];
                            }
                        }
                    }
                ?>

                </td>
            </tr>
        </table>
    </form>
</body>
</html>

Please help and debug.

D4V1D
  • 5,805
  • 3
  • 30
  • 65
Ali
  • 1
  • How can we "help and debug" without the full code? – D4V1D May 18 '15 at 14:17
  • 5
    _'doesn't work'_ is not a problem description –  May 18 '15 at 14:18
  • 1
    StackOverflow is a question and answer site, not a site for posting "plz fix my codez" requests disguised as a question. [**http://ericlippert.com/2014/03/05/how-to-debug-small-programs/**](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – spencer7593 May 18 '15 at 14:23
  • Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and consider using PDO, [it's not as hard as you think](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 18 '15 at 14:34
  • Please don't dump code in comments. Edit your original post to add any new information. – Jay Blanchard May 18 '15 at 14:34

1 Answers1

1

Your MySQL SELECT statement is wrong. It should be:

$res=mysql_query("SELECT * FROM product WHERE pname LIKE '%$body%'"); // note the absence of =

If this is, of course, what you mean by "it doesn't work".

PS : do not use mysql_* functions. They are deprecated.

Community
  • 1
  • 1
D4V1D
  • 5,805
  • 3
  • 30
  • 65