CodeIgniter プログラミングは初めてです。フォルダーから画像を保存および取得したいのですが、コードを実行すると、次のようなエラーが見つかりました。
最初のエラー:
Upload failed!
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: in
Filename: controllers/main.php
Line Number: 104
2 番目のエラー:
A Database Error Occurred
You must use the "set" method to update an entry.
Filename: application/models/main_model.php
Line Number: 80
私はこのコードを使用しています:
コントロールで:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Main extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->model('main_model');
$this->load->helper(array('form', 'url'));
}
public function product()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('productname','Product Code','trim|required');
$this->form_validation->set_rules('productcode','Product Code','trim|required');
$this->form_validation->set_rules('productprice','Product Price','trim|required');
$this->form_validation->set_rules('quantity','Quantity','trim|required');
$this->form_validation->set_rules('uploadimage','Upload Image','trim|required');
if($this->form_validation->run()==FALSE)
{
$this->index();
}else
{
if ($this->input->post('upload'))
{
$in=array();
$in['productname'] = $this->input->post('productnamename');
$in['productcode'] = $this->input->post('productcode');
$in['productprice']=$this->input->post('productprice');
$in['quantity']=$this->input->post('quantity');
$in['uploadimage']=$_FILES['image']['name'];
}
if($this->main_model->do_upload()) {
echo $this->upload->display_errors();
}else
{
$this->main_model->save_gallery($in);
header('location:product');
}
$data['images']=$this->main_model->get_images();
$this->load->view('query_view',$data);
}
}
モデル内:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Main_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function do_upload()
{
$config = array(
'allowed_types' => 'jpg|png|bmp',
'upload_path'=>'./uploads/', //make sure you have this folder
'max_size'=>2000);
$this->load->library('upload',$config);
if ($this->upload->do_upload()) {
echo "Upload success!";
} else {
echo "Upload failed!";
}
$image_data = $this->upload->data();
}
function get_images()
{
$query = $this->db->get('product');
return $query;
}
function save_gallery($in)
{
$save=$this->db->get("product");
if($save->num_rows())
{
$save=$this->db->insert('product',$in);
return $save;
}
}
ビューで:
<?php foreach ($images as $image):?>
<h1><?php echo $image['a_name'];?></h1>
<h1><?php echo $image['a_details'];?></h1>
<?php echo '<img src ="'. base_url().'images1/'.$image['a_photo'].'" >";
endforeach; ?>
FronPage ビュー:
<?php echo form_open("main/product"); ?>
<p>
<label for="product_name">Product Name:</label>
<input type="text" id="productname" name="productname" value="<?php echo set_value('product_name'); ?>" />
</p>
<p>
<label for="ProductCode">Product Code</label>
<input type="text" id="productcode" name="productcode" value="<?php echo set_value('productcode'); ?>" />
</p>
<p>
<label for="productprice">Product Price:</label>
<input type="text" id="productprice" name="productprice" value="<?php echo set_value('productprice'); ?>" />
</p>
<p>
<label for="Quantity">Quantity:</label>
<select name="quantity" id="quantity" value="<?php echo set_value('quantity'); ?>" /><option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</p>
<p>
<label for="Uploadimage">Upload Image:</label>
<input type="file" name="uploadimage" id="uploadimage" value="<?php echo set_value('uploadimage'); ?>" />
</p>
<p>
<input type="submit" class="greenButton" value="submit" />
</p>
<?php echo form_close(); ?>