- Home
- Categorie
- Coding e Sistemistica
- MYSQL e altri Database
- Errore registrazione
-
Errore registrazione
Buonasera ragazzi, ho inserito nel database una riga "RUOLO" ma quando vado a registrare l'utente appare questo errore
ERRORE: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'ruolo' cannot be null
Ho provato ad inserire il ruolo all'interno del form (il ruolo sarà modificabile solo da pagina esterna o DB, quindi non serve il campo per l'inserimento nella registrazione) ma probabilmente sbaglio qualcosa..
FORM REGISTRAZIONE
[PHP]<?php require('includes/config.php');
// se effettuato l'accesso reindirizza alla pagina dei membriif( $user->is_logged_in() ){ header('Location: memberpage.php'); exit(); }
// se il modulo è stato inviato, elaboraloif(isset($_POST['submit'])){
if (!isset($_POST['username'])) $error[] = "Please fill out all fields"; if (!isset($_POST['email'])) $error[] = "Please fill out all fields"; if (!isset($_POST['password'])) $error[] = "Please fill out all fields";
$username = $_POST['username'];
// validazione if(!$user->isValidUsername($username)){ $error[] = 'Usernames must be at least 3 Alphanumeric characters'; } else { $stmt = $db->prepare('SELECT username FROM members WHERE username = :username'); $stmt->execute(array(':username' => $username)); $row = $stmt->fetch(PDO::FETCH_ASSOC);
if(!empty($row['username'])){ $error[] = 'Nome inserito già in uso.'; }
}
if(strlen($_POST['password']) < 3){ $error[] = 'Password troppo corta.'; }
if(strlen($_POST['passwordConfirm']) < 3){ $error[] = 'La password confermata è troppo corta'; }
if($_POST['password'] != $_POST['passwordConfirm']){ $error[] = 'le password non corrispondono'; }
//validazione email $email = htmlspecialchars_decode($_POST['email'], ENT_QUOTES); if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ $error[] = 'Inserisci un indirizzo email valido.'; } else { $stmt = $db->prepare('SELECT email FROM members WHERE email = :email'); $stmt->execute(array(':email' => $email)); $row = $stmt->fetch(PDO::FETCH_ASSOC);
if(!empty($row['email'])){ $error[] = 'Email inserita già in uso.'; }
}// se non sono stati creati errori, continua if(!isset($error)){
//hash the password $hashedpassword = $user->password_hash($_POST['password'], PASSWORD_BCRYPT);
//create the activasion code $activasion = md5(uniqid(rand(),true));
try {
// inserisce nel database con un'istruzione preparata $stmt = $db->prepare('INSERT INTO members (username,password,email,ruolo,active) VALUES (:username, :password, :email, :ruolo, :active)'); $stmt->execute(array( ':username' => $username, ':password' => $hashedpassword, ':email' => $email, ':ruolo' => $ruolo, ':active' => $activasion )); $id = $db->lastInsertId('memberID');
//invia email $to = $_POST['email']; $subject = "Registration Confirmation"; $body = "<p>Registrazione avvenuta con successo.</p> <p>Per attivare il tuo account, clicca sul link: <a href='".DIR."activate.php?x=$id&y=$activasion'>".DIR."activate.php?x=$id&y=$activasion</a></p> <p>Saluti, Davide Marcellino</p>";
$mail = new Mail(); $mail->setFrom(SITEEMAIL); $mail->addAddress($to); $mail->subject($subject); $mail->body($body); $mail->send();
// reindirizza alla pagina dell'indice header('Location: index.php?action=joined'); exit;
// else cattura l'eccezione e mostra l'errore. } catch(PDOException $e) { $error[] = $e->getMessage(); }
}
}
// definisce il titolo della pagina$title = 'Demo';
// include modello di intestazionerequire('layout/header.php');?><div class="container">
<div class="row">
<div class="col-xs-12 col-sm-8 col-md-6 col-sm-offset-2 col-md-offset-3"> <form role="form" method="post" action="" autocomplete="off"> <h2>REGISTRATI ADESSO!</h2> <p>Sei già registrato? <a href='login.php'>Login</a></p> <hr>
<?php // controlla eventuali errori if(isset($error)){ foreach($error as $error){ echo '<p class="bg-danger">'.$error.'</p>'; } }
// se l'azione si unisce mostra il successo if(isset($_GET['action']) && $_GET['action'] == 'joined'){ echo "<h2 class='bg-success'>Registrazione avvenuta con successo, controlla la tua email per attivare il tuo account.</h2>"; } ?>
<div class="form-group"> <input type="text" name="username" id="username" class="form-control input-lg" placeholder="Nome utente" value="<?php if(isset($error)){ echo htmlspecialchars($_POST['username'], ENT_QUOTES); } ?>" tabindex="1"> </div> <div class="form-group"> <input type="email" name="email" id="email" class="form-control input-lg" placeholder="Email" value="<?php if(isset($error)){ echo htmlspecialchars($_POST['email'], ENT_QUOTES); } ?>" tabindex="2"> </div> <div class="row"> <div class="col-xs-6 col-sm-6 col-md-6"> <div class="form-group"> <input type="password" name="password" id="password" class="form-control input-lg" placeholder="Password" tabindex="3"> </div> </div> <div class="col-xs-6 col-sm-6 col-md-6"> <div class="form-group"> <input type="password" name="passwordConfirm" id="passwordConfirm" class="form-control input-lg" placeholder="Conferma password" tabindex="4"> </div> </div> </div>
<div class="row"> <div class="col-xs-6 col-md-6"><input type="submit" name="submit" value="Register" class="btn btn-primary btn-block btn-lg" tabindex="5"></div> </div> </form> </div> </div>
</div>
<?php// include modello di intestazionerequire('layout/footer.php');?>[/PHP]
[TABLE="class: data"]
[TR="class: even"]
[/TR][TH="align: left"][/TH]
[TH="align: left"]Nome[/TH]
[TH="align: left"]Tipo[/TH]
[TH="align: left"]Codifica caratteri[/TH]
[TH="align: left"]Attributi[/TH]
[TH="align: left"]Null[/TH]
[TH="align: left"]Predefinito[/TH]
[TH="align: left"]Extra[/TH]
[TH="class: action, colspan: 8, align: left"]Azione[/TH]
[/TR]
[TR="class: odd"]
[TD="class: center, align: center"][/TD]
[TD="class: right, align: right"]1[/TD]
[TH="class: nowrap, align: left"]memberID[/TH]
[TD="class: nowrap"]int(11)[/TD]
[TD][/TD]
[TD="class: column_attribute nowrap"][/TD]
[TD]No[/TD]
[TD="class: nowrap"]Nessuno[/TD]
[TD="class: nowrap"]AUTO_INCREMENT[/TD]
[TD="class: edit center, align: center"][/TD]
[TD="class: drop center, align: center"][/TD]
[TD][/TD]
[/TR]
[TR="class: even"]
[TD="class: center, align: center"][/TD]
[TD="class: right, align: right"]2[/TD]
[TH="class: nowrap, align: left"]username[/TH]
[TD="class: nowrap"]varchar(255)[/TD]
[TD]latin1_swedish_ci[/TD]
[TD="class: column_attribute nowrap"][/TD]
[TD]No[/TD]
[TD="class: nowrap"]Nessuno[/TD]
[TD="class: nowrap"][/TD]
[TD="class: edit center, align: center"][/TD]
[TD="class: drop center, align: center"][/TD]
[TD][/TD]
[/TR]
[TR="class: odd"]
[TD="class: center, align: center"][/TD]
[TD="class: right, align: right"]3[/TD]
[TH="class: nowrap, align: left"]password[/TH]
[TD="class: nowrap"]varchar(255)[/TD]
[TD]latin1_swedish_ci[/TD]
[TD="class: column_attribute nowrap"][/TD]
[TD]No[/TD]
[TD="class: nowrap"]Nessuno[/TD]
[TD="class: nowrap"][/TD]
[TD="class: edit center, align: center"][/TD]
[TD="class: drop center, align: center"][/TD]
[TD][/TD]
[/TR]
[TR="class: even"]
[TD="class: center, align: center"][/TD]
[TD="class: right, align: right"]4[/TD]
[TH="class: nowrap, align: left"]email[/TH]
[TD="class: nowrap"]varchar(255)[/TD]
[TD]latin1_swedish_ci[/TD]
[TD="class: column_attribute nowrap"][/TD]
[TD]No[/TD]
[TD="class: nowrap"]Nessuno[/TD]
[TD="class: nowrap"][/TD]
[TD="class: edit center, align: center"][/TD]
[TD="class: drop center, align: center"][/TD]
[TD][/TD]
[/TR]
[TR="class: odd"]
[TD="class: center, align: center"][/TD]
[TD="class: right, align: right"]5[/TD]
[TH="class: nowrap, align: left"]ruolo[/TH]
[TD="class: nowrap"]varchar(255)[/TD]
[TD]latin1_swedish_ci[/TD]
[TD="class: column_attribute nowrap"][/TD]
[TD]No[/TD]
[TD="class: nowrap"]Nessuno[/TD]
[TD="class: nowrap"][/TD]
[TD="class: edit center, align: center"][/TD]
[TD="class: drop center, align: center"][/TD]
[TD][/TD]
[/TR]
[TR="class: even"]
[TD="class: center, align: center"][/TD]
[TD="class: right, align: right"]6[/TD]
[TH="class: nowrap, align: left"]active[/TH]
[TD="class: nowrap"]varchar(255)[/TD]
[TD]latin1_swedish_ci[/TD]
[TD="class: column_attribute nowrap"][/TD]
[TD]No[/TD]
[TD="class: nowrap"]Nessuno[/TD]
[TD="class: nowrap"][/TD]
[TD="class: edit center, align: center"][/TD]
[TD="class: drop center, align: center"][/TD]
[TD][/TD]
[/TR]
[TR="class: odd"]
[TD="class: center, align: center"][/TD]
[TD="class: right, align: right"]7[/TD]
[TH="class: nowrap, align: left"]resetToken[/TH]
[TD="class: nowrap"]varchar(255)[/TD]
[TD]latin1_swedish_ci[/TD]
[TD="class: column_attribute nowrap"][/TD]
[TD]Sì[/TD]
[TD="class: nowrap"]NULL[/TD]
[TD="class: nowrap"][/TD]
[TD="class: edit center, align: center"][/TD]
[TD="class: drop center, align: center"][/TD]
[TD][/TD]
[/TR]
[TR="class: even"]
[TD="class: center, align: center"][/TD]
[TD="class: right, align: right"]8[/TD]
[TH="class: nowrap, align: left"]resetComplete[/TH]
[TD="class: nowrap"]varchar(3)[/TD]
[TD]latin1_swedish_ci[/TD]
[TD="class: column_attribute nowrap"][/TD]
[TD]Sì[/TD]
[TD="class: nowrap"]No[/TD]
[/TR]
[/TABLE]