Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Zapytanie do bazy
Dukov
post
Post #1





Grupa: Zarejestrowani
Postów: 69
Pomógł: 0
Dołączył: 21.03.2017

Ostrzeżenie: (0%)
-----


Wszystko już jest na dobrej drodze, problem mam jeszcze z zapytaniem do bazy
  1. <?php
  2. $avatar;
  3. if ($_POST['name'] != null){
  4. $userName = $_POST['name'];
  5. }
  6. if ($_POST['city'] != null ) {
  7. $userCity = $_POST['city'];
  8. }
  9. if ($_POST['desc'] != null){
  10. $userDesc = $_POST['desc'];
  11. }
  12.  
  13. if ($_POST['login'] != '' && $_POST['pass'] != '' && $_POST['mail'] != '') {
  14.  
  15.  
  16. if (strlen($_POST['login']) > 3 && strlen($_POST['login']) < 10 && strlen($_POST['pass']) > 5 && strlen($_POST['pass']) < 16 ) {
  17. if (preg_match('/^[a-zA-Z0-9\.\-_]+\@[a-zA-Z0-9\.\-_]+\.[a-z]{2,4}$/D', $_POST['mail']) && preg_match('/^[a-zA-Z0-9\.\-_]/', $_POST['login']) ) {
  18.  
  19.  
  20. $userLogin = $_POST['login'];
  21. $userPass = $_POST['pass'];
  22. $userMail = $_POST['mail'];
  23. $len = 20;
  24. $userID = substr(sha1(rand(1,10000)),0,$len);
  25. echo $userID;
  26. if($_FILES['filename']['name'] != ''){
  27. $avatar = fileReceive();
  28. }
  29. reg();
  30. }
  31. else
  32. {
  33. header("Location: ../reg.php?reg=badchars");
  34. }
  35. }
  36. else
  37. {
  38. header("Location: ../reg.php?reg=shortpole");
  39. }
  40. }
  41. else
  42. {
  43. header("Location: ../reg.php?reg=emptyf");
  44. }
  45. function reg(){
  46. global $userID, $userMail, $userPass, $userLogin, $userDesc, $userCity, $userName, $avatar;
  47. try {
  48. $host = 'localhost';
  49. $baza = 'user';
  50. $uzytkownik = 'adamek';
  51. $haslo = 'aaaaa5';
  52.  
  53.  
  54. $conn = new PDO("mysql:host=$host;dbname=$baza", $uzytkownik, $haslo);
  55. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  56. $stmt = $conn->prepare("SELECT Nazwa AND mail FROM Users WHERE Nazwa = '$userLogin' OR mail = '$userMail'");
  57. $stmt->execute();
  58. $row = $stmt->fetch();
  59. if($row['Nazwa'] == 0 && $row['mail'] == 0){
  60. $stmt = $conn->exec("INSERT INTO Users ( ID , Nazwa , Haslo , mail , name , city , image , desci , activate , admin ) VALUES ('$userID','$userLogin', '$userPass', '$userMail', '$userName', '$userCity, $avatar', '$userDesc', 0 , 0 ");
  61.  
  62. echo "ok";
  63. #$sendMail();
  64. }
  65.  
  66. else{
  67. header("Location: ../reg.php?reg=userexists");
  68. }
  69. }
  70. catch(PDOException $e)
  71. {
  72. echo "duppppp";
  73. }
  74. }
  75.  
  76.  
  77.  
  78.  
  79. function fileReceive(){
  80. $max_size = 1024*1024;
  81. if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
  82. if ($_FILES['filename']['size'] > $max_size) {
  83. echo 'Błąd! Plik jest za duży!';
  84. }
  85. else {
  86. echo 'Odebrano plik. Początkowa nazwa: '.$_FILES['filename']['name'];
  87. echo '<br/>';
  88. if (isset($_FILES['filename']['type'])) {
  89. echo 'Typ: '.$_FILES['filename']['type'].'<br/>';
  90. }
  91. $strExt = substr( $_FILES['filename']['name'], strrpos( $_FILES['filename']['name'], '.' ) + 1 );
  92. echo "Rozszerzenie: ". $strExt;
  93. $len = 20;
  94. $randstring = substr(sha1(rand(1,10000)),0,$len).".jpg";
  95. echo $randstring;
  96. echo $_FILES['filename']['name'];
  97.  
  98. if ($strExt == 'jpg' || 'JPG' || 'jpeg' || 'JPEG'){
  99. move_uploaded_file($_FILES['filename']['tmp_name'],
  100. $_SERVER['DOCUMENT_ROOT'].'/avatars/'.$_FILES['filename']['name']);
  101.  
  102. }
  103. else{
  104. echo "niy!!";}
  105. }
  106. } else {
  107. echo 'Błąd przy przesyłaniu danych!';
  108. }
  109.  
  110. rename('../avatars/'.$_FILES['filename']['name'], "../avatars/". $randstring);
  111. return $randstring;
  112. }
  113.  
  114.  
  115.  
  116. function sendMail(){
  117. $addressee = 'ajjambor912@gmail.com';
  118. @$email = $_POST['email'];
  119.  
  120. @$content = 'Name '.$_POST['username'].' Phone: '.$_POST['userphone'].' Content: '.$_POST['message'];
  121.  
  122. $header = "From: ".$_POST['email']." \nContent-Type:".
  123. ' text/plain;charset="iso-8859-2"'.
  124. "\nContent-Transfer-Encoding: 8bit";
  125. if (mail($addressee, 'Message from NAhandyman: ', $content , $header))
  126.  
  127. header('Location:index.php?send=true');
  128. else
  129. header('Location:index.php?send=false');
  130. }
  131. ?>


Tutaj jest błąd
  1. $stmt = $conn->exec("INSERT INTO Users ( ID , Nazwa , Haslo , mail , name , city , image , desci , activate , admin ) VALUES ('$userID','$userLogin', '$userPass', '$userMail', '$userName', '$userCity, $avatar', '$userDesc', 0 , 0 ");


Skąd wiem? Bo jak wykomentuje to skrypt leci do kolejnej lini i mam wyświetla się ok, z echa. Jeśli skasuje komentarz to przechodzi do instrukcji catch i z echa wyświetla się duppp.
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Dukov
post
Post #2





Grupa: Zarejestrowani
Postów: 69
Pomógł: 0
Dołączył: 21.03.2017

Ostrzeżenie: (0%)
-----


AuthorIP miałem jako INT, a w adresie są kropki. Dzięki, na przyszłość będę wiedział, że jak coś wygląda ok, to żeby sprawdzić sam SQL, aczkolwiek możesz mi powiedzieć, czy dobrze robię, ze każda funkcja, która potrzebuje coś z bazy łączy się z bazą? Czy lepiej napisać osobną funkcje do łączenia z bazą? Albo łączyć się tylko w głównej funkcji i wywoływać w niej pozostałe?

EDIT
Działa.

Ten post edytował Dukov 7.08.2017, 20:38:18
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 6.10.2025 - 06:01