Harusnya masuk ke if tapi malah masuk ke else di codeigniter

saya punya form type file

<input type="file" class="form-control" id="imagefile" name="image" value="okok">

lalu di controller saya cek

if($this->input->post('image') == "okok"){
//harusnya masuk kesini
}else{
//tapi pas dijalankan masuk kesini
}

saya pakai codeigniter. salahnya dimana yah kak ?

avatar tonisuwendi
@tonisuwendi

169 Kontribusi 36 Poin

Diperbarui 4 tahun yang lalu

1 Jawaban:

script viewnya

<pre> &lt;form role="form" action="&lt;?php echo base_url(); ?&gt;admin/article/edit/&lt;?php echo $article['idar']; ?&gt;" method="post" enctype="multipart/form-data"&gt; &lt;div class="box-body"&gt; &lt;?php echo $this-&gt;session-&gt;flashdata('error'); ?&gt; &lt;div class="form-group"&gt; &lt;label for="title"&gt;Title&lt;/label&gt; &lt;input type="text" class="form-control" id="title" placeholder="Title" name="title" value="&lt;?php echo $article['artitle']; ?&gt;"&gt; &lt;/div&gt; &lt;small class="form-text text-danger pl-1"&gt;&lt;?php echo form_error('title'); ?&gt;&lt;/small&gt; &lt;div class="form-group"&gt; &lt;label for="image"&gt;Image&lt;/label&gt;&lt;br&gt; &lt;img src="&lt;?php echo base_url(); ?&gt;assets/img/article/&lt;?php echo $article['imgar']; ?&gt;" style="width:100px"&gt; &lt;small&gt;Old picture&lt;/small&gt; &lt;br&gt;&lt;br&gt; &lt;input type="file" class="form-control" id="imagefile" onchange='changeEventHandler(event);' name="image" value="no"&gt; &lt;/div&gt; &lt;small class="form-text text-danger pl-1"&gt;&lt;?php echo form_error('image'); ?&gt;&lt;/small&gt; &lt;div class="form-group"&gt; &lt;label for="category"&gt;Category&lt;/label&gt; &lt;select class="form-control" name="category"&gt; &lt;option value="&lt;?php echo $article['category']; ?&gt;"&gt;&lt;?php echo $article['titlecat']; ?&gt;&lt;/option&gt; &lt;?php foreach($allCategory-&gt;result_array() as $data){ ?&gt; &lt;option value="&lt;?php echo $data['id']; ?&gt;"&gt;&lt;?php echo $data['title']; ?&gt;&lt;/option&gt; &lt;?php } ?&gt; &lt;/select&gt; &lt;/div&gt; &lt;div class="form-group"&gt; &lt;label for="desc"&gt;Description&lt;/label&gt; &lt;textarea name="description"&gt;&lt;?php echo set_value('description'); ?&gt;&lt;?php echo form_error('description'); ?&gt;&lt;?php echo $article['descar']; ?&gt;&lt;/textarea&gt; &lt;/div&gt; &lt;small class="form-text text-danger pl-1"&gt;&lt;?php echo form_error('category'); ?&gt;&lt;/small&gt; &lt;button type="submit" class="btn btn-primary"&gt;Edit Article&lt;/button&gt; &lt;/div&gt; &lt;/form&gt; </pre>

script controller

<pre> public function editarticle($id) { $query = $this-&gt;db-&gt;get_where('article', ['id' =&gt; $id])-&gt;row_array(); if($query['posted_by'] != $this-&gt;session-&gt;userdata('id')){ redirect(base_url() . 'admin/myarticles'); } $this-&gt;form_validation-&gt;set_rules('title', 'Title', 'required|min_length[10]'); $this-&gt;form_validation-&gt;set_rules('description', 'Description', 'required|min_length[20]'); if($this-&gt;form_validation-&gt;run() == false){ $data['title'] = 'Edit Article - Admin Panel'; $data['css'] = ''; $data['dataUser'] = $this-&gt;User_model-&gt;getDataUser(); $data['allCategory'] = $this-&gt;Categories_model-&gt;getAllCat(); $data['article'] = $this-&gt;Article_model-&gt;getArticleById($id); $this-&gt;load-&gt;view('admin/edit_article', $data); }else{ if($this-&gt;input-&gt;post('image') == "okok"){ $data = array(); $upload = $this-&gt;Article_model-&gt;uploadgambar(); if($upload['result'] == "success"){ $this-&gt;Article_model-&gt;updateArticle($upload,$id); $this-&gt;session-&gt;set_flashdata('upload', "&lt;script&gt; swal({ text: 'Article updated successfully!', icon: 'success' }); &lt;/script&gt;"); redirect(base_url() . 'admin/myarticles'); }else{ $this-&gt;session-&gt;set_flashdata('upload', "&lt;script&gt; swal({ text: 'Article not uploaded. Make sure the image you upload is smaller than 2mb and must be in jpg, jpeg, or png format.', icon: 'error' }); &lt;/script&gt;"); redirect(base_url() . 'admin/article/edit/' . $id); } }else{ $upload = ""; $this-&gt;Article_model-&gt;updateArticle($upload,$id); $this-&gt;session-&gt;set_flashdata('upload', "&lt;script&gt; swal({ text: 'Article updated successfully!', icon: 'success' }); &lt;/script&gt;"); redirect(base_url() . 'admin/myarticles'); } } } </pre>

modelnya

<pre> public function uploadgambar() { $config['upload_path'] = './assets/img/article/'; $config['allowed_types'] = 'jpg|png|jpeg'; $config['max_size'] = '2048'; $config['file_name'] = round(microtime(true)*1000);

	$this-&amp;gt;load-&amp;gt;library(&#039;upload&#039;, $config);
	if($this-&amp;gt;upload-&amp;gt;do_upload(&#039;image&#039;)){
		$return = array(&#039;result&#039; =&amp;gt; &#039;success&#039;, &#039;file&#039; =&amp;gt; $this-&amp;gt;upload-&amp;gt;data(), &#039;error&#039; =&amp;gt; &#039;&#039;);
		return $return;
	}else{
		$return = array(&#039;result&#039; =&amp;gt; &#039;failed&#039;, &#039;file&#039; =&amp;gt; &#039;&#039;, &#039;error&#039; =&amp;gt; $this-&amp;gt;upload-&amp;gt;display_errors());
		return $return;
	}
}

</pre>

<pre> public function updateArticle($upload,$id) { $title = htmlspecialchars($this-&gt;input-&gt;post('title')); if($this-&gt;input-&gt;post('image') == "okok"){ $this-&gt;db-&gt;set('image', $upload['file']['file_name']); } $this-&gt;db-&gt;set('title', $title); $this-&gt;db-&gt;set('description', $this-&gt;input-&gt;post('description')); $this-&gt;db-&gt;set('category', htmlspecialchars($this-&gt;input-&gt;post('category'))); $this-&gt;db-&gt;where('id', $id); $this-&gt;db-&gt;update('article'); } </pre>

avatar tonisuwendi
@tonisuwendi

169 Kontribusi 36 Poin

Dipost 5 tahun yang lalu

Login untuk ikut Jawaban