Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [HTML][MySQL][PHP]Dodanie do formularza emaila
Leilang
post 9.10.2018, 13:12:30
Post #1





Grupa: Zarejestrowani
Postów: 23
Pomógł: 0
Dołączył: 11.07.2018

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


Hej,
Dorwałem darmowy szablon z formularzem logowania. Wszystko fajnie działa ale potrzebuje też żeby użytkownik przy rejestracji podawał email. No i własnie przy przerobieniu kodu (dodaniu inputa z emailem i dopisaniu kodu php) zwraca mi komunikat:

  1. Warning: mysqli_stmt_close() expects parameter 1 to be mysqli_stmt, boolean given in /uploads/register.php on line 86
  2.  
  3. Warning: mysqli_stmt_close() expects parameter 1 to be mysqli_stmt, boolean given in /uploads/register.php on line 146


Będę wdzięczny za pomoc wink.gif

To co dopisałem
  1.  
  2.  
  3.  
  4. // Validate email
  5. if(empty(trim($_POST["email"]))){
  6. $email_err = "Proszę podać email.";
  7. } else{
  8. // Prepare a select statement
  9. $sql = "SELECT id FROM users WHERE email = ?";
  10.  
  11. if($stmt = mysqli_prepare($link, $sql)){
  12. // Bind variables to the prepared statement as parameters
  13. mysqli_stmt_bind_param($stmt, "s", $param_email);
  14.  
  15. // Set parameters
  16. $param_email = trim($_POST["email"]);
  17.  
  18.  
  19. // Attempt to execute the prepared statement
  20. if(mysqli_stmt_execute($stmt)){
  21. /* store result */
  22. mysqli_stmt_store_result($stmt);
  23.  
  24. if(mysqli_stmt_num_rows($stmt) == 1){
  25. $email_err = "Ten email już był wykorzystany.";
  26. } else{
  27. $email = trim($_POST["email"]);
  28. }
  29. } else{
  30. echo "Oops! Coś poszło nie tak spróbuj później.";
  31. }
  32. }
  33.  
  34. // Close statement
  35. mysqli_stmt_close($stmt);
  36. }
  37.  
  38.  
  39.  

  1. <div class="form-group <?php echo (!empty($email_err)) ? 'has-error' : ''; ?>">
  2. <label>Email</label>
  3. <input type="text" name="email" class="form-control" value="<?php echo $email; ?>">
  4. <span class="help-block"><?php echo $email_err; ?></span>
  5. </div>



