Pomoc - Szukaj - U¿ytkownicy - Kalendarz
Pe³na wersja: [PHP][MySQL] Id, klucz glowny i rejestracja
Forum PHP.pl > Forum > Przedszkole
ArthaS_Delano
Mam powazny problem.. Otoz mam kod do rejestracji i logowania na stronce.. Wszystko dziala idealnie gdy mam w bazie tylko dane typu user password itd.. Niestety problem pojawia sie gdy dodam do tabeli users rekord 'id' , wiadomo ze chce dac je jako pole auto_increment a zeby to zrobic musze ustawic je jako klucz glowny.. I tu sie pojawia problem bo podczas rejestracji wyswietla sie blad opisany w kodzie jako "registration failed".. Nie wiem zupelnie gdzie lezy problem, moglbym oczywiscie jako identyfikator brac username ale wiadomo ze latwiej byloby poslugiwac sie 'id'..

EDIT

Sprawdzilem na polu username i chodzi o to ze zadne pole tabeli nie moze byc kluczem glownym, obojetnie co ustawie jako klucz glowny to rejestracja nie dziala.. Co zrobic zeby tak nie reagowalo ?

REJESTRACJA.PHP

  1. <?
  2. include("database.php");
  3. /**
  4.  * Returns true if the username has been taken
  5.  * by another user, false otherwise.
  6.  */
  7. function usernameTaken($username){
  8.  global $conn;
  9. $username = addslashes($username);
  10.  }
  11.  $q = "select username from users where username = '$username'";
  12.  $result = mysql_query($q,$conn);
  13.  return (mysql_numrows($result) > 0);
  14. }
  15.  
  16. /**
  17.  * Inserts the given (username, password) pair
  18.  * into the database. Returns true on success,
  19.  * false otherwise.
  20.  */
  21. function addNewUser($username, $password){
  22.  global $conn;
  23.  $q = "INSERT INTO users VALUES ('$username', '$password')";
  24.  return mysql_query($q,$conn);
  25. }
  26.  
  27. /**
  28.  * Displays the appropriate message to the user
  29.  * after the registration attempt. It displays a 
  30.  * success or failure status depending on a
  31.  * session variable set during registration.
  32.  */
  33. function displayStatus(){
  34.  $uname = $_SESSION['reguname'];
  35.  if($_SESSION['regresult']){
  36. ?>
  37.  
  38. <h1>Registered!</h1>
  39. <p>Thank you <b><? echo $uname; ?></b>, your information has been added to the database, you may now <a href="main.php" title="Login">log in</a>.</p>
  40.  
  41. <?
  42.  }
  43.  else{
  44. ?>
  45.  
  46. <h1>Registration Failed</h1>
  47. <p>We're sorry, but an error has occurred and your registration for the username <b><? echo $uname; ?></b>, could not be completed.<br>
  48. Please try again at a later time.</p>
  49.  
  50. <?
  51.  }
  52.  unset($_SESSION['reguname']);
  53.  unset($_SESSION['registered']);
  54.  unset($_SESSION['regresult']);
  55. }
  56.  
  57. if(isset($_SESSION['registered'])){
  58. /**
  59.  * This is the page that will be displayed after the
  60.  * registration has been attempted.
  61.  */
  62. ?>
  63.  
  64.  
  65.  
  66. <? displayStatus(); ?>
  67.  
  68.  
  69.  
  70. <?
  71.  return;
  72. }
  73.  
  74. /**
  75.  * Determines whether or not to show to sign-up form
  76.  * based on whether the form has been submitted, if it
  77.  * has, check the database for consistency and create
  78.  * the new account.
  79.  */
  80. if(isset($_POST['subjoin'])){
  81.  /* Make sure all fields were entered */
  82.  if(!$_POST['user'] || !$_POST['pass']){
  83. die('You didn't fill in a required field.');
  84.  }
  85.  
  86.  /* Spruce up username, check length */
  87.  $_POST['user'] = trim($_POST['user']);
  88.  if(strlen($_POST['user']) > 30){
  89. die("Sorry, the username is longer than 30 characters, please shorten it.");
  90.  }
  91.  
  92.  /* Check if username is already in use */
  93.  if(usernameTaken($_POST['user'])){
  94. $use = $_POST['user'];
  95. die("Sorry, the username: <strong>$use</strong> is already taken, please pick another one.");
  96.  }
  97.  
  98.  /* Add the new account to the database */
  99.  $md5pass = md5($_POST['pass']);
  100.  $_SESSION['reguname'] = $_POST['user'];
  101.  $_SESSION['regresult'] = addNewUser($_POST['user'], $md5pass);
  102.  $_SESSION['registered'] = true;
  103.  echo "<meta http-equiv=\"Refresh\" content=\"0;url=$HTTP_SERVER_VARS[PHP_SELF]\">";
  104.  return;
  105. }
  106. else{
  107. /**
  108.  * This is the page with the sign-up form, the names
  109.  * of the input fields are important and should not
  110.  * be changed.
  111.  */
  112. ?>
  113.  
  114. <form action="<? echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post">
  115. <table align="center" border="0" cellspacing="0" cellpadding="3">
  116. <tr><td>Nick:</td><td><input type="text" name="user" maxlength="30"></td></tr>
  117. <tr><td>Haslo:</td><td><input type="password" name="pass" maxlength="30"></td></tr>
  118. <tr><td colspan="2" align="right"><input type="submit" name="subjoin" value="Zarejestruj !"></td></tr>
  119. </table>
  120. </form>
  121.  
  122.  
  123.  
  124. <?
  125. }
  126. ?>


Z gory dziekuje za jakas podpowiedz ..
loonger
popraw na:
  1. <?php
  2. function addNewUser($username, $password){
  3.  global $conn;
  4.  $q = "INSERT INTO users (username,password) VALUES ('$username', '$password')";
  5.  return mysql_query($q,$conn);
  6. }
  7. ?>
ArthaS_Delano
Nie mam zamiaru nabijac postow, ale dziekuje bardzo, probowalem zmieniac inne rzeczy a wciaz mam problem z konkretyzacja zapytan smile.gif Jeszcze raz dziekuje smile.gif
To jest wersja lo-fi g³ównej zawarto¶ci. Aby zobaczyæ pe³n± wersjê z wiêksz± zawarto¶ci±, obrazkami i formatowaniem proszê kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.