PHP搜索有多个下拉框(PHP search with multiple drop down boxes)

编程入门 行业动态 更新时间:2024-10-11 23:20:14
PHP搜索有多个下拉框(PHP search with multiple drop down boxes)

我正在制作一个带有多个下拉框的搜索框。 这是主页面:

<div id="wrapper"> <form action="search2.php" method="post"> <select name="user"> <option value="" selected="selected">All Users</option> <option value="tom">tom</option> <option value="bob">bob</option> </select> <select name="city"> <option value="" selected="selected">All cities</option> <option value="NY">NY</option> <option value="NA">LA</option> </select> <input type="submit" value="search" /> </form> </div>

这是search2.php

<?php include('includes/db_AF.php'); //includes the db credentials $connection = @new mysqli(HOSTNAME, MYSQLUSER, MYSQLPASS, MYSQLDB); $whereClauses = array(); if (! empty($_POST['user'])) $whereClauses[] ="user='".mysqli_real_escape_string($_POST['user'])."'"; if (! empty($_POST['city'])) $whereClauses[] ="city='".mysqli_real_escape_string($_POST['city'])."'"; $where = ''; if (count($whereClauses) > 0) { $where = ' WHERE '.implode(' AND ',$whereClauses); } $sql = mysqli_query("SELECT * FROM profile " .$where." ORDER BY user "); $result=mysqli_query($sql); or die("Error: ".mysql_error()."<br />Query: ".$sql); while ($row = mysqli_fetch_assoc($result)) { echo $row['user']; echo $row['city']; } ?>

我收到错误消息说:

mysqli_real_escape_string()只需要2个参数,给定1个

mysqli_query()需要至少2个参数,给定1个

我究竟做错了什么?

I am making a search box with multiple drop down boxes. Here is the main page:

<div id="wrapper"> <form action="search2.php" method="post"> <select name="user"> <option value="" selected="selected">All Users</option> <option value="tom">tom</option> <option value="bob">bob</option> </select> <select name="city"> <option value="" selected="selected">All cities</option> <option value="NY">NY</option> <option value="NA">LA</option> </select> <input type="submit" value="search" /> </form> </div>

And here is the search2.php

<?php include('includes/db_AF.php'); //includes the db credentials $connection = @new mysqli(HOSTNAME, MYSQLUSER, MYSQLPASS, MYSQLDB); $whereClauses = array(); if (! empty($_POST['user'])) $whereClauses[] ="user='".mysqli_real_escape_string($_POST['user'])."'"; if (! empty($_POST['city'])) $whereClauses[] ="city='".mysqli_real_escape_string($_POST['city'])."'"; $where = ''; if (count($whereClauses) > 0) { $where = ' WHERE '.implode(' AND ',$whereClauses); } $sql = mysqli_query("SELECT * FROM profile " .$where." ORDER BY user "); $result=mysqli_query($sql); or die("Error: ".mysql_error()."<br />Query: ".$sql); while ($row = mysqli_fetch_assoc($result)) { echo $row['user']; echo $row['city']; } ?>

I get error messages saying:

mysqli_real_escape_string() expects exactly 2 parameters, 1 given

mysqli_query() expects at least 2 parameters, 1 given

What am I doing wrong?

最满意答案

请尝试此代码,将mysqli_query($ sql)更改为mysqli_query($ connection,$ sql)并将$ connection添加到某个部分。 还删除了一些不需要的代码 请比较一下。

include('includes/db_AF.php'); //includes the db credentials $connection = @new mysqli(HOSTNAME, MYSQLUSER, MYSQLPASS, MYSQLDB); if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $whereClauses = array(); if (! empty($_POST['user'])) $whereClauses[] ="user='".mysqli_real_escape_string($connection,$_POST['user'])."'"; if (! empty($_POST['city'])) $whereClauses[] ="city='".mysqli_real_escape_string($connection,$_POST['city'])."'"; $where = ''; if (count($whereClauses) > 0) { $where = ' WHERE '.implode(' AND ',$whereClauses); } $sql = "SELECT * FROM profile " .$where." ORDER BY user "; $result=mysqli_query($connection,$sql) or die("Error: ".mysqli_error()."<br />Query: ".$sql); while ($row = mysqli_fetch_assoc($result)) { echo $row['user']; echo $row['city']; }

注意:请参考面向对象的样式和程序样式。

面向对象的风格

$ result = $ connection-> query($ sql);

程序风格

mysqli_query($连接,$ SQL);

http://www.php.net/manual/en/mysqli.query.php ,

跟随任何一个。

Please try this code, mysqli_query($sql) changed to mysqli_query($connection,$sql) and $connection added to some section. also removed some unwanted code. please compare it.

include('includes/db_AF.php'); //includes the db credentials $connection = @new mysqli(HOSTNAME, MYSQLUSER, MYSQLPASS, MYSQLDB); if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $whereClauses = array(); if (! empty($_POST['user'])) $whereClauses[] ="user='".mysqli_real_escape_string($connection,$_POST['user'])."'"; if (! empty($_POST['city'])) $whereClauses[] ="city='".mysqli_real_escape_string($connection,$_POST['city'])."'"; $where = ''; if (count($whereClauses) > 0) { $where = ' WHERE '.implode(' AND ',$whereClauses); } $sql = "SELECT * FROM profile " .$where." ORDER BY user "; $result=mysqli_query($connection,$sql) or die("Error: ".mysqli_error()."<br />Query: ".$sql); while ($row = mysqli_fetch_assoc($result)) { echo $row['user']; echo $row['city']; }

NB: Please Refer Object oriented style and Procedural style.

Object oriented style

$result = $connection->query($sql);

Procedural style

mysqli_query($connection,$sql);

http://www.php.net/manual/en/mysqli.query.php ,

follow any one.

更多推荐

本文发布于:2023-08-05 17:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1437212.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   下拉框   PHP   search   boxes

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!