如何使用PHP表单从多个表中检索数据? [英] How to retrieve data from multiple tables using a PHP form?

查看:70
本文介绍了如何使用PHP表单从多个表中检索数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用点运算符/联接从多个表中检索数据,其中参数是从HTML/PHP表单传递的.

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

HTML代码

<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

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).

它仅在查询如下所示时有效:
select * from table1,table2 where table1.rollno=table2.rollno

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

但是,在那种情况下,它会获取所有行,但是我只需要用户以上述形式输入的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!

推荐答案

使用 AND 关键字指定rollno.

Use the AND keyword to specify the rollno.

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

您可能会像这样使用关键字 JOIN :

You could probably use the keyword JOIN instead like this :

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

这篇关于如何使用PHP表单从多个表中检索数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