我有一个以下表格的应用程序:
<form action="<?php echo base_url('registration/registrationSearch'); ?>" method="post">
<td widtd="40"> </td>
<td><input type="text" name="txt_apply_for" class="form-control" /></td>
<td><input type="text" name="txt_name" class="form-control" /></td>
<td><input type="text" name="txt_mobile" class="form-control" /></td>
<td><input type="text" name="txt_address" class="form-control" /></td>
<td><input type="text" name="txt_state" class="form-control" /></td>
<td><input type="text" name="txt_city" class="form-control" /></td>
<td><input type="text" name="txt_area" class="form-control" /></td>
<td><input type="text" name="txt_email" class="form-control" /></td>
<td><input type="text" name="txt_work_experience" class="form-control" /></td>
<td><input type="text" name="txt_pan" class="form-control" /></td>
<td><input type="text" name="txt_refrence" class="form-control" /></td>
<td><input type="text" name="txt_refrence_others" class="form-control" /></td>
<td><input type="text" name="txt_status" class="form-control" /></td>
<td><input type="text" name="txt_payment_others" class="form-control" /></td>
<td><input type="text" name="txt_application_date" class="form-control" /></td>
<td><input type="submit" name="sbt" class="form-control" value="search" /></td>
</form>
我注意到,例如,当我仅使用name
提交时,我没有得到任何结果
当我搜索name
和mobile
时也会发生同样的情况:没有返回任何结果
我的搜索仅在搜索每个字段时有效。
这是我在搜索name
和mobile
时生成的查询:
SELECT *
FROM `application`
WHERE `apply_for` = ''
AND `full_name` = 'Ashish'
AND `mobile` = '1234567890'
AND `Mumbai` = ''
AND `Maharshtra` = ''
AND `Mumbai` = ''
AND `area` = ''
AND `email_id` = ''
AND `work_experinece` = ''
AND `pan` = ''
AND `refrence` = ''
AND `refrence_for_others` = ''
AND `approved` = ''
AND `payment_status` = ''
我的错误是我的搜索仅在每个字段都填满时才有效? 如果表单上的字段未填充,如何正确搜索数据库?
P.S。:我在我的后端使用PHP。
答案 0 :(得分:0)
在AND
子句中使用WHERE
布尔运算符意味着必须满足所有这些条件才能返回记录。在您的问题的示例中,除了full_name
和mobile
之外,它只返回所有列都为空的记录。
选项1:
在php代码中动态创建WHERE
子句字符串。仅当表单的字段值不为空时,才向WHERE
子句添加语句。引用你问题中的例子,你的sql看起来像这样。如果更多表单字段具有值,只需将它们附加到WHERE
子句。
SELECT *
FROM `application`
WHERE `full_name` = 'Ashish'
AND `mobile` = '1234567890'
选项2: 让你的SQL检查空值。
SELECT *
FROM `application`
WHERE `mobile` = ifnull([your parameter value], `mobile`)
AND `full_name` = ifnull([your parameter value], `full_name`)
AND `mobile` = ifnull([your parameter value], `mobile`)
etc.
旁注:请务必使用参数化查询,否则您很容易受到SQL注入攻击。有关加强代码的简单方法,请参阅How can I prevent SQL injection in PHP?。
答案 1 :(得分:0)
另一种选择,您可以编辑后端中用于获取数据的脚本,如下所示:
<?php
// Grab every data sent to you.
$name = $_POST['txt_name'];
$mobile = $_POST['txt_mobile'];
// .. other data here
$payment_status = $_POST['txt_status'];
// Create the arrays to store the parameters you will use
$types = array();
$where = array();
$param = array();
// Let's check every parameter...
// Have the user sent `name` to us?
if( $name != null ) {
// Then let's create a WHERE to it...
// How to choose the type: https://php.net/manual/en/mysqli-stmt.bind-param.php
$types[] = 's';
$where[] = '`full_name` = ?';
$param[] = $name;
}
// The same for `mobile`
if( $mobile != null ) {
$types[] = 's';
$where[] = '`mobile` = ?';
$param[] = $mobile;
}
// ... other parameters here...
// And to `payment_status`
if( $payment_status != null ) {
$types[] = 's';
$where[] = '`payment_status` = ?';
$param[] = $payment_status;
}
$sql = "SELECT * FROM `application`";
if( !empty( $param ) ) {
$types = implode( '', $types )
$sql = $sql . ' ' . implode( ' AND ', $where );
}
$stmt = $mysqli->stmt_init();
$stmt->prepare( $sql );
if( !empty( $param ) ) {
$stmt->bind_param( $types, ...$param );
}
$stmt->execute();
// Use the fetched data any way you want...
?>
不相关:
我希望它有所帮助。如果没有,请告诉我。