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
<?
include("database.php");
/**
* Returns true if the username has been taken
* by another user, false otherwise.
*/
function usernameTaken($username){
}
$q = "select username from users where username = '$username'";
}
/**
* Inserts the given (username, password) pair
* into the database. Returns true on success,
* false otherwise.
*/
function addNewUser($username, $password){
$q = "INSERT INTO users VALUES ('$username', '$password')";
}
/**
* Displays the appropriate message to the user
* after the registration attempt. It displays a
* success or failure status depending on a
* session variable set during registration.
*/
function displayStatus(){
$uname = $_SESSION['reguname'];
if($_SESSION['regresult']){
?>
<h1>Registered!</h1>
<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>
<?
}
else{
?>
<h1>Registration Failed</h1>
<p>We're sorry, but an error has occurred and your registration for the username <b>
<? echo $uname; ?></b>, could not be completed.<br>
Please try again at a later time.</p>
<?
}
unset($_SESSION['reguname']); unset($_SESSION['registered']); unset($_SESSION['regresult']); }
if(isset($_SESSION['registered'])){ /**
* This is the page that will be displayed after the
* registration has been attempted.
*/
?>
<? displayStatus(); ?>
<?
return;
}
/**
* Determines whether or not to show to sign-up form
* based on whether the form has been submitted, if it
* has, check the database for consistency and create
* the new account.
*/
if(isset($_POST['subjoin'])){ /* Make sure all fields were entered */
if(!$_POST['user'] || !$_POST['pass']){
die('You didn't fill in a required field.'); }
/* Spruce up username, check length */
$_POST['user'] = trim($_POST['user']); if(strlen($_POST['user']) > 30
){ die("Sorry, the username is longer than 30 characters, please shorten it."); }
/* Check if username is already in use */
if(usernameTaken($_POST['user'])){
$use = $_POST['user'];
die("Sorry, the username: <strong>$use</strong> is already taken, please pick another one."); }
/* Add the new account to the database */
$md5pass = md5($_POST['pass']); $_SESSION['reguname'] = $_POST['user'];
$_SESSION['regresult'] = addNewUser($_POST['user'], $md5pass);
$_SESSION['registered'] = true;
echo "<meta http-equiv=\"Refresh\" content=\"0;url=$HTTP_SERVER_VARS[PHP_SELF]\">"; return;
}
else{
/**
* This is the page with the sign-up form, the names
* of the input fields are important and should not
* be changed.
*/
?>
<form action="
<? echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post">
<table align="center" border="0" cellspacing="0" cellpadding="3">
<tr><td>Nick:</td><td><input type="text" name="user" maxlength="30"></td></tr>
<tr><td>Haslo:</td><td><input type="password" name="pass" maxlength="30"></td></tr>
<tr><td colspan="2" align="right"><input type="submit" name="subjoin" value="Zarejestruj !"></td></tr>
</table>
</form>
<?
}
?>
Z gory dziekuje za jakas podpowiedz ..
Ten post edytował ArthaS_Delano 20.09.2007, 15:06:54