Cały kod :

  1. <?php
  2. // Include config file
  3. require_once "config.php";
  4.  
  5. // Define variables and initialize with empty values
  6. $username = $password = $email = $confirm_password = "";
  7. $username_err = $password_err = $email_err = $confirm_password_err = "";
  8.  
  9. // Processing form data when form is submitted
  10. if($_SERVER["REQUEST_METHOD"] == "POST"){
  11.  
  12. // Validate username
  13. if(empty(trim($_POST["username"]))){
  14. $username_err = "Please enter a username.";
  15. } else{
  16. // Prepare a select statement
  17. $sql = "SELECT id FROM users WHERE username = ?";
  18.  
  19. if($stmt = mysqli_prepare($link, $sql)){
  20. // Bind variables to the prepared statement as parameters
  21. mysqli_stmt_bind_param($stmt, "s", $param_username);
  22.  
  23. // Set parameters
  24. $param_username = trim($_POST["username"]);
  25.  
  26.  
  27. // Attempt to execute the prepared statement
  28. if(mysqli_stmt_execute($stmt)){
  29. /* store result */
  30. mysqli_stmt_store_result($stmt);
  31.  
  32. if(mysqli_stmt_num_rows($stmt) == 1){
  33. $username_err = "This username is already taken.";
  34. } else{
  35. $username = trim($_POST["username"]);
  36. }
  37. } else{
  38. echo "Oops! Something went wrong. Please try again later.";
  39. }
  40. }
  41.  
  42. // Close statement
  43. mysqli_stmt_close($stmt);
  44. }
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52. ///////////////////// DODANE PHP ///////////////////////////
  53.  
  54.  
  55. // Validate email
  56. if(empty(trim($_POST["email"]))){
  57. $email_err = "Proszę podać email.";
  58. } else{
  59. // Prepare a select statement
  60. $sql = "SELECT id FROM users WHERE email = ?";
  61.  
  62. if($stmt = mysqli_prepare($link, $sql)){
  63. // Bind variables to the prepared statement as parameters
  64. mysqli_stmt_bind_param($stmt, "s", $param_email);
  65.  
  66. // Set parameters
  67. $param_email = trim($_POST["email"]);
  68.  
  69.  
  70. // Attempt to execute the prepared statement
  71. if(mysqli_stmt_execute($stmt)){
  72. /* store result */
  73. mysqli_stmt_store_result($stmt);
  74.  
  75. if(mysqli_stmt_num_rows($stmt) == 1){
  76. $email_err = "Ten email już był wykorzystany.";
  77. } else{
  78. $email = trim($_POST["email"]);
  79. }
  80. } else{
  81. echo "Oops! Coś poszło nie tak spróbuj później.";
  82. }
  83. }
  84.  
  85. // Close statement
  86. mysqli_stmt_close($stmt);
  87. }
  88.  
  89.  
  90. ///////////////////// DODANE PHP KONIEC///////////////////////////
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101. // Validate password
  102. if(empty(trim($_POST["password"]))){
  103. $password_err = "Podaj hasło.";
  104. } elseif(strlen(trim($_POST["password"])) < 6){
  105. $password_err = "Hasło musi mieć co najmniej 6 znaków.";
  106. } else{
  107. $password = trim($_POST["password"]);
  108. }
  109.  
  110. // Validate confirm password
  111. if(empty(trim($_POST["confirm_password"]))){
  112. $confirm_password_err = "Potwierdź hasło.";
  113. } else{
  114. $confirm_password = trim($_POST["confirm_password"]);
  115. if(empty($password_err) && ($password != $confirm_password)){
  116. $confirm_password_err = "Hasło nie pasuje.";
  117. }
  118. }
  119.  
  120. // Check input errors before inserting in database
  121. if(empty($username_err) && empty($email_err) && empty($password_err) && empty($confirm_password_err)){
  122.  
  123. // Prepare an insert statement
  124. $sql = "INSERT INTO users (username, password, email) VALUES (?, ?, ?)";
  125.  
  126. if($stmt = mysqli_prepare($link, $sql)){
  127. // Bind variables to the prepared statement as parameters
  128. mysqli_stmt_bind_param($stmt, "ss", $param_username, $param_password, $param_email);
  129.  
  130. // Set parameters
  131. $param_username = $username;
  132. $param_email = $email;
  133. $param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash
  134.  
  135. // Attempt to execute the prepared statement
  136. if(mysqli_stmt_execute($stmt)){
  137. // Redirect to login page
  138.  
  139. echo("<script>location.href = '/login.php?msg=$msg';</script>");
  140. } else{
  141. echo "Something went wrong. Please try again later.";
  142. }
  143. }
  144.  
  145. // Close statement
  146. mysqli_stmt_close($stmt);
  147. }
  148.  
  149. // Close connection
  150. mysqli_close($link);
  151. }
  152. ?>
  153.  
  154. <!DOCTYPE html>
  155. <html lang="en">
  156. <head>
  157. <meta charset="UTF-8">
  158. <title>Sign Up</title>
  159. <link rel="stylesheet" type="text/css" href="css/mystylenew2.css">
  160. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
  161. <style type="text/css">
  162. body{ font-family: 'Fira Sans', sans-serif; font-size: 14px; }
  163. .wrapper{ width: 350px; padding: 20px; }
  164. </style>
  165. </head>
  166. <body>
  167. <BR><br><br><BR><br><br>
  168. <div class="wrapper"; style = "text-align: center; background-color:WHITE; border-radius: 25px;";>
  169. <h2>Zarejestruj się</h2>
  170. <p>Wypełnij pola aby założyć konto.</p>
  171. <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
  172. <div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
  173. <label>Użytkownik</label>
  174. <input type="text" name="username" class="form-control" value="<?php echo $username; ?>">
  175. <span class="help-block"><?php echo $username_err; ?></span>
  176. </div>
  177.  
  178.  
  179.  
  180. <div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
  181. <label>Hasło</label>
  182. <input type="password" name="password" class="form-control" value="<?php echo $password; ?>">
  183. <span class="help-block"><?php echo $password_err; ?></span>
  184. </div>
  185.  
  186.  
  187.  
  188. <!-- //////////////////////////// DODANE HTML ////////////////////////////////////////////////////// -->
  189. <div class="form-group <?php echo (!empty($email_err)) ? 'has-error' : ''; ?>">
  190. <label>Email</label>
  191. <input type="text" name="email" class="form-control" value="<?php echo $email; ?>">
  192. <span class="help-block"><?php echo $email_err; ?></span>
  193. </div>
  194. <!-- //////////////////////////// DODANE HTML KONIEC ////////////////////////////////////////////////////// -->
  195.  
  196.  
  197.  
  198.  
  199. <div class="form-group <?php echo (!empty($confirm_password_err)) ? 'has-error' : ''; ?>">
  200. <label>Potwierdź hasło</label>
  201. <input type="password" name="confirm_password" class="form-control" value="<?php echo $confirm_password; ?>">
  202. <span class="help-block"><?php echo $confirm_password_err; ?></span>
  203. </div>
  204.  
  205. <div class="form-group">
  206. <input type="submit" class="btn btn-primary" value="Wyślij">
  207. <input type="reset" class="btn btn-default" value="Resetuj">
  208. </div>
  209. <p>Masz już konto? <a href="login.php">Zaloguj się tutaj</a>.</p>
  210. </form>
  211.  
  212. </div>
  213. </body>
  214. </html>


Go to the top of the page
+Quote Post
nospor
post 9.10.2018, 13:56:09
Post #2





Grupa: Moderatorzy
Postów: 36 440
Pomógł: 6290
Dołączył: 27.12.2004




Wynika jakbys w tabeli nie mial pola o nazwie "email"


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
Leilang
post 9.10.2018, 14:19:19
Post #3





Grupa: Zarejestrowani
Postów: 23
Pomógł: 0
Dołączył: 11.07.2018

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


Mam tabele "users" a w niej kolumny: id, username, password, email, created_at. To chyba nie to ?
Z bazą też się łączy poprawnie bo bez mojego inputa dla emaila wszystko działało wink.gif

EDIT:

Wybacz miałeś rację, nadpisywałem nie tą bazę co trzeba.. Wszystko działa, dziękuje za szybką pomoc !

Ten post edytował Leilang 9.10.2018, 14:43:39
Go to the top of the page
+Quote Post
nospor
post 9.10.2018, 14:36:46
Post #4





Grupa: Moderatorzy
Postów: 36 440
Pomógł: 6290
Dołączył: 27.12.2004




No to wyswietl sobie blad zapytania bo ewidentnie przy prepare z nowym polem email baza ci sie wywala - bedzie wszystko jasne


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post

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

 



RSS Wersja Lo-Fi Aktualny czas: 29.03.2024 - 16:08