PHP MYSQL选择邮政编码或customerid或其他 [英] PHP MYSQL Select postcode OR customerid OR anythingelse
问题描述
大家好(对不起,我懒惰的英语^^)!我想要SELECT的searchform来搜索(示例)邮政编码1234的john doe.但是,当我用1234搜索john doe时,结果也就是具有customerid之类的客户1234.多数民众赞成在不好,但我认为,我在正确的道路上.我该如何解决?好的,这是我的初学者代码,我希望您能笑一点,并且您可以帮助我将此代码编码为我正在寻找的代码. :D非常感谢,伙计们!
Hi guys (and sorry for my lazy english^^) ! I want a SELECT for a searchform to search (example) with zipcode 1234 for john doe. but when i search for john doe with 1234 the result is a customer with customerid like 1234, too. thats not good, but i am think, i am on the right way. How can i fix it ? Okay, so here is my beginners code and i hope you can laugh a little bit and you can help my to code this to a that what i am looking for. :D Thank you so much, Guys !!
<table class="table table-striped" >
<p>
<tr>
<th>Kundennummer</th>
<th>Reklamationsnummer</th>
<th>Firma</th>
<th>Nachname</th>
<th>Vorname</th>
<th>Straße</th>
<th>Nr</th>
<th>Plz</th>
<th>Ort</th>
<th>Telefon</th>
<th>Mail</th>
<tr>
<?php
error_reporting(-1);
ini_set('display_errors', true);
include "config.php";
$kdnr = mysqli_real_escape_string($mysqli, $_GET['kdnr']);
$reklamationsnummer = mysqli_real_escape_string($mysqli, $_GET['reklamationsnummer']);
$firma = mysqli_real_escape_string($mysqli, $_GET['firma']);
$nachname = mysqli_real_escape_string($mysqli, $_GET['nachname']);
$vorname = mysqli_real_escape_string($mysqli, $_GET['vorname']);
$street = mysqli_real_escape_string($mysqli, $_GET['street']);
$hausnummer = mysqli_real_escape_string($mysqli, $_GET['hausnummer']);
$postleitzahl = mysqli_real_escape_string($mysqli, $_GET['postleitzahl']);
$ort = mysqli_real_escape_string($mysqli, $_GET['ort']);
$telefon = mysqli_real_escape_string($mysqli, $_GET['telefon']);
$mail = mysqli_real_escape_string($mysqli, $_GET['mail']);
$result = $mysqli->query("SELECT kdnr, reklamationsnummer, firma, nachname, vorname, street, hausnummer, postleitzahl, ort, telefon , mail
FROM kundentest
WHERE
kdnr = '".$kdnr."' XOR
reklamationsnummer = '".$reklamationsnummer."' XOR
firma = '".$firma."' XOR
nachname = '".$nachname."' XOR
vorname = '".$vorname."' XOR
street = '".$street."' XOR
hausnummer = '".$hausnummer."' XOR
postleitzahl = '".$postleitzahl."' XOR
ort = '".$ort."' XOR
telefon = '".$telefon."' XOR
mail = '".$mail."' ");
while ($row = $result->fetch_assoc()):
?>
<tr>
<td><?php echo $row['kdnr']; ?></td>
<td><?php echo $row['reklamationsnummer']; ?></td>
<td><?php echo $row['firma']; ?></td>
<td><?php echo $row['nachname']; ?></td>
<td><?php echo $row['vorname']; ?></td>
<td><?php echo $row['street']; ?></td>
<td><?php echo $row['hausnummer']; ?></td>
<td><?php echo $row['postleitzahl']; ?></td>
<td><?php echo $row['ort']; ?></td>
<td><?php echo $row['telefon']; ?></td>
<td><?php echo $row['mail']; ?></td>
</tr>
<?php
endwhile;
?>
</table>
<table>
<tr>
<th><a class="btn btn-info" href="index.php" role="button">zurück zur Suche</a></th>
</tr>
</table>
推荐答案
我想您需要某种动态查询字符串生成.
尝试将片段从include "config.php";
开始替换为:
I guess you need some kind of dynamic query string generation.
Try to replace the fragment starts from include "config.php";
to :
include "config.php";
$query = "SELECT kdnr, reklamationsnummer, firma, nachname, vorname, street, hausnummer, postleitzahl, ort, telefon , mail
FROM kundentest ";
$search_fields = array('kdnr',
'reklamationsnummer',
'firma',
'nachname',
'vorname',
'street',
'hausnummer',
'postleitzahl',
'ort',
'telefon',
'mail'
);
$first = true;
$params = array();
foreach ($search_fields as $column) {
if(isset($_GET[$column])) {
if ($first) {
$query.='WHERE ';
$first = false;
} else {
$query.=' AND ';
}
$query.= $column.' = ? ';
$params[]=$_GET[$column];
}
}
if ($stmt = $mysqli->prepare($query)) {
$i=0;
foreach($params as $param) {
${'param'.++$i} = $param;
$stmt->bind_param('s', ${'param'.$i});
}
$stmt->execute();
/* bind result variables */
$stmt->bind_result($kdnr, $reklamationsnummer, $firma, $nachname, $vorname, $street, $hausnummer, $postleitzahl, $ort, $telefon , $mail);
while ($stmt->fetch()) { ?>
<tr>
<td><?= $kdnr ?></td>
<td><?= $reklamationsnummer ?></td>
<td><?= $firma ?></td>
<td><?= $nachname ?></td>
<td><?= $vorname ?></td>
<td><?= $street ?></td>
<td><?= $hausnummer ?></td>
<td><?= $postleitzahl ?></td>
<td><?= $ort ?></td>
<td><?= $telefon ?></td>
<td><?= $mail ?></td>
</tr>
<?php }
}
如果有任何问题,我们非常欢迎.
you are very welcome if any questions.
这篇关于PHP MYSQL选择邮政编码或customerid或其他的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!