[英]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 / PHP表单传递的。
HTML CODE HTML代码
<input name="rollno" type="text" placeholder="Roll Number" required>
<input name="submit_view_details" type="submit" value="Proceed">
PHP CODE PHP代码
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: 在浏览器中,如果输入输入1
并回显此查询,则如下所示:
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. 但是,在那种情况下,它获取所有行,但是我只需要用户以上述形式输入的rollno行。
I am just not able to work this out. 我只是无法解决这个问题。 Help would be much appreciated. 帮助将不胜感激。 Thanks! 谢谢!
Use the AND keyword to specify the rollno. 使用AND关键字指定rollno。
SELECT * FROM table1, table2 WHERE table1.rollno = table2.rollno
AND table1.rollno = {$rollno};
You could probably use the keyword JOIN instead like this : 您可能会像这样使用关键字JOIN :
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 http://www.tutorialspoint.com/mysql/mysql-using-joins.htm
You should use join like this 您应该像这样使用join
$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";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.