dsdtszi0520538 2014-11-19 14:27 采纳率: 100%
浏览 28
已采纳

php和select mysql之间的比较问题

I've been looking for an answer to my problems for few hours, but it seems I don't look at the right place, or I'm the first to have a so twisted programing style.

I have a in my php code, with some input inside, that put every informations in a database. After that, I have another form, with others input that allows me to make some research in my DB.

Here the troubles comes.

If I want to find something in aprticular, I have to fill all the inputs, like "name", "nickname", "computer name", and it finds it well, with a "select * from db where name=name and nickname=nickname etc...".

But if i want to find every computers that one person have, I would just put his name, and I want that it shows me every row that contain that name.

In other words, I want that my MySQL query just ignore the variables that are not completed in the form, I set them to NULL by default, but I have the feeling that mysql think it is a value, and compare it to the data in the db.

If I only fill 1 input, it shows nothing, I have to complete all the fields..

Code incoming, don't be rude, I'm pretty beegining... and also sorry for my english. :(

in advance, thank you for your help.

<html>
<head>
<link rel="stylesheet" href="index.css" type="text/css" media="screen" /> 


<title>TITRE</title>
</head>
<body>
<?php

//initializing everything to NULL, in case nothing is typed in the fields

if(!empty($_POST['techName']))
$techNom = $_POST['techName'];
else
$techNom = NULL;

if(!empty($_POST['techNickname']))
$techPrenom = $_POST['techNickname'];
else
$techPrenom = NULL;

if(!empty($_POST['userName']))
$userNom = $_POST['userName'];
else
$userNom = NULL;

if(!empty($_POST['userNickname']))
$userPrenom = $_POST['userNickname'];
else
$userPrenom = NULL;

if(!empty($_POST['assetTag']))
$tag = $_POST['assetTag'];
else
$tag = NULL;

if(!empty($_POST['serial']))
$serial = $_POST['serial'];
else
$serial = NULL;

if(!empty($_POST['cpuName']))
$cpuName = $_POST['cpuName'];
else
$cpuName = NULL;

if(!empty($_POST['modele']))
$modele = $_POST['modele'];
else
$modele = NULL;

if(isset($_POST['fixe']))
$type = $_POST['fixe'];
else
$type = NULL;

if(isset($_POST['portable']))
$type1 = $_POST['portable'];
else
$type1 = NULL;

if(!empty($_POST['date']))
$date = $_POST['date'];
else
$date = NULL;
?>

<h1>Visualisation de poste utilisateur</h1>
<ul id=menu>
<table>
<tr><td><a href="index.html">Accueil</a></td></tr>
<tr><td><a href="saisie.html">Saisie de matériel entrant</a></td></tr>
<tr><td><a href="visualisation.html">Visualisation du matériel</a></td></tr>
<tr><td><a href="visuGlobale.php">Visualisation du stock</a></td></tr>
<tr><td><a href="suppression.html">Suppression d'élément</a></td></tr>
<tr><td><a href="ajout_user.html">Ajout d'utilisateur</a></td></tr>
<tr><td><a href="visu_users.php">Visualisation des utilisateurs</a></td></tr>

</table>
</ul>

<FORM method="POST" action="">
<input type=hidden value='0' name=portable>
<input type=hidden value='0' name=fixe>
<table>
<tr><td colspan=4 align=center><b>TECHNICIEN</b></td></tr>
<tr><td>Prénom :</td><td><input type=text size=20 name=techNickname value=<?php echo $techPrenom ?>></td><td>Nom :</td><td><input type=text size=20 name=techName value=<?php echo $techNom?>></td></tr>
<tr><td colspan=4 align=center><b>UTILISATEUR</b></td></tr>
<tr><td>Prénom :</td><td><input type=text size=20 name=userNickname value=<?php echo $userPrenom?>></td><td>Nom :</td><td><input type=text size=20 name=userName value=<?php echo $userNom?>></td></tr>
<tr><td>TAG :</td><td><input type=text size=20 name=assetTag value=<?php echo $tag?>></td><td>N° de série :</td><td><input type=text size=20 name=serial value=<?php echo $serial?>></td></tr>
<tr><td>Nom de machine :</td><td><input type=text size=20 name=cpuName value=<?php echo $cpuName ?>></td><td>modèle :</td><td><input type=text size=20 name=modele value=<?php echo $modele ?>></td></tr>
<tr><td><label for="1">Portable</label></td><td><input type=checkbox name=portable checked="true" value=<?php $type1?>></td>
<td><label for="2">Fixe</label></td><td><input type=checkbox name=fixe value=<?php $type ?>></td></tr>
<tr><td>Date d'installation :</td><td><input type=text size=20 name=date placeholder="jj-mm-aaaa" value=<?php echo $date ?>></td></tr>
<tr><td colspan=2 ><INPUT type=submit Value=Envoyer></td><td><INPUT type=hidden name=afficher value=ok></td></tr>

</table>


<?php

//connecting DB
try
{
    $bdd = new PDO('mysql:host=localhost;dbname=base_test', 'root', '', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}
catch (Exception $e)
{
        die('Erreur : ' . $e->getMessage());
}

//some test that didn't help me well

/*$req = $bdd->prepare('SELECT * FROM table_utilisateurs WHERE userNom=:userNom
                        AND userPrenom=:userPrenom');
$req->execute(array('userNom' => $userNom,
                    'userPrenom' => $userPrenom));*/

$req = $bdd->prepare('SELECT * FROM table_utilisateurs WHERE userNom=:userNom 
                        AND userPrenom=:userPrenom
                        AND techNom=:techNom
                        AND techPrenom=:techPrenom
                        AND assetTag=:assetTag
                        AND serialNumber=:serialNumber
                        AND modele=:modele
                        AND type=:type
                        AND dateInstall=:dateInstall
                        AND name=:name');


$req->execute(array('userNom' => $userNom,
                    'userPrenom' => $userPrenom,
                    'techNom' => $techNom,
                    'techPrenom' => $techPrenom,
                    'assetTag' => $tag,
                    'serialNumber' => $serial,
                    'modele' => $modele,
                    'type' => $type,
                    'dateInstall' => $date,
                    'name' => $cpuName));





while($donnees = $req->fetch()){
    echo $donnees['userNom'], " ", $donnees['userPrenom'], " ", $donnees['techNom'], " ", $donnees['techPrenom'], " ", $donnees['assetTag'], " ", $donnees['serialNumber'], " ", 
                            $donnees['modele'], " ", $donnees['type'], " ", $donnees['dateInstall'], " ", $donnees['name'];

                            echo '<br/>';
    };


    $req->closeCursor();
    /*$donnees = $req->fetch();
    echo $donnees['userNom'], " ", $donnees['userPrenom'], " ", $donnees['techNom'], " ", $donnees['techPrenom'], " ", $donnees['assetTag'], " ", $donnees['serialNumber'], " ", 
                            $donnees['modele'], " ", $donnees['type'], " ", $donnees['dateInstall'], " ", $donnees['name'];*/



?>


</body>
</html>
  • 写回答

1条回答 默认 最新

  • douchengchen7959 2014-11-19 14:58
    关注

    As @David jones suggest. You can build custom where condition according to inputs entered by user in search field.

    $where_cond="";
    
    if($_POST['techName']!="")
    {
    $where_cond.="technamecolumn=".$_POST['techName'];
    } 
    
    if($_POST['username']!="")
    {
    $where_cond.="usernamecolumn=".$_POST['userName'];
    }
    

    same for other Now write your query as

    $q="SELECT technanmcolumn,usernamecolumn FROM tablename WHERE ".$where_cond;
    

    This query include those values that are entered by user to search in where condition. Execute that query and get result. I will suggest you to use switch case instead of IF conditions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 slam rangenet++配置
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