Advertisement
Guest User

Codul sursa pentru Editare Clienti

a guest
Oct 31st, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.36 KB | None | 0 0
  1. <?php
  2.     $servername = "localhost";
  3.     $username = "root";
  4.     $password = "";
  5.     $DBname = "itmaniax";
  6.     $user = $pass = $email = $confirm = "";
  7.     $user_error = $pass_error = $email_error = $confirm_error = $mesaj = "";
  8.     $valid_user = $valid_pass = $valid_confirm = $valid_email = 0;
  9.     $conn = mysqli_connect($servername, $username, $password, $DBname);
  10.    
  11. function validare()
  12. {
  13.     if(!($GLOBALS['conn']))
  14.     {
  15.         die("Connection to the DB failed: ") . mysqli_connect_error();
  16.         return;
  17.     }
  18.  
  19.     if(empty($_POST["user"]))
  20.     {
  21.         $GLOBALS['user_error'] = "Campul este gol!";
  22.     }
  23.        
  24.     else
  25.     {
  26.         $GLOBALS['user'] = $_POST['user'];
  27.         $sql = "SELECT username FROM itmaniax.client";
  28.         $result = mysqli_query($GLOBALS['conn'], $sql);
  29.        
  30.         while($row = mysqli_fetch_assoc($result))
  31.         {
  32.             if(strcmp($GLOBALS['user'], $row['username']) == 0)
  33.             {
  34.                 $GLOBALS['user_error'] = "Username luat!";
  35.                 $GLOBALS['valid_user'] = 0;
  36.             }
  37.             else if(!preg_match('/^[a-zA-Z ]*$/', $GLOBALS['user']))
  38.             {
  39.                 $GLOBALS['user_error'] = "Username invalid!";
  40.                 $GLOBALS['valid_user'] = 0;
  41.             }
  42.             else
  43.             {
  44.                 $GLOBALS['valid_user'] = 1;
  45.             }
  46.         }
  47.     }
  48.        
  49.     if(empty($_POST["email"]))
  50.     {
  51.         $GLOBALS['email_error'] = "Campul este gol!";
  52.     }
  53.        
  54.     else
  55.     {
  56.         $GLOBALS['email'] = $_POST["email"];
  57.         if(!filter_var($GLOBALS['email'], FILTER_VALIDATE_EMAIL))
  58.         {
  59.             $GLOBALS['email_error'] = "E-mail invalid!";
  60.         }
  61.         else
  62.         {
  63.             $GLOBALS['valid_email'] = 1;
  64.         }
  65.     }
  66.    
  67.     if(empty($_POST["pass"]))
  68.         $GLOBALS['pass_error'] = "Campul este gol!";
  69.     else
  70.     {
  71.         $GLOBALS['valid_pass'] = 1;
  72.         $GLOBALS['pass'] = $_POST["pass"];
  73.     }
  74.    
  75.     if(empty($_POST["confirm"]))
  76.         $GLOBALS['confirm_error'] = "Campul este gol!";
  77.     else
  78.     {
  79.         $GLOBALS['confirm'] = $_POST["confirm"];
  80.         if(strcmp($GLOBALS['confirm'], $GLOBALS['pass']) == 0)
  81.         {
  82.             $GLOBALS['valid_confirm'] = 1;
  83.         }
  84.         else
  85.         {
  86.             $GLOBALS['confirm_error'] = "Parola nu e identica!";
  87.         }
  88.     }
  89. }
  90.  
  91. function inserare_client()
  92. {
  93.     if($GLOBALS['valid_user'] && $GLOBALS['valid_pass'] && $GLOBALS['valid_confirm'] && $GLOBALS['valid_email'] && $GLOBALS['conn'])
  94.     {
  95.         $sql = "INSERT INTO itmaniax.client(username, password, email) VALUES ('".$GLOBALS['user']."', '".$GLOBALS['pass']."', '".$GLOBALS['email']."')";
  96.        
  97.         if(mysqli_query($GLOBALS['conn'], $sql))
  98.         {
  99.             $GLOBALS['mesaj'] = "A fost adaugat cu succes!";
  100.         }
  101.         else
  102.         {
  103.             $GLOBALS['mesaj'] = "Error: " . mysqli_error($GLOBALS['conn']);
  104.         }
  105.     }
  106.     mysqli_close($GLOBALS['conn']);
  107. }
  108.  
  109. function stergere()
  110. {
  111.     $gasit = 0;
  112.    
  113.     if(!($GLOBALS['conn']))
  114.     {
  115.         die("Connection to the DB failed: ") . mysqli_connect_error();
  116.         return;
  117.     }
  118.  
  119.     if(empty($_POST["user"]))
  120.     {
  121.         $GLOBALS['user_error'] = "Campul este gol!";
  122.     }
  123.     else
  124.     {
  125.         $GLOBALS['user'] = $_POST['user'];
  126.         $sql = "SELECT username FROM itmaniax.client";
  127.         $result = mysqli_query($GLOBALS['conn'], $sql);
  128.        
  129.         while($row = mysqli_fetch_assoc($result))
  130.         {
  131.             if(strcmp($GLOBALS['user'], $row['username']) == 0)
  132.             {
  133.                 $GLOBALS['user'] = $row['username'];
  134.                 $gasit = 1;
  135.                 break;
  136.             }
  137.         }
  138.     }
  139.    
  140.     if(!$gasit)
  141.     {
  142.         $GLOBALS['user_error'] = "Userul nu exista!";
  143.     }
  144.     else
  145.     {
  146.         $sql = "DELETE FROM itmaniax.client WHERE username = '".$GLOBALS['user']."'";
  147.    
  148.         if(mysqli_query($GLOBALS['conn'], $sql))
  149.         {
  150.             $GLOBALS['mesaj'] = "Clientul a fost sters cu success!";
  151.         }
  152.         else
  153.         {
  154.             $GLOBALS['mesaj'] = "Error: " . mysqli_error($GLOBALS['conn']);
  155.         }
  156.     }
  157.     mysqli_close($GLOBALS['conn']);    
  158. }
  159.  
  160. function editeaza()
  161. {
  162.     $gasit = 0;
  163.    
  164.     if(!($GLOBALS['conn']))
  165.     {
  166.         die("Connection to the DB failed: ") . mysqli_connect_error();
  167.         return;
  168.     }
  169.  
  170.     if(empty($_POST["user"]))
  171.     {
  172.         $GLOBALS['user_error'] = "Campul este gol!";
  173.     }
  174.    
  175.     else
  176.     {
  177.         $GLOBALS['user'] = $_POST['user'];
  178.         $sql = "SELECT username FROM itmaniax.client";
  179.         $result = mysqli_query($GLOBALS['conn'], $sql);
  180.        
  181.         while($row = mysqli_fetch_assoc($result))
  182.         {
  183.             if(strcmp($GLOBALS['user'], $row['username']) == 0)
  184.             {
  185.                 $gasit = 1;
  186.                 $GLOBALS['valid_user'] = 1;
  187.                 break;
  188.             }
  189.         }
  190.     }
  191.  
  192.     if($gasit == 0)
  193.     {
  194.         $GLOBALS['user_error'] = "Userul nu exista!";
  195.     }
  196.     else
  197.     {
  198.         if(empty($_POST["email"])) // daca nu a introdus nimic in campul "E-mail" va ramane valoarea din db dupa ce va da submit
  199.         {
  200.             $sql = "SELECT email FROM itmaniax.client WHERE username = '".$GLOBALS['user']."'";
  201.             $result = mysqli_query($GLOBALS['conn'], $sql);
  202.             $GLOBALS['email'] = $result; // retin valoarea emailului in $GLOBALS['email'], dar in query imi va da eroare ca nu il putea converti in string
  203.             $GLOBALS['valid_email'] = 1;
  204.         }
  205.        
  206.         else // altfel se schimba cu valoarea introdusa
  207.         {
  208.             $GLOBALS['email'] = $_POST["email"]; // aici functioneaza corect
  209.             if(!filter_var($GLOBALS['email'], FILTER_VALIDATE_EMAIL))
  210.             {
  211.                 $GLOBALS['email_error'] = "E-mail invalid!";
  212.             }
  213.             else
  214.             {
  215.                 $GLOBALS['valid_email'] = 1;
  216.             }
  217.         }
  218.        
  219.         if(empty($_POST["pass"])) // daca nu introduce nimic in campul parola, ramane aceeasi
  220.         {
  221.             $sql = "SELECT password FROM itmaniax.client WHERE username = '".$GLOBALS['user']."'";
  222.             $result = mysqli_query($GLOBALS['conn'], $sql);
  223.             $GLOBALS['pass'] = $result;
  224.             $GLOBALS['valid_pass'] = 1;
  225.         }
  226.         else // altfel se schimba
  227.         {
  228.             $GLOBALS['valid_pass'] = 1;
  229.             $GLOBALS['pass'] = $_POST["pass"]; // aici e bine
  230.         }
  231.        
  232.         if($GLOBALS['valid_user'] && $GLOBALS['valid_pass'] && $GLOBALS['valid_email'])
  233.         {
  234.             // aici apare eroarea in caz ca nu se introduce nimic,
  235.             $sql = "UPDATE itmaniax.client SET password = '".$GLOBALS['pass']."', email = '".$GLOBALS['email']."' WHERE username = '".$GLOBALS['user']."'";
  236.    
  237.             if(mysqli_query($GLOBALS['conn'], $sql))
  238.             {
  239.                 $GLOBALS['mesaj'] = "Clientul a fost editat cu success!";
  240.             }
  241.             else
  242.             {
  243.                 $GLOBALS['mesaj'] = "Error: " . mysqli_error($GLOBALS['conn']);
  244.             }
  245.         }        
  246.     }
  247.     mysqli_close($GLOBALS['conn']);      
  248. }
  249.  
  250. if(isset($_POST['submit']))
  251. {
  252.     validare();
  253.     inserare_client();
  254. }
  255.  
  256. if(isset($_POST['submit_sterge']))
  257. {
  258.     stergere();
  259. }
  260.  
  261. if(isset($_POST['submit_editeaza']))
  262. {
  263.     editeaza();
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement