My search method in php not working. Please help

0 votes
asked May 4 in Web Development by binoysarker (120 points)

Hi I am trying to make a search form that will search the database table and display on the table. There are 3 sections.
1. index.php
2. process_studennt.php
3. Database.php

from the form it will go to the process_studennt.php then it go to the Database.php and after that it will come back and give the result in the table.

But I am having problem with it. Please help.

The warning is :

Notice: Undefined index: valuetosearch in
H:\install\xampp\htdocs\crud\index.php on line 46

Warning: Invalid argument supplied for foreach() in
H:\install\xampp\htdocs\crud\lib\Database.php on line 274

Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or
access violation: 1064 You have an error in your SQL syntax; check the
manual that corresponds to your MariaDB server version for the right
syntax to use near 'FROM tblstudent' at line 1 in
H:\install\xampp\htdocs\crud\lib\Database.php:280 Stack trace: #0
H:\install\xampp\htdocs\crud\lib\Database.php(280):
PDOStatement->execute() #1 H:\install\xampp\htdocs\crud\index.php(88):
Database->search('tbl
student', Array) #2 {main} thrown in

My form in the index.php page is :

<!-- creating a filter section to filter data -->
              <form class="form form-control" action="lib/process_student.php" method="POST" >
                  <fieldset class="form-group">
                            <input type="text" class="form-control-sm"  name="valuetosearch" placeholder="Search..">
                            <input type="hidden" name="action" value="search">
                            <input type="submit" class="btn btn-primary" id="submit" name="search" Value="Search">
                    </fieldset>

and the table to show the result is:

> /*
>                           search section 
> 
>                            */
> 
> 
>                           $searchdata = $db-> search($tablename,$selectcon);
>                           if (!empty($searchdata)) {
>                               ?>
>                               <tr>
>                                       <td><?php echo $searchdata["id"];; ?></td>
>                                       <td><?php echo $searchdata["name"]; ?></td>
>                                       <td><?php echo $searchdata["email"]; ?></td>
>                                       <td><?php echo $searchdata["phone"]; ?></td>
>                                       <td>
>                                           <a href="editstudent.php?id=<?php echo $searchdata["id"]; ?>" class="btn btn-secondary">Edit</a>
>                                           <a href="lib/process_student.php?action=delete&id=<?php echo $searchdata["id"]; ?>" class="btn btn-danger">Delete</a>
>                                       </td>
>                                   </tr>
>                               <?php
>                           } else {
>                               # code...
>                           }
>                           
> 
>                            ?>

the process_student.php file section:

// search section

    elseif ($_REQUEST["action"] == "search" && !empty($_REQUEST["action"])) {
        $searchvalue = $_POST["valuetosearch"];
        if (!empty($searchvalue)) {

            $tablename = "tbl_student";
            $condition = ["name" => $searchvalue];
            $searchdata = $db->search($tablename,$condition);

            if ($searchdata) {
                $msg = "Data founded";

            } else {
                $msg = "Error! No data founded";

            }

            /*
            redirecting to the index page 

             */
            Session::set("msg",$msg);
            $home_url = "../index.php"; 

            header("Location: ".$home_url);
        } else {
            # code...
        }

And the Database.php section :

public function search($tablename,$data)
    {

        $sql = "SELECT ";
        $sql .= array_key_exists("select", $data)?$data["select"]:"*";
        $sql .= " FROM ".$tablename;
        if (array_key_exists("where", $data)) {
            $sql .= " WHERE ";
            $i = 0;
            foreach ($data["where"] as $key => $value) {
                $add = ($i > 0)?" OR " : "";
                $sql .= "$add"."$key=:$key";
                $i++;
            }
        }





        // now prepare and execute
        $query = $this->pdo->prepare($sql);

        // bind value
        if (array_key_exists("select", $data)) {
            foreach ($data["select"] as $key => $value) {
                $query->bindValue(":$key","$value");
            }
        }

        // now execute
        $query->execute();

        // now for the return type

        if (array_key_exists("return_type", $data)) {
            switch ($data["return_type"]) {
                case 'count':
                    $value = $query->rowCount();
                    break;
                case 'single':
                    $value = $query->fetch(PDO::FETCH_ASSOC);
                    break;  

                default:
                    $value = "";
                    break;
            }
        }
        elseif ($query->rowCount() > 0) {
            $value = $query->fetchAll();
        }

        // finaly return
        return !empty($value)?$value:false;



    }

What is the problem please help me.
Thank you.

Please log in or register to answer this question.

722 questions

656 answers

179 comments

4,748 users

Welcome to IDB Scholars Forum, where you can ask questions and receive answers from other members of the community. It's 100% free.
আইডিবি স্কলারস ফোরামে আপনাকে স্বাগত - প্রোগ্রামিং, ডিজাইন, ডেভেলপমেন্ট, ব্লগিং, ইন্টারনেটসহ তথ্য প্রযুক্তির যাবতীয় বিষয় নিয়ে আলোচনার একটি মুক্ত মঞ্চ হলো এই ফোরাম । এখানে আইসিটি বিষয়ক আপনার প্রশ্ন, উত্তর এবং অভিজ্ঞতা শেয়ার করুন, আলোচনা করুন এবং সমাধান নিন। দেশের তরুণ প্রজন্মকে আইসিটিতে আগ্রহী করাই আমাদের মূল লক্ষ্য।
** ইংলিশ বা বাংলা যে কোনো ভাষায় আপনি এই ফোরামে আলোচনা করতে পারবেন। তবে বাংলাকে ইংরেজি অক্ষরে লিখবেন না।
  1. tulbadex

    110 points

    0 answers

  2. stepscale54

    100 points

    0 answers

  3. JettaQoa3381

    100 points

    0 answers

  4. clutch7conga

    100 points

    0 answers

  5. WhitneyMauer

    100 points

    0 answers

...