add and search record done

This commit is contained in:
adri 2019-01-02 13:23:07 +01:00
parent 50705e1234
commit f99073eedd
5 changed files with 164 additions and 38 deletions

View File

@ -1,19 +1,22 @@
<?php <?php
//SELECT * FROM `projetphp` WHERE `login` = 'aaazzze'
function userExist($login){
try { try {
$config = include('config.php'); $config = include('config.php');
$bdd = new PDO('mysql:host='.$config['host'].';dbname='.$config['dbName'].';charset=utf8', $config['username'], $config['pw']); $bdd = new PDO('mysql:host='.$config['host'].';dbname='.$config['dbName'].';charset=utf8', $config['username'], $config['pw']);
} catch (Exception $e) { } catch (Exception $e) {
die('Erreur : '.$e->getMessage()); die('Erreur : '.$e->getMessage());
} }
//SELECT * FROM `projetphp` WHERE `login` = 'aaazzze'
function userExist($login){
$req = $bdd->prepare('SELECT * FROM `projetphp` WHERE `login` = :login'); $req = $bdd->prepare('SELECT * FROM `projetphp` WHERE `login` = :login');
$req->execute(array('login' => htmlspecialchars($_POST["userToDel"]))); $req->execute(array('login' => htmlspecialchars($login)));
if($req->rowCount() != 0) if($req->rowCount() != 0)
{ {
$req->closeCursor();
return true; return true;
} }
else { else {
$req->closeCursor();
return false; return false;
} }
} }

View File

