我在oop php中构建用于学习的应用程序,并且试图通过透视表上传与属性表关联的照片。我无法在将这些照片插入数据库的模型中编写函数。任何帮助是极大的赞赏。我已成功将照片上传到照片表以及应用程序目录中的文件夹中,但未在数据透视表中写入ID。任何帮助表示赞赏。
这是我的代码:
AdModel.php
public function createAd($data, $pht)
{
$photo_id = $this->photoInfo($data['photo']);
$this->db->query("INSERT INTO properties (title, description, type_of_property, use_of_the_property, quadrature, location, price, sales_clerk_info, booked, type_of_market, type_of_payment, status) VALUES (:title, :description, :type_of_property, :use_of_the_property, :quadrature, :location, :price, :sales_clerk_info, :booked, :type_of_market, :type_of_payment, :status) ");
$this->db->bind(':title', $data['title']);
$this->db->bind(':description', $data['description']);
$this->db->bind(':type_of_property', $data['type_of_property']);
$this->db->bind(':use_of_the_property', $data['use_of_the_property']);
$this->db->bind(':quadrature', $data['quadrature']);
$this->db->bind(':location', $data['location']);
$this->db->bind(':price', $data['price']);
$this->db->bind(':sales_clerk_info', $data['sales_clerk_info']);
$this->db->bind(':booked', $data['booked']);
$this->db->bind(':type_of_market', $data['type_of_market']);
$this->db->bind(':type_of_payment', $data['type_of_payment']);
$this->db->bind(':status', '1');
$this->db->execute();
$lastid = $this->db->lastid();
$this->db->query('INSERT INTO photos (name, extension) VALUES (:name, :extension)');
$this->db->bind(':name', $pht);
$this->db->bind(':extension', $data['extension]);
$this->db->execute();
$this->db->query('INSERT INTO property_photo (property_id, photo_id) VALUES (:property_id, photo_id)');
$this->db->bind(':property_id', $lastid);
$this->db->bind('photo_id', $photo_id[0]->id);
$this->db->execute();
return true;
}
public function photoInfo($photoname)
{
$this->db->query('SELECT id FROM photos WHERE name = :name');
$this->db->bind(':name', $photoname);
$data = $this->db->resultSet();
return $data;
}
AdsController.php
public function createAction()
{
$userinfo = $this->Auth->role($_SESSION['user_id']);
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
$data = [
'title' => trim($_POST['title']),
'description' => trim($_POST['description']),
'type_of_property' => trim($_POST['type_of_property']),
'use_of_the_property' => trim($_POST['use_of_the_property']),
'quadrature' => trim($_POST['quadrature']),
'location' => trim($_POST['location']),
'price' => trim($_POST['price']),
'sales_clerk_info' => trim($_POST['sales_clerk_info']),
'booked' => trim($_POST['booked']),
'type_of_market' => trim($_POST['type_of_market']),
'type_of_payment' => trim($_POST['type_of_payment']),
'title_err' => '',
'description_err' => '',
'type_of_property_err' => '',
'use_of_the_property_err' => '',
'quadrature_err' => '',
'location_err' => '',
'price_err' => '',
'sales_clerk_info_err' => '',
'booked_err' => '',
'type_of_market_err' => '',
'type_of_payment_err' => ''
];
if (empty($data['title'])) {
$data['title_err'] = 'Please enter your title!!!';
}
if (empty($data['description'])) {
$data['description_err'] = 'Please enter your description!!!';
}
if (empty($data['type_of_property'])) {
$data['type_of_property_err'] = 'Please select your type!!!';
}
if (empty($data['use_of_the_property'])) {
$data['use_of_the_property_err'] = 'Please enter use of the property!!!';
}
if (empty($data['quadrature'])) {
$data['quadrature_err'] = 'Please enter your quadrature!!!';
}
if (empty($data['location'])) {
$data['location_err'] = 'Please enter your location!!!';
}
if (empty($data['price'])) {
$data['price_err'] = 'Please enter your price!!!';
}
if (empty($data['sales_clerk_info'])) {
$data['sales_clerk_info_err'] = 'Please enter your info!!!';
}
if (empty($data['booked'])) {
$data['booked_err'] = 'Please select!!!';
}
if (empty($data['type_of_market'])) {
$data['type_of_market_err'] = 'Please select your type of market!!!';
}
if (empty($data['type_of_payment'])) {
$data['type_of_payment_err'] = 'Please select your type of payment!!!';
}
$photo_validate = $this->AdModel->photoValidate($_FILES['photo']);
if (empty($data['title_err']) && empty($data['description_err']) && empty($data['type_of_property_err']) && empty($data['use_of_the_property_err']) && empty($data['quadrature_err']) && empty($data['location_err']) && empty($data['price_err']) && empty($data['sales_clerk_info_err']) && empty($data['booked_err']) && empty($data['type_of_market_err']) && empty($data['type_of_payment_err']) && $photo_validate[0] === true) {
move_uploaded_file($photo_validate[1], "C:/xampp/htdocs/testApp/public/photos/".$photo_validate[2]);
if ($this->AdModel->createAd($data, $photo_validate[2])) {
redirect('ads/index');
} else {
if ($photo_validate === false) {
$photo_validate='Please select image';
} else {
if ($photo_validate[0] === true) {
$photo_validate='';
}
}
$data=[
'photo_validate'=>$photo_validate
];
die('Something went wrong!');
}
} else {
$this->view->render('ads/create', $data, $userinfo);
}
} else {
$data = [
'photo_validate'=>'',
'title' => '',
'description' => '',
'type_of_property' => '',
'use_of_the_property' => '',
'quadrature' => '',
'location' => '',
'price' => '',
'sales_clerk_info' => '',
'booked' => '',
'type_of_market_id' => '',
'type_of_payment' => '',
'title_err' => '',
'description_err' => '',
'type_of_property_err' => '',
'use_of_the_property_err' => '',
'quadrature_err' => '',
'location_err' => '',
'price_err' => '',
'sales_clerk_info_err' => '',
'booked_err' => '',
'type_of_market_err' => '',
'type_of_payment_err' => ''
];
$this->view->render('ads/create', $data, $userinfo);
}
}
create.php
<form action="/testApp/ads/create" method="POST" enctype="multipart/form-data"
<div class="form-group row">
<div class="col-sm-12">
<h5>Upload property image</h6>
<input type="file" name="photo" class="form-control form-control-lg">
</div>
</div>
<div class="form-group">
<button type="submit" name="submit" class="form-control btn btn-primary">Submit</button>
</div>
</form>