Salve, ho questo codice scritto dai vari tutorial che leggo.. Vorrei sapere per far che l'utente quando modifica l'email non deve andare ad duplicare la email di un altro utente .. cosa devo fare?, grazie mille.
Grazie mille.
codice:
Grazie mille.
codice:
PHP:
<?php
session_start();
require 'inc.php';
require 'admin.php';
$db = getDB();
$message = '';
if(isset($_POST["submit"]))
{
$name = cleanInput($_POST["name"]);
$surname = cleanInput($_POST["surname"]);
$birthday = cleanInput($_POST["birthday"]);
$sex = cleanInput($_POST["sex"]);
$username = cleanInput($_POST["username"]);
$password = cleanInput($_POST["password"]);
$email = cleanInput($_POST["email"]);
if(empty($name))
{
$message = "Inserire il nome";
}else
if(empty($surname))
{
$message = "Inserire il cognome";
}else
if(empty($birthday))
{
$message = "Inserire la data di nascita";
}
else if(empty($username))
{
$message = "Inserire l'username";
}else if(empty($email))
{
$message = "Inserire l'email";
}
else if(empty($password))
{
$message = "Inserire la password";
}
else {
$age = date_diff(date_create($birthday), date_create('now'))->y;
$sql = "UPDATE users SET name=:name, surname=:surname, description=:description, slug=:slug, picture=:picture, birthday=:birthday, age=:age, sex=:sex, username=:username, password=:password, email=:email, isLevel=:isLevel, date_reg=:date_reg WHERE id=:id";
$stmt = $db->prepare($sql);
$stmt->bindParam(':name', $name, PDO::PARAM_STR);
$stmt->bindParam(':surname', $surname, PDO::PARAM_STR);
$description = '';
$stmt->bindParam(':description', $description, PDO::PARAM_STR);
$stmt->bindParam(':slug', $username, PDO::PARAM_STR);
$pic = '';
$stmt->bindParam(':picture', $pic, PDO::PARAM_STR);
$stmt->bindParam(':birthday', $birthday, PDO::PARAM_STR);
$stmt->bindParam(':age', $age, PDO::PARAM_INT);
$stmt->bindParam(':sex', $sex, PDO::PARAM_STR);
$stmt->bindParam(':username', $username, PDO::PARAM_STR);
$pwd = password_hash($password, PASSWORD_BCRYPT);
$stmt->bindParam(':password', $pwd);
$stmt->bindParam(':email', $email, PDO::PARAM_STR);
$isLevel = 1;
$stmt->bindParam(':isLevel', $isLevel, PDO::PARAM_INT);
$date = date('Y-m-d H:i:s');
$stmt->bindParam(':date_reg', $date, PDO::PARAM_STR);
$stmt->bindValue(':id', $_SESSION['user_id']);
if( $stmt->execute() ):
$message = 'Aggiornato';
else:
$message = "Problema nella creazione del'account";
endif;
}
}
require 'theme/header-admin.php';
$records = $db->prepare('SELECT * FROM users WHERE id = :id');
$records->bindValue(':id', $_SESSION['user_id']);
$records->execute();
$results = $records->fetch(PDO::FETCH_ASSOC);
$user = NULL;
if( count($results) > 0){
$user = $results;
}
?>
<?php if(!empty($message)): ?>
<p><?= $message ?></p>
<?php endif; ?>
<h1>Aggiorna</h1>
<span>or <a href="login.php">login</a></span>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="POST">
Nome: <input type="text" name="name" value="<?php echo $user['name']; ?>">
Cognome: <input type="text" name="surname" value="<?php echo $user['surname']; ?>">
Birthday: <input type="date" name="birthday" value="<?php echo $user['birthday']; ?>">
Sex:
<?php
$sex = $user["sex"];
?>
<p><select name="sex" class="form-control">
<option value="M" <?php if ($sex == "M") echo ' selected' ?>>Maschio</option>
<option value="F" <?php if ($sex == "F") echo ' selected' ?>>Femmina</option>
</select>
</p>
Username:
<input type="username" name="username" value="<?php echo $user['username']; ?>">
Password:
<input type="password" name="password">
Email:
<input type="email" name="email" value="<?php echo $user['email']; ?>">
<input type="submit" name="submit">
</form>
<?php
require 'theme/footer-admin.php';