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>