简体   繁体   中英

How to retrieve data from multiple tables using a PHP form?

I want to retrieve data from multiple tables using dot operator/join where arguments are passed from an HTML/PHP form.

HTML CODE

<input name="rollno" type="text" placeholder="Roll Number" required>
<input name="submit_view_details" type="submit" value="Proceed">

PHP CODE

if(isset($_POST['submit_view_details']))
{
    $rollno = (int) $_POST['rollno'];
    $query = "select * from table1, table2 where table1.{$rollno}=table2.{$rollno}";
    $result=mysqli_query($connection,$query);
}

In the browser if enter the input 1 and echo this query then it looks like follows:
select * from table1, table2 where table1.1=table2.1

and no row is fetched despite of having data in the table(s).

it only works if the query looks like follows:
select * from table1,table2 where table1.rollno=table2.rollno

However, in that case it fetches all the rows but I need only the row of the rollno that user entered in the above mentioned form.

I am just not able to work this out. Help would be much appreciated. Thanks!

Use the AND keyword to specify the rollno.

SELECT * FROM table1, table2 WHERE table1.rollno = table2.rollno 
AND table1.rollno = {$rollno};

You could probably use the keyword JOIN instead like this :

SELECT * FROM table1 NATURAL JOIN table2 
WHERE rollno = {$rollno};

You need joins

take a reference of joins from here,

i am sure it will help

http://www.tutorialspoint.com/mysql/mysql-using-joins.htm

You should use join like this

$query = "SELECT tbl1.*, tbl2.* 
          FROM tbl1 
          INNER JOIN tbl2 ON tbl1.id = tbl2.id 
          WHERE tbl1.column = value ";
foreach ($pieces_2 AS $value) {
         $pieces_3[] ="(CONCAT_WS('|',$presql2) like '%$value%')";      //concat all columns from one table
 }
 $serch_jyouken = implode(" and ",$pieces_3);       // for multiple keywords                    
 $result1 = mysqli_query($connection, "select p.p_no from pfr_data p where (" .$serch_jyouken .")");
 $res1 = array();
 while($r1 = mysqli_fetch_array($result1){
         $res1[] = $r1['p_no'] ;        //fetch primary key from table and store it into array 
 }
 foreach ($pieces_2 AS $value) {            
         $pieces_4[] ="(CONCAT_WS('|',$presql3) like '%$value%')";      // same as above
 }
 $serch_jyouken1 = implode(" and ",$pieces_4);
 $result2 = mysqli_query($connection, "select p2.p_no from pfr_mod_inform p2 where (" .$serch_jyouken1 .")" );
$res2 = array();
 while($r2 = mysqli_fetch_array($result2)){                                                                               
         $res2[] = $r2['p_no'];
 }
 $res_mrg = array_merge($res1 , $res2);     //merge array
 $result = implode("','",$res_mrg );        // array to sring
 $sql5 = $presql ." from pfr_data p where p.p_no in ('$result') order by p.section_p,p.status,p.no";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM