PDA

Pogčedajte punu verziju : PDO pitanje


McChoban
31. 08. 2007., 09:14
Ok, ovo je metod iz objekta, ocigledno. Ne obazirite se na global $db, ionako je u pitanju test ;)

Naime, update radi savrseno, a insert nece ni da bekne.
Hvatao sam mu i exception (u ovom kodu nema exc. handlinga, ali nije bitno), no nikakav exception nije uhvacen. Samo se nista ne desava pri upisu u bazu... Any ideas???
Hm da, objekat je imao napravljene parametre, nema ID, i kod je prosao froz uslov tamo kuda treba da prodje, samo se nista nije desilo.
Probao sam i sa samo 2 parametra cisto da vidim da ga ne buni neki podatak, ali ne...


public function save() {
global $db;
if ($this->id != null) {
$sql = "update users set username = :username , password = :password , email = :email , firstName = :firstName , lastName = :lastName , address = :address , city = :city , postCode = :postCode , country = :country , phone = :phone , birthDate = :birthDate , startYear = :startYear , numDogs = :numDogs , kennelName = :kennelName , isValidated = :isValidated , timeCreated = :timeCreated , timeModified = :timeModified where id = :id";
$stmt = $db->prepare($sql);
$stmt->bindParam(':id', $this->id);
$stmt->bindParam(':username', $this->username);
$stmt->bindParam(':password', $this->password);
$stmt->bindParam(':email', $this->email);
$stmt->bindParam(':firstName', $this->firstName);
$stmt->bindParam(':lastName', $this->lastName);
$stmt->bindParam(':address', $this->address);
$stmt->bindParam(':city', $this->city);
$stmt->bindParam(':postCode', $this->postCode);
$stmt->bindParam(':country', $this->country);
$stmt->bindParam(':phone', $this->phone);
$stmt->bindParam(':birthDate', $this->birthDate);
$stmt->bindParam(':startYear', $this->startYear);
$stmt->bindParam(':numDogs', $this->numDogs);
$stmt->bindParam(':kennelName', $this->kennelName);
$stmt->bindParam(':isValidated', $this->isValidated);
$stmt->bindParam(':timeCreated', $this->timeCreated);
$stmt->bindParam(':timeModified', $this->timeModified);
$stmt->execute();
} else {
$sql = "insert into users (username, password, email, firstName, lastName, address, city, postCode, country, phone, birthDate, startYear, numDogs, kennelName, isValidated, timeCreated, timeModified) values (:username, :password, :email, :firstName, :lastName, :address, :city, :postCode, :country, :phone, :birthDate, :startYear, :numDogs, :kennelName, :isValidated, :timeCreated, :timeModified)";
$stmt = $db->prepare($sql);
$stmt->bindParam(':username', $this->username);
$stmt->bindParam(':password', $this->password);
$stmt->bindParam(':email', $this->email);
$stmt->bindParam(':firstName', $this->firstName);
$stmt->bindParam(':lastName', $this->lastName);
$stmt->bindParam(':address', $this->address);
$stmt->bindParam(':city', $this->city);
$stmt->bindParam(':postCode', $this->postCode);
$stmt->bindParam(':country', $this->country);
$stmt->bindParam(':phone', $this->phone);
$stmt->bindParam(':birthDate', $this->birthDate);
$stmt->bindParam(':startYear', $this->startYear);
$stmt->bindParam(':numDogs', $this->numDogs);
$stmt->bindParam(':kennelName', $this->kennelName);
$stmt->bindParam(':isValidated', $this->isValidated);
$stmt->bindParam(':timeCreated', $this->timeCreated);
$stmt->bindParam(':timeModified', $this->timeModified);
$stmt->execute();
$this->setId($db->lastInsertId());
}
}

McChoban
31. 08. 2007., 09:50
evo i sam sebi da odgovorim - ovo ce da radi:


$stmt->bindParam(':username', $username);
$stmt->bindParam(':password', $password);
$username = $this->username;
$password = $this->password;
... itd




Dal' iko ima predstavu sto ovo ne radi na prvi nacin, i zasto kod update radi a kod inserta mora ovaj drugi ...??? :1092: :1092: :1092:

McChoban
31. 08. 2007., 10:06
Ok , sad me sramota... neki podaci su bili null, a u bazi je not null; Zaboravio sam da ih inicijalizujem... Jeb'o me moj kod generator kad sam zaboravan.
Ako, sad je bolji nego sto je bio ;)
Obrisite ovu temu... Da je moguce brisati svoje postove, ja bih to vec uradio :)

Ilija Studen
31. 08. 2007., 10:40
Ovo je za nauk novim članovima :p

Just kidding ;)