3 Replies - 1755 Views - Last Post: 04 October 2017 - 09:57 AM Rate Topic: -----

#1 Sureprise  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-October 17

Search Using Radio Button

Posted 03 October 2017 - 02:25 PM

hi..really need help from all of you.
I want to use radio button to search between three different type of keyword and I have a problem with my code now.
This code only success on the first case only which means case 'name'. The other 2 case doesn't work at all. Please help me...

<table border="0" cellspacing="3" cellpadding="3">
                <tbody><form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
                    <tr align="center">
                        <td><input type="radio" name="RBsearch" value="name" />name
                            <input type="radio" name="RBsearch" value="password" />password
                            <input type="radio" name="RBsearch" value="role" />role
                        </td>
                    </tr>
                    <tr align="center">
                        <td>Search : <input type="text" name="txtSearch" value="" size="30" />
                            <input type="submit" value="Search" name="btnSearch" /></td>
                    </tr></form>
                <?php
					
$host = "localhost";  // имя сервера   
$user = "phpstart";       // имя пользователя
$password = "";       // пароль
$db_name = "phpshop"; 
					
                     $con = mysql_connect($host, $user, $password) or die ('unable to connect!' . mysql_error());
                    mysql_select_db("phpshop", $con);


                    $radio = isset($_POST['RBsearch']);
                    switch ($radio){
                         case 'name' : $result = mysql_query("SELECT * FROM user  WHERE  name LIKE '$_POST[txtSearch]' ");
                            break;
                        case 'password' : $result = mysql_query("SELECT * FROM user WHERE password LIKE '$_POST[txtSearch]' ");
                            break;
                        case 'role' : $result = mysql_query("SELECT * FROM user  WHERE role LIKE '$_POST[txtSearch]' ");
                            break;
                        default :  $result = mysql_query("SELECT * FROM  user ");
                            break;
                            }
                    ?>
                    <tr>
                        <td align="center"><table border="1" cellspacing="3" cellpadding="3">
                                <thead>
                                    <tr>
                                        <th width="100">id</th>
                                        <th width="200">name</th>
                                        <th width="200">email</th>
                                        <th width="200">password</th>
                                        <th width="200">role</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php while($row = mysql_fetch_array($result))
                                {
                                    echo "<tr>";
                                        echo "<td align=\"center\">". $row['id'] ."</td>";
                                        echo "<td>". $row['name'] ."</td>";
                                        echo "<td>". $row['email'] ."</td>";
                                        echo "<td>". $row['password'] ."</td>";
                                        echo "<td>". $row['role'] ."</td>
                                        
                                    </tr>";
                                }?>
                                </tbody>
                            </table>
                        </td>
                    </tr>
                </tbody>
            </table>

  </center>
    </body>




Is This A Good Question/Topic? 0
  • +

Replies To: Search Using Radio Button

#2 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1828
  • View blog
  • Posts: 5,755
  • Joined: 15-January 14

Re: Search Using Radio Button

Posted 03 October 2017 - 04:27 PM

You're setting $radio to the return value of isset. Isset only returns true or false if the variable is set or not. You should also use prepared statements for those queries.

Yikes, it looks like you're still using the old mysql extension. Don't use that. That code won't work at all in PHP 7. Use either mysqli or PDO, and learn and use prepared statements. It's not 2004 any more.
Was This Post Helpful? 0
  • +
  • -

#3 Sureprise  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-October 17

Re: Search Using Radio Button

Posted 03 October 2017 - 10:11 PM

So help, how to do differently.
I'm just a beginner in this
Was This Post Helpful? 0
  • +
  • -

#4 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1828
  • View blog
  • Posts: 5,755
  • Joined: 15-January 14

Re: Search Using Radio Button

Posted 04 October 2017 - 09:57 AM

If you're a beginner with PHP then you need to start learning how to use it. There's a tutorial for beginners here:

https://www.w3school...php/default.asp

That includes a section about prepared statements with the database:

https://www.w3school..._statements.asp

Those kind of things are directed at beginners, there's a lot more information about PHP and databases than what's there but that'a decent start. There are some more advanced resources here:

http://www.phptherightway.com
https://phpdelusions.net/pdo
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1