Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何根据用户提供的输入使用join在mysql中选择两个表_Php_Mysql - Fatal编程技术网

Php 如何根据用户提供的输入使用join在mysql中选择两个表

Php 如何根据用户提供的输入使用join在mysql中选择两个表,php,mysql,Php,Mysql,我有两张桌子。一个是包含用户详细信息的表,另一个是包含一些报价详细信息的表。这两个表都将用户ID作为主键。我想根据用户提供的ID访问两个表中的所有详细信息。我该怎么做? 用户表:- --------------------------------------------- id name email password acc_conf ---------------------------------------------- ---------------------

我有两张桌子。一个是包含用户详细信息的表,另一个是包含一些报价详细信息的表。这两个表都将用户ID作为主键。我想根据用户提供的ID访问两个表中的所有详细信息。我该怎么做? 用户表:-

---------------------------------------------
id    name    email    password    acc_conf
----------------------------------------------
----------------------------------------------
id    user_id    offer_code    quantity
----------------------------------------------
报价表:-

---------------------------------------------
id    name    email    password    acc_conf
----------------------------------------------
----------------------------------------------
id    user_id    offer_code    quantity
----------------------------------------------
脚本:-

<?php
     $id = $_POST['id'];
?>

您可以执行以下操作:

$sql = "SELECT a.id AS user_id, a.name, a.email, a.password, a.acc_conf,
b.id AS offer_id, b.offer_code, b.quantity 
FROM user_table AS a, offer_table AS b
WHERE a.id = b.user_id AND a.id = {$id}";
警告!:如果您按原样使用上述代码,那么您对SQL注入非常开放。前面的答案只是一个概念证明,代码将提供正确的结果,但是我建议您准备语句并像这样绑定参数:

$sql = "SELECT a.id AS user_id, a.name, a.email, a.password, a.acc_conf,
b.id AS offer_id, b.offer_code, b.quantity 
FROM user_table AS a, offer_table AS b
WHERE a.id = b.user_id AND a.id = :id";

$stmt = $conn->prepare($sql);
$stmt->bindParam(':id', $id);
$stmt->execute();
我使用了名称
user\u table
offer\u table
。我不确定你在问题中的名字之间是否有空格。如果您确实有空格,只需将我代码中的名称替换为
'user table'
'offer table'
。当列和表中有空格时,SQL需要引号来理解这些是空格,而不是SQL语句中的其他命令


已编辑:为了获得从查询执行中获得的值,可以使用以下代码:

$rows=$stmt->fetchAll();//now you have a multidimentional array called
                        //$rows with the information inside

print_r($rows);//echoing the information out
这很简单:

$query = "SELECT * FROM user INNER JOIN user ON offer.user_id=user.id WHERE user.id=$id";

注意:在执行查询之前清理输入,否则它容易受到SQL注入的攻击。

首先,“用户详细信息”表通常将用户ID作为*主键*-在这种情况下,外部关系的父关系是什么?至于你的实际问题,你要么想加入表,要么单独查询。。。具体取决于您要完成的任务(这在您的问题中肯定不清楚!)。您是否在问如何编写一个查询来实现这一点?如果是这样,您将需要编辑问题,以提供有关表和试图获得的输出的更多信息?是使用
id
id
还是使用
user\u id
id
?对不起,我指的是主键,但我提到了外键there@Webeng它的'id'带有'user_id',但我如何提供id的值。它来自user@AshirogiMuto
a.id
正在从用户处获取
id
,因为在上面例如,
a
user\u表
作为
来自user\u表。这就是你要问的吗?@AshirogiMuto我编辑了我的答案,并将
a.id命名为user\u id
,因此生成的表将在名为
user\u id
的列下。让我知道这是否是你想要的。我已经编辑了我的问题。我想根据user@Webeng:您的意思是
id
是参数吗?如果是这样,您需要将where子句添加为
where a.id=?