首先对于带有代码的长篇文章感到抱歉,但我觉得我需要直接解释自己。问题是,我正在尝试将一些数据插入数据库,并在RESTAURANT_IMAGE列中插入文件名。一切都很好地插入,但图像没有上传,文件名也没有插入数据库。
编辑:编辑回调函数以正确返回True或False,如果回调返回true,现在我在main函数中执行插入操作。 现在我能够成功上传图像,但仍然没有将文件名和扩展名放到我的数据库中。
function restaurant_add()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$config = array(
array(
'field' => 'RestaurantName',
'label' => 'Restaurant Name',
'rules' => 'required|callback_restaurant_check'
),
array(
'field' => 'RestaurantAddress',
'label' => 'Address',
'rules' => 'required'
),
array(
'field' => 'RestaurantReservations',
'label' => 'Reservations',
'rules' => 'max_length[1]|integer'
),
array(
'field' => 'RestaurantWifi',
'label' => 'RestaurantWifi',
'rules' => 'max_length[1]|integer'
),
array(
'field' => 'RestaurantDelivery',
'label' => 'Restaurant Delivery',
'rules' => 'max_length[1]|integer'
),
array(
'field' => 'RestaurantMultibanco',
'label' => 'RestaurantMultibanco',
'rules' => 'max_length[1]|integer'
),
array(
'field' => 'RestaurantImage',
'label' => 'RestaurantImage',
'rules' => ''
)
);
$this->form_validation->set_rules($config);
if ($this->form_validation->run() == FALSE)
{
$this->load->view('sample_navbar_view');
$this->load->view('restaurant_add');
return FALSE;
}
else
{
$file_name = $this->do_upload();
$data = array(
'RESTAURANT_NAME' => $this->input->post('RestaurantName'),
'RESTAURANT_ADDRESS' => $this->input-> post('RestaurantAddress'),
'RESTAURANT_RESERVATIONS'=> $this->input-> post('RestaurantReservations'),
'RESTAURANT_WIFI'=> $this->input-> post('RestaurantWifi'),
'RESTAURANT_DELIVERY'=> $this->input-> post('RestaurantDelivery'),
'RESTAURANT_MULTIBANCO'=> $this->input-> post('RestaurantMultibanco'),
'RESTAURANT_OUTDOOR_SEATING'=> $this->input-> post('RestaurantOutdoorSeating'),
'RESTAURANT_IMAGE'=> $file_name
);
$this->load->model('restaurant_model');
$this->restaurant_model->restaurant_add($data);
return TRUE;
}
}
回调函数检查餐馆名称是否存在,如果存在,则不会注册,如果它没有将数据发送到模型中。
function restaurant_check($restaurantname){
$this->load->model('restaurant_model');
$result = $this->restaurant_model->check_restaurant_name($restaurantname);
if($result > 0) {
return FALSE;
}
else {
return TRUE;
}
}
我使用了Codeigniter Documentation中的上传文件。
查看代码:
<div class="container">
<div class="row">
<div class="col-md-4 col-md-offset-4">
<?php echo form_open_multipart('restaurant/restaurant_add');?>
<form name="restaurant_add" method="post" action="<?php echo site_url().'Restaurant/restaurant_add'; ?>">
<h2>Register your Restaurant</h2>
<input class="form-control" type="text" class="user" name="RestaurantName" placeholder="Restaurant Name">
<input class="form-control"type="text" class="pass" name="RestaurantAddress" placeholder="Restaurant Address">
<input class="form-control"type="number" class="numero" name="RestaurantReservations" placeholder="Restaurant Rerservations">
<input class="form-control"type="number" class="numero" name="RestaurantWifi" placeholder="Wi-Fi">
<input class="form-control"type="number" class="numero" name="RestaurantDelivery" placeholder="Home Deliveries">
<input class="form-control"type="number" class="numero" name="RestaurantMultibanco" placeholder="Have Multibanco?">
<input class="form-control"type="number" class="numero" name="RestaurantOutdoorSeating" placeholder="Esplanada">
<input class="form-control"type="file" class="image" name="RestaurantImage" placeholder="Upload a Picture">
<div class="container">
<?php echo validation_errors(); ?>
</div>
<button class="btn btn-danger"type="submit">Registar</button>
</form>
</div>
</div>
编辑:型号代码:
function restaurant_add($info)
{
$query =
"INSERT INTO RESTAURANTS
(RESTAURANT_NAME,RESTAURANT_ADDRESS,RESTAURANT_RESERVATIONS,
RESTAURANT_WIFI,RESTAURANT_DELIVERY,RESTAURANT_MULTIBANCO,
RESTAURANT_OUTDOOR_SEATING,RESTAURANT_IMAGE
)
VALUES
(
'".$info['RESTAURANT_NAME']."',
'".$info['RESTAURANT_ADDRESS']."',
'".$info['RESTAURANT_RESERVATIONS']."',
'".$info['RESTAURANT_WIFI']."',
'".$info['RESTAURANT_DELIVERY']."',
'".$info['RESTAURANT_MULTIBANCO']."',
'".$info['RESTAURANT_OUTDOOR_SEATING']."',
'".$info['RESTAURANT_IMAGE']."'
)
";
$result = $this->db->query($query);
return TRUE;
}
function check_restaurant_name($restaurantname) {
$this->db->trans_begin();
$query = "SELECT RESTAURANT_NAME FROM RESTAURANTS WHERE RESTAURANT_NAME = '".$restaurantname."'";
$result = $this->db->query($query);
$rows = $result->num_rows();
if ($this->db->trans_status() === FALSE)
{
$this->db->trans_rollback();
}
else
{
$this->db->trans_commit();
}
return $rows;
}
do_upload()函数:
public function do_upload()
{
$config['upload_path'] = './assets/images/restaurantes';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 2000;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('success_view', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
$upload_data = $this->upload->data();
//Returns array of containing all of the data related to the file you uploaded.
$file_name = $this->upload->file_name;
return $file_name;
}