non duplicare dati in stato "aggiornamento"

luigi777

Utente Attivo
14 Feb 2008
1.086
1
38
42
Massa, Italy
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:


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';
 
Salve, seconda versione con le api di pdo errorinfo. ma non so non mi stampa nulla. vi posto il codice se mi potete aiutarmi.
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"]);
    $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($email))
    {
            $message = "Inserire l'email";
    }

    else if(empty($password))
    {
        $message = "Inserire la password";
    }
    else {


        $already_exists = true;
        $stmt = $db->prepare("SELECT id FROM email WHERE email = :email");
        $stmt->execute(array(':email' => $email));
        if ($stmt->rowCount() > 0) {
            $already_exists = false;
        }
        else {
            try {

                        $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, 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);
                        $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']);
                        $stmt->execute();
            } catch (PDOException $e) {
                if ($e->errorInfo[1] == 1062) {
                    $already_exists = false;
                } else {
                    throw $e;
                }
            }
        }





try {
            $stmt->execute();
            $message = "Aggiornato";
        } catch (PDOException $e) {
           if ($e->errorInfo[1] == 1062) {
                    $message = "Il dato esiste gi&agrave;";
           } else {

           }
        }
    }

}
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 here</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>
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';
 
Non ti viene nessun errore?
Se il codice lo hai copiato un po' qua e là, ti consiglio di mettere degli echo dentro al codice php in modo da capire dove sta l'errore. Tipo prova così e poi dicci quali messaggi ti vengono fuori:
PHP:
<?php
session_start();
require 'inc.php';
require 'admin.php';

$db = getDB();

$message = '';


if(isset($_POST["submit"]))
{
echo "ok <br>";
$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";
}elseif(empty($surname)){
$message = "Inserire il cognome";
}elseif(empty($birthday)){
$message = "Inserire la data di nascita";
}elseif(empty($username)){
$message = "Inserire l'username";
}elseif(empty($email)){
$message = "Inserire l'email";
}elseif(empty($password)){
$message = "Inserire la password";
}
else {
echo "no errori <br>";
$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() ){
echo "query ok <br>";
$message = 'Aggiornato';
}else{
echo "errore nella query <br>";
$message = "Problema nella creazione del'account";
}

}

}
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 ($user['sex'] == "M") echo ' selected' ?>>Maschio</option>
     <option value="F" <?php if ($user['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';
P.S. la connessione sei sicuro che funzioni? Hai mai provato ad eseguire altre query?
 
Salve, non so se è giusto gestire gli errorInfo, di pdo . dimmi se va bene cosi. perché in questo modo funziona. Ma non sono sicuro se va bene gestire gli errorInfo di Pdo. grazie mille.
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"]);

    $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($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, picture=:picture, birthday=:birthday, age=:age, sex=:sex, password=:password, email=:email, 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);

                        $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);

                        $pwd = password_hash($password, PASSWORD_BCRYPT);

                        $stmt->bindParam(':password', $pwd);

                        $stmt->bindParam(':email', $email, PDO::PARAM_STR);

                        $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())

         {

             $err = $stmt->errorInfo();

             if (isset($err[1]))

             {

                 if ($err[1] == 1062)

                     $message =  'This email already exists.';

             }

         }

        }

}



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>



    <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>

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';
 
Non saprei mi dispiace... Prova a vedere online se trovi qualcosa, ma comunque se funziona io lascerei così
 
sta per i duplicate entry. ora cerco in inglese su google. grazie mille.
Ciao, scusa non sarebbe più logico effettuare una query con ID ed Email attuale utente e dal modello effettui l'update?

PHP:
$user=MyModel::instanceOfModel()->find($_SESSION['user_id']);

$fEmail=MyModel::instenceOfModel()->whereEmail($email);

if(null===$fEmail){
array_walk($_POST,function($v,$k) use (&$user){
    $user->$k=cleanInput($v);
});
$user->save();

}else{
//Email già presente
}
 

Discussioni simili