Saving search results and sending to database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MikeSpider
Forum Commoner
Posts: 25
Joined: Sat Oct 22, 2011 6:45 pm

Saving search results and sending to database

Post by MikeSpider »

Hi guys,
My application fetchs items on a database according to the user input/multiple-choices, and displays them on a page.
Is there a way to save those same results and send them to database. or even, send them by email ?

Any light will be greatly appreciated.
Thanks in advance,
Mike Spider
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: Saving search results and sending to database

Post by manohoo »

The purpose of a form is to capture user's input and then do something with it.
Your question suggests that you don't understand forms yet, google "html form tutorial" and you will find plenty of options.
MikeSpider
Forum Commoner
Posts: 25
Joined: Sat Oct 22, 2011 6:45 pm

Re: Saving search results and sending to database

Post by MikeSpider »

Hehehehe, very funny.
What I meant was: Saving the Query results , and not just the user input.

When the users makes the search with a particular criteria,
the query returns those resuls from database into the page.
I want to know it there's a way to save the returned query result,
and save them in another db table.

thanks,
Mike Spider
mikeashfield
Forum Contributor
Posts: 159
Joined: Sat Oct 22, 2011 10:50 am

Re: Saving search results and sending to database

Post by mikeashfield »

MikeSpider wrote:Hehehehe, very funny.
What I meant was: Saving the Query results , and not just the user input.

When the users makes the search with a particular criteria,
the query returns those resuls from database into the page.
I want to know it there's a way to save the returned query result,
and save them in another db table.

thanks,
Mike Spider
This will largely depend on the code you are using to retrieve the results; does the query return one or many values? Does the DB have data added or is it the same data consistently? If no records are added to the DB from one request to the next, then there's no problem in using a query like:

Code: Select all

 INSERT INTO tbl_name ('query_string') VALUES ('$REQUEST[searched_string]');
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Re: Saving search results and sending to database

Post by icesolid »

Maybe something like:

Code: Select all

<?php
// connect to your DB here

$result = mysql_query("your search SQL statement");

// set a variable to hold result data, get it started, make sure it's clear
$save_it = '';

while($row = mysql_fetch_assoc($result)) {
    // add each search result to our $save_it variable, format this data here how ever you currently format each row you output
    $save_it.= $row['here_is_a_table_column'] . ' and ' . $row['here_is_another_table_column'];
}

// print out our results, held in $save_it

echo $save_it;

// now down here do what you want with the data you stored in your $save_it variable, maybe add it to a database as a record...

mysql_query("INSERT INTO `search_db` (search_data) VALUES ('$save_it')");

// OR mail it and send it to someone...

mail(..., ..., $save_it);

// or you can leave both lines in above and you can accomplish both tasks, either way, $save_it will always hold our results and we can display it, send it around, or add it to a DB or whatever you want really
?>
MikeSpider
Forum Commoner
Posts: 25
Joined: Sat Oct 22, 2011 6:45 pm

Re: Saving search results and sending to database

Post by MikeSpider »

Thanks for your quick replies :).

@ mikeashfield, The database is constantly updated, and the query returns the values in an array form,
the number of results depends mainly in how many values were found related to search criteria.

@icesolid, I will try it out and will get back.

Many thanks,
Mike Spider
MikeSpider
Forum Commoner
Posts: 25
Joined: Sat Oct 22, 2011 6:45 pm

Re: Saving search results and sending to database

Post by MikeSpider »

I thought this could work, but only empty values are being inserted into database table,
I stored the results in $data, but it seems empty when i call it inside other function (Insert).

below is the code:

Code: Select all

private function Search()
    {
       try{
                  if("" != $this->substance){

                 $sql = "SELECT * FROM registration WHERE substance1='{$this->substance}' ORDER BY joined DESC";

                }
                 $result = $this->db->mysqli->query($sql);

                   if(!$result){

                       throw new Exception("Query failed: " . $sql . " - " . $this->db->mysqli->error);
                   }else{

                       
                       return $result;
                       
                   }



                  }  catch(Exception $e){
                   echo("Message: " . $e->getMessage());
                }

    }


 public function AdminViewMembers()
    {

        $view_members = $this->Search();
         while($p = $view_members->fetch_assoc()){

        $this->data[] = array( 'id'=>$p['id'],
                               'user'=>$p['user'],
                               'email'=>$p['email'],
                               'name'=>$p['name'],
                               'surname'=>$p['surname'],
                               'age'=>$p['age'],
                               'address'=>$p['address'],
                                'postcode'=>$p['postcode'],
                                'telephone'=>$p['telephone'],
                               'substance1'=>$p['substance1'],
                               'substance2'=>$p['substance2'],
                               'timeusing'=>$p['timeusing'],
                               'service'=>$p['service'],
                               'previledges'=>$p['previledges'],
                               'joined'=>$p['joined'],
                               'delete'=>"<a style='color:blue'href='manage-users.php?id={$p['id']}& action=delete' onclick='return(confirm(\"Are you sure you want to delete this user?\"))'>Delete</a>",
                               'update'=>"<a style='color:blue' href='update-users.php?id={$p['id']}&s={$p['service']}&pv={$p['previledges']}'>Update</a>");

         }
         return $this->data;

    }

     public function Insert($user,$name,$surname,$age,$address,$pcode,$phone,$subs1,$othersubs,$subs2,$time,$service,$support,$othersup,$gp,$gpaddress,$joined){

        try{

           for($row=0;$row<count($this->data);$row++){


            $user = $this->data[$row]['user'];
            $name = $this->data[$row]['name'];
            $surname = $this->data[$row]['surname'];
            $age = $this->data[$row]['age'];
            $email = $this->data[$row]['email'];
            $address = $this->data[$row]['address'];
            $pcode = $this->data[$row]['postcode'];
            $phone = $this->data[$row]['telephone'];
            $subs1 = $this->data[$row]['substance1'];
            $othersubs = $this->data[$row]['othersubstance'];
            $subs2 = $this->data[$row]['substance2'];
            $time = $this->data[$row]['timeusing'];
             $service = $this->data[$row]['service'];
            $support = $this->data[$row]['support'];
            $othersup = $this->data[$row]['othersupport'];
             $gp = $this->data[$row]['gp'];
              $gpaddress = $this->data[$row]['gpaddress'];
             $joined = $this->data[$row]['joined'];

           }
            $sql = "INSERT INTO searchresults VALUES(NULL,'{$this->data['user']}','{$this->data['name']}','{$surname}','{$age}','{$email}',
            '{$address}','{$pcode}','{$phone}','{$subs1}','{$othersubs}','{$subs2}','{$time}','{$service}','{$support}','{$othersup}','{$gp}','{$gpaddress}','{$joined}',NOW())";
           
            $result = $this->db->mysqli->query($sql);
         
            if(!$result){

               throw new Exception("Query failed: " . $sql . " - " . $this->db->mysqli->error);

            }else{
                $this->success = true;


                }   


            } catch(Exception $e){
               echo("Message: " . $e->getMessage());


        }
    }


Thanks,
MikeSpider
Post Reply