我想使用点运算符/连接从多个表中检索数据,其中参数从HTML / PHP表单传递。
HTML CODE
<input name="rollno" type="text" placeholder="Roll Number" required>
<input name="submit_view_details" type="submit" value="Proceed">
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);
}
在浏览器中输入输入1
并回显此查询,然后如下所示:
select * from table1, table2 where table1.1=table2.1
并且尽管表中有数据,但不会获取任何行。
只有在查询如下所示时才有效:
select * from table1,table2 where table1.rollno=table2.rollno
但是,在这种情况下,它会获取所有行,但我只需要用户在上述表单中输入的rollno行。
我无法解决这个问题。非常感谢帮助。谢谢!
答案 0 :(得分:3)
使用 AND 关键字指定rollno。
SELECT * FROM table1, table2 WHERE table1.rollno = table2.rollno
AND table1.rollno = {$rollno};
您可以使用关键字 JOIN ,而不是这样:
SELECT * FROM table1 NATURAL JOIN table2
WHERE rollno = {$rollno};
答案 1 :(得分:1)
答案 2 :(得分:0)
你应该像这样使用
$query = "SELECT tbl1.*, tbl2.*
FROM tbl1
INNER JOIN tbl2 ON tbl1.id = tbl2.id
WHERE tbl1.column = value ";
答案 3 :(得分:-1)
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";