So, my image doesn't want to insert. I have no clue why, I'm trying to combine it with other inserts and this is why it doesn't want to work. I have the exact same code, but seperately working fine. No clue why it doesn't find the name of it. Any idea's?
Here's the error:
Notice: Undefined index: name in /Applications/MAMP/htdocs/MAJOREIND/dao/UsersDAO.php on line 39 Notice: Undefined index: extension in /Applications/MAMP/htdocs/MAJOREIND/dao/UsersDAO.php on line 40 Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null' in /Applications/MAMP/htdocs/MAJOREIND/dao/UsersDAO.php:41 Stack trace: #0 /Applications/MAMP/htdocs/MAJOREIND/dao/UsersDAO.php(41): PDOStatement->execute() #1 /Applications/MAMP/htdocs/MAJOREIND/controller/UsersController.php(78): UsersDAO->insert(Array) #2 [internal function]: UsersController->boeken() #3 /Applications/MAMP/htdocs/MAJOREIND/controller/Controller.php(9): call_user_func(Array) #4 /Applications/MAMP/htdocs/MAJOREIND/index.php(65): Controller->filter() #5 {main} thrown in /Applications/MAMP/htdocs/MAJOREIND/dao/UsersDAO.php on line 41
Here's the class code:
public function boeken() {
$errors = array();
if(empty($_SESSION['user'])){
if(!empty($_POST)) {
if(!empty($_FILES['image'])){
if(!empty($_FILES['image']['error'])){
$errors['image']='Something went wrong';
}
if(empty($errors['image'])){
$size = getimagesize($_FILES['image']['tmp_name']);
if(empty($size)){
$errors['image'] = 'Uploaded image is not an image';
}
}
if(empty($errors['image'])){
if($size[1] != $size[0]){
$errors['image'] = 'Image must be a square';
}
}
if(empty($errors['image'])){
$sourceFile = $_FILES['image']['tmp_name'];
$destFile = WWW_ROOT . 'uploads' . DS . $_FILES['image']['name'];
$lastDotPos = strrpos($_FILES['image']['name'], '.');
$name = "lol";
$name = substr(($_FILES['image']['name']), 0, $lastDotPos);
$extension = substr($_FILES['image']['name'], $lastDotPos + 1);
move_uploaded_file($sourceFile, $destFile);
$image = new Eventviva\ImageResize($destFile);
$image->resizeToHeight(100);
$image->save(WWW_ROOT . 'uploads' . DS . $name . '_th.' . $extension);
move_uploaded_file($sourceFile, $destFile);
$image = new Eventviva\ImageResize($destFile);
$image->resizeToHeight(400);
$image->save(WWW_ROOT . 'uploads' . DS . $name . '.' . $extension);
if(empty($_FILES['image']['name'])){
$errors['name'] = 'Kies een profielfoto';
}
if(empty($errors)) {
//$hasher = new \Phpass\Hash;
$passwordHash = $this->hasher->hashPassword($_POST['password']);
$user = array(
"naam"=>$_POST["naam"],
"email"=>$_POST["email"],
"password"=>$passwordHash
);
$insertedUser = $this->userDAO->insert($user);
$userBooking = array(
"email"=>$_POST['email'],
"tours"=>$_POST["tours"],
"amount"=>$_POST["amount"],
"message"=>$_POST["message"],
'name'=>$name,
'extension' => $extension
);
$insertedBooking = $this->userDAO->insertBooking($userBooking);
if(!empty($insertedUser)) {
$_SESSION['info'] = 'Registration successful';
$this->redirect('index.php');
} else {
$_SESSION['error'] = 'Registration failed';
}
} else {
$_SESSION['error'] = 'Please complete the form';
}
//$data = array('profilepic' => $profilepic);
//$inserted = $this->userDAO->insert($data);
//if(!empty($inserted)){
// $_SESSION['info'] = 'The image was inserted';
// $this->redirect('index.php?page=add');
//}
}
}
if(empty($_POST['naam'])) {
$errors['naam'] = 'Vul een naam in';
}
if(empty($_POST['email'])) {
$errors['email'] = 'Vul een email in';
} else {
//check unique email
$existing = $this->userDAO->selectByEmail($_POST['email']);
if(!empty($existing)) {
$errors['email'] = 'Email bestaat reeds';
}
}
if(empty($_POST['password'])) {
$errors['password'] = 'Vul een paswoord in';
}
if($_POST['confirm_password'] != $_POST['password']) {
$errors['confirm_password'] = 'Ongelijke paswoorden';
}
if(empty($_POST['tours'])) {
$errors['tours'] = 'Kies een tour in';
}
if(empty($_POST['amount'])) {
$errors['amount'] = 'Kies aantal personen in';
}
}
}else{
if(!empty($_POST)) {
if(empty($_POST['tours'])) {
$errors['tours'] = 'Kies een rondleiding';
}
if(empty($_POST['amount'])) {
$errors['amount'] = 'Kies het aantal personen';
}
if(empty($errors)) {
$user = array(
"user_id"=>$_SESSION['user']['id'],
"email"=>$_SESSION['user']['email'],
"tours"=>$_POST["tours"],
"amount"=>$_POST["amount"],
"message"=>$_POST["message"],
);
$insertedBooking = $this->userDAO->insertBooking($user);
if(empty($insertedBooking)) {
$_SESSION['info'] = 'Booking successful';
$this->redirect('index.php');
} else {
$_SESSION['error'] = 'Booking failed';
}
} else {
$_SESSION['error'] = 'Please complete the form';
}
}
}
$this->set('errors', $errors);
}
This is my html code for it:
<label for="addimg">
<img src="images/photo.png"/>
</label>
<input type="file" name="image" id="addimg" class="form-control" value="<?php if(!empty($_POST['image'])) echo $_POST['image'];?>" />
<span class="error-message"<?php if(empty($errors['image'])) echo 'style="display: none;"';?>><?php
if(empty($errors['image'])) echo 'Please select an image';
else echo $errors['image'];
?></span>
And this is the query function:
public function insert($data) {
$errors = $this->getValidationErrors($data);
if(empty($errors)) {
$sql = "INSERT INTO `users` (`naam`, `email`, `password`, `name`, `extension`) VALUES (:naam, :email, :password, :name, :extension)";
$stmt = $this->pdo->prepare($sql);
$stmt->bindValue(':naam', $data['naam']);
$stmt->bindValue(':email', $data['email']);
$stmt->bindValue(':password', $data['password']);
$stmt->bindValue(':name', $data['name']);
$stmt->bindValue(':extension', $data['extension']);
if($stmt->execute()) {
$insertedId = $this->pdo->lastInsertId();
return $this->selectById($insertedId);
}
}
return false;
}