我正在尝试构建一个依赖于所选表单选项的查询。我想我已经完成了这个,但我认为它看起来不像我想象的那样有效(如果我给你看剧本,你会认为阅读圣经不会花那么长时间)。我想看看你们中是否有人能提出任何想法来提高效率。
用户必须选择两件事:
首先,他们想要显示什么样的发票(albaran),(paid(pagado),open(pte),without issuing(sin factura),all of them(todos))。 其次,发票(albaran)指的是哪个客户(特定的一个或所有客户(todos))。 invoices和clients是共享ref_cliente列的两个不同的mysql表
所有这些都是在 page1.php 中选择的,当填写表单并使用 post 方法发送时,页面会重新加载“激活”带有数组 $_POST 加载的脚本。
感谢您的时间和兴趣!
编辑问题以添加 OP 的表架构
TABLE clientes(列:ref_cliente、nombre、nif)
TABLE facturas(列:ref_factura、ref_cliente、estado)
estado
是一个 varchar 列,表示发票是否已支付、未支付或未开具。 ( bool 效应)
最佳答案
正如 bonCodigo 所说,如果您显示表架构,可能会更容易提供帮助...但是,我会尝试一下。
我会用两个参数“status”和“client”来设置它,然后必须根据这些参数的值构建 SQL。我正在使用 PDO
喜欢:
$sql="SELECT * from invoices i, clients c where c.ref_cliente=i.ref_cliente";
$params=array();
if($_POST['status']!='todos'){
$sql.=' and i.status=?';
$params[]=$_POST['status'];
}
if($_POST['clients']!='todos'){
$sql.=' and c.ref_cliente=?';
$params[]=$_POST['clients'];
}
$sqlh=$dbh->prepare($sql); // $dbh must be set up, see the PDO-documentation
$sqlh->execute($params);
关于php - 多个表上的多个选择选项 mysql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13604068/