@ -25,7 +25,7 @@ $reponse = $bdd->query('SELECT * FROM projetphp');
<link rel="stylesheet" href="style.css?<?php echo date('l jS \of F Y h:i:s A'); ?>"> <link rel="stylesheet" href="style.css?<?php echo date('l jS \of F Y h:i:s A'); ?>">
<script src="script.js?<?php echo date('l jS \of F Y h:i:s A'); ?>"></script> <script src="script.js?<?php echo date('l jS \of F Y h:i:s A'); ?>"></script>
</head> </head>
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names..">
<body> <body>
<table id="allRecord"> <table id="allRecord">
<thead> <thead>
@ -62,6 +62,7 @@ while ($data = $reponse->fetch()) {
</tr> </tr>
<?php <?php
} }
$reponse->closeCursor();
?> ?>
<tr rowspan="2"> <tr rowspan="2">
<td colspan="6" style="text-align:center"><i class="fas fa-user-plus fa-4x click"></i></td> <td colspan="6" style="text-align:center"><i class="fas fa-user-plus fa-4x click"></i></td>
@ -69,5 +70,28 @@ while ($data = $reponse->fetch()) {
</tbody> </tbody>
</table> </table>
</body> </body>
<script>
//https://www.w3schools.com/howto/howto_js_filter_table.asp
function myFunction() {
// Declare variables
var input, filter, table, tr, td, i, txtValue;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
table = document.getElementById("allRecord");
tr = table.getElementsByTagName("tr");
// Loop through all table rows, and hide those who don't match the search query
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[0];
if (td) {
txtValue = td.textContent || td.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
}
</script>
</html> </html>

View File

@ -1,4 +1,8 @@
<?php <?php
require_once("function.php");
$emailPatern = '/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/';
$pwPatern = '/^[a-z]{8,16}$/';
$loginPatern = '/^[a-z]{8,16}$/';
$ret["return"] = false; $ret["return"] = false;
$ret["pw"] = null; $ret["pw"] = null;
try { try {
@ -12,13 +16,53 @@ try {
if (isset($_POST["userToDel"])) { if (isset($_POST["userToDel"])) {
$req = $bdd->prepare('DELETE FROM `projetphp` WHERE `projetphp`.`login`=:login'); $req = $bdd->prepare('DELETE FROM `projetphp` WHERE `projetphp`.`login`=:login');
$req->execute(array( 'login' => htmlspecialchars($_POST["userToDel"]))); $req->execute(array( 'login' => htmlspecialchars($_POST["userToDel"])));
$req->closeCursor();
} }
elseif (isset($_POST["userExist"])) {
if(userExist($_POST["userExist"]))
{
$ret["return"] = "userExist";
}
else
{
$ret["return"] = "userNotExist";
}
}
//INSERT INTO `projetphp` (`login`, `date_naissance`, `e_mail`, `mot_de_passe`, `security_level`) VALUES ('blipblop', '2019-01-09', 'aaaaa@aaa.cc', MD5('sdfqsdfsfd'), '5');
elseif (isset($_POST["newUser"],$_POST["datene"], $_POST["email"], $_POST["pw"], $_POST["sl"]) && $_POST["newUser"] == "true") {
if(preg_match($loginPatern, $_POST["login"]) && preg_match($emailPatern, $_POST[email]) && preg_match($pwPatern, $_POST["pw"]))
{
if(userExist($_POST["login"]))
{
$ret["return"] = "userExist";
}
else
{
$ret["pw"] = md5(htmlspecialchars($_POST["pw"], ENT_QUOTES, 'UTF-8'));
$req = $bdd->prepare('INSERT INTO `projetphp` (`login`, `date_naissance`, `e_mail`, `mot_de_passe`, `security_level`) VALUES (:login, :datene, :email, :pw, :slvl)');
$req->execute(array(
'login' => htmlspecialchars($_POST["login"], ENT_QUOTES, 'UTF-8'),
'datene' => htmlspecialchars($_POST["datene"], ENT_QUOTES, 'UTF-8'),
'email' => (htmlspecialchars($_POST["email"], ENT_QUOTES, 'UTF-8')),
'pw' => $ret["pw"],
'slvl' => htmlspecialchars($_POST["slvl"], ENT_QUOTES, 'UTF-8'),
));
$req->closeCursor();
$ret["return"] = true;
}
}
else
{
$ret["return"] = "patern mismatch !".preg_match($loginPatern, $_POST["login"]) ."_". preg_match($emailPatern, $_POST[email]) ."_". preg_match($pwPatern, $_POST["pw"]);
}
}
//UPDATE `projetphp` SET `security_level` = '6' WHERE `projetphp`.`login` = 'aaazzze'; //UPDATE `projetphp` SET `security_level` = '6' WHERE `projetphp`.`login` = 'aaazzze';
if (isset($_POST["updatePW"], $_POST["datene"], $_POST["email"], $_POST["pw"], $_POST["sl"], $_POST["login"])) { else if (isset($_POST["newUser"], $_POST["updatePW"], $_POST["datene"], $_POST["email"], $_POST["pw"], $_POST["sl"], $_POST["login"]) && $_POST["newUser"] == "false") {
if (preg_match('/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/', $_POST["email"])) { if (preg_match($emailPatern, $_POST["email"])) {
if (htmlspecialchars($_POST["updatePW"], ENT_QUOTES, 'UTF-8') == "true" && preg_match('/^[a-z]{8,16}$/', $_POST["pw"])) { if (htmlspecialchars($_POST["updatePW"], ENT_QUOTES, 'UTF-8') == "true" && preg_match($pwPatern, $_POST["pw"])) {
$req = $bdd->prepare('UPDATE `projetphp` SET `date_naissance` = :datene, `e_mail` = :email, `mot_de_passe` = :pw, `security_level` = :sl WHERE `projetphp`.`login` = :login'); $req = $bdd->prepare('UPDATE `projetphp` SET `date_naissance` = :datene, `e_mail` = :email, `mot_de_passe` = :pw, `security_level` = :sl WHERE `projetphp`.`login` = :login');
$req->execute(array( $req->execute(array(
'datene' => htmlspecialchars($_POST["datene"], ENT_QUOTES, 'UTF-8'), 'datene' => htmlspecialchars($_POST["datene"], ENT_QUOTES, 'UTF-8'),
@ -27,6 +71,7 @@ if (isset($_POST["updatePW"], $_POST["datene"], $_POST["email"], $_POST["pw"], $
'sl' => htmlspecialchars($_POST["sl"], ENT_QUOTES, 'UTF-8'), 'sl' => htmlspecialchars($_POST["sl"], ENT_QUOTES, 'UTF-8'),
'login' => htmlspecialchars($_POST["login"], ENT_QUOTES, 'UTF-8'), 'login' => htmlspecialchars($_POST["login"], ENT_QUOTES, 'UTF-8'),
)); ));
$req->closeCursor();
$ret["return"] = true; $ret["return"] = true;
$ret["pw"] = md5(htmlspecialchars($_POST["pw"], ENT_QUOTES, 'UTF-8')); $ret["pw"] = md5(htmlspecialchars($_POST["pw"], ENT_QUOTES, 'UTF-8'));
} elseif (htmlspecialchars($_POST["updatePW"], ENT_QUOTES, 'UTF-8') == "false") { } elseif (htmlspecialchars($_POST["updatePW"], ENT_QUOTES, 'UTF-8') == "false") {
@ -37,6 +82,7 @@ if (isset($_POST["updatePW"], $_POST["datene"], $_POST["email"], $_POST["pw"], $
'sl' => htmlspecialchars($_POST["sl"], ENT_QUOTES, 'UTF-8'), 'sl' => htmlspecialchars($_POST["sl"], ENT_QUOTES, 'UTF-8'),
'login' => htmlspecialchars($_POST["login"], ENT_QUOTES, 'UTF-8'), 'login' => htmlspecialchars($_POST["login"], ENT_QUOTES, 'UTF-8'),
)); ));
$req->closeCursor();
$ret["return"] = true; $ret["return"] = true;
$ret["pw"] = htmlspecialchars($_POST["pw"], ENT_QUOTES, 'UTF-8'); $ret["pw"] = htmlspecialchars($_POST["pw"], ENT_QUOTES, 'UTF-8');
} else ; } else ;

105
script.js
View File

@ -1,6 +1,8 @@
htmlAddUser = '<tr class="newRecord">\ htmlAddUser = '<tr class="newRecord">\
<td class="login">\ <td class="login">\
<input type="text" /></td>\ <input type="text" />\
<div class="userExist">L\'utilisateur existe déja !</div>\
<div class="ruleExcept">le login comporte entre 8 et 16 caractères parmi a..z</div></td>\
<td class="datene" placeholder="yyyy/mm/dd">\ <td class="datene" placeholder="yyyy/mm/dd">\
<input type="date" /></td>\ <input type="date" /></td>\
<td class="email">\ <td class="email">\
@ -52,6 +54,32 @@ $(function() {
$('div', $(this).parent()).css("display", "block"); $('div', $(this).parent()).css("display", "block");
} }
}); });
$("#allRecord").on("keyup change", ".login :input", function(e) {
login = ltrim($(this).val());
input = $(this);
td = $(this).parent();
if (loginPatern.test(login)) {
$(this).css("background-color", "");
$('.ruleExcept', $(this).parent()).css("display", "none");
$.post("post.php", {userExist: login}, function(data) {
try {
if (data.return == "userExist") {
$('.userExist', td).css("display", "block");
input.css("background-color", "red");
} else if(data.return == "userNotExist")
{
$('.userExist', td).css("display", "none");
input.css("background-color", "");
}
} catch (e) {
swal("Oops", "Something went wrong!<br>" + e, "error");
}
}, "json");
} else {
$(this).css("background-color", "red");
$('.ruleExcept', td).css("display", "block");
}
});
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/*~~~~~~~~~~~~~~~~TR TO DEL~~~~~~~~~~~~~~~~*/ /*~~~~~~~~~~~~~~~~TR TO DEL~~~~~~~~~~~~~~~~*/
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
@ -141,34 +169,20 @@ $(function() {
} }
}); });
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/*~~~~~~~~~~~~~~~~CONFIRM EDIT~~~~~~~~~~~~~~~~*/ /*~~~~~~~~~~~~~~~~CONFIRM EDIT/NewUser~~~~~~~~~~~~~~~~*/
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
$("#allRecord").on("click", ".confirmEditUser", function() { $("#allRecord").on("click", ".confirmEditUser, .confirmNewUser", function() {
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/*~~~~~~~~~~~~~~~~CALL BACK DE LA VERIF DE MOT DE PASSE~~~~~~~~~~~~~~~~*/ /*~~~~~~~~~~~~~~~~CALL BACK DE LA VERIF DE MOT DE PASSE~~~~~~~~~~~~~~~~*/
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
function callBackVerif(data) { function callBackVerifPW(data) {
var dataToSend = new Object(); //Objet contenant les données à envoyé function postData(dataToSend)
dataToSend["updatePW"] = (data === true ? false : true); //Si maj du mdp data === false
if ($(".pw input", tr).val() === data || data === true) //Verif du mot de passe si nécesaire
{ {
for (i = 0; i < tds.length - 1; i++) //Analyse de chaque td
{
j = $(tds[i]);
className = j.attr('class');
if (className == "pw" && data === true)
value = $('input,select', j).data("oldpw");
else if (className == "login")
value = ltrim(j.html());
else
value = $('input,select', j).val();
dataToSend[className] = value;
}
$.post("post.php", dataToSend, function(data) { $.post("post.php", dataToSend, function(data) {
try { try {
if (data.return == true) { if (data.return == true) {
for (i = 1; i < tds.length - 1; i++) //On change les input en pure html for (i = (dataToSend["newUser"] ?0:1); i < tds.length - 1; i++) //On change les input en pure html
{ {
j = $(tds[i]); j = $(tds[i]);
className = j.attr('class'); className = j.attr('class');
@ -182,14 +196,53 @@ $(function() {
$(that).toggleClass('fa-check fa-edit'); $(that).toggleClass('fa-check fa-edit');
$(that).toggleClass('confirmEditUser editUser'); $(that).toggleClass('confirmEditUser editUser');
} else } else
swal(data); swal("Oops", "Something went wrong!<br> " + JSON.stringify(data), "error");
} catch (e) { } catch (e) {
swal("Oops", "Something went wrong!<br>" + e, "error"); swal("Oops", "Something went wrong!<br>" + e, "error");
} }
console.log(data.return) console.log(data.return)
}, "json"); }, "json");
}
var dataToSend = new Object(); //Objet contenant les données à envoyé
dataToSend["newUser"] = tr.is(".newRecord")
dataToSend["updatePW"] = (data === true ? false : true); //Si maj du mdp data === false
if ($(".pw input", tr).val() === data || data === true && !dataToSend["newUser"]) //Verif du mot de passe si nécesaire
{
for (i = 0; i < tds.length - 1; i++) //Analyse de chaque td
{
j = $(tds[i]);
className = j.attr('class');
if (className == "pw" && data === true)
value = $('input,select', j).data("oldpw");
else if (className == "login" && !dataToSend["newUser"])
value = ltrim(j.html());
else
value = $('input,select', j).val();
dataToSend[className] = value;
}
if(dataToSend["newUser"])
$.post("post.php", {userExist: dataToSend["login"]}, function(data) {
try {
if (data.return == "userExist") {
$('.userExist', tr).css("display", "block");
$(".login input", tr).css("background-color", "red");
swal("Oops", "User "+dataToSend["login"]+" already exist !", "error");
return false;
} else if(data.return == "userNotExist")
{
$('.userExist', tr).css("display", "none");
$(".login input", tr).css("background-color", "");
postData(dataToSend);
}
} catch (e) {
swal("Oops", "Something went wrong!<br>" + e, "error");
}
}, "json");
else
postData(dataToSend);
} else { } else {
swal("Oops", "Password don't match !", "error"); swal("Oops", (dataToSend["newUser"]?"New user need password !":"Password don't match !"), "error");
} }
} }
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
@ -199,7 +252,7 @@ $(function() {
that = this; that = this;
if (mailPatern.test($(".email input", tr).val()) && (passwordPatern.test($(".pw input", tr).val()) || $(".pw input", tr).val() == "")) if (mailPatern.test($(".email input", tr).val()) && (passwordPatern.test($(".pw input", tr).val()) || $(".pw input", tr).val() == ""))
if ($(".pw input", tr).val() === "") { if ($(".pw input", tr).val() === "") {
callBackVerif(true); callBackVerifPW(true);
} }
else { else {
swal({ swal({
@ -212,7 +265,7 @@ $(function() {
type: "password", type: "password",
}, },
}, },
}).then(callBackVerif); }).then(callBackVerifPW);
} else } else
swal("Oops", "Input don't respect rules !", "error"); swal("Oops", "Input don't respect rules !", "error");
}); });

View File

@ -2,7 +2,7 @@
{ {
cursor: pointer; cursor: pointer;
} }
.pw div{ .pw div, .login div{
display:none; display:none;
-moz-border-radius:6px; -moz-border-radius:6px;