CodeIgniter Forums
Delete data by a form field - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: Delete data by a form field (/thread-69392.html)



Delete data by a form field - prezli - 11-13-2017

I want to delete email addresses from my database via a form field. If the email address in the field equals to the email address in the database, it (the row) gets deleted. I'm a bit confused how to do it.

View:
Code:
<form action="Controller/delete" method="post" id="form" name="form">
   <input id="email" name="email" type="email">
   <input name="submit" id="submit" type="submit" value="Unsubscribe">
</form>

<?php

if (isset($_GET['submit'])) {
 $email=$_POST['email'];

foreach($select->result() as $row){
 $email=$row->email;

 if($email==$row){
   delete($row);
 }
}
}
?>

Model:
Code:
function select() {
 $this->db->select("email");
 $this->db->from("users");
 $query=$this->db->get();
 return $query;
}

function delete($email) {
 $this->db->where("email", $email);
 $this->db->delete("users");
}

Controller:
Code:
public function index()
{
   $this->load->model('users');

   $data["select"]=$this->users->select();
   $this->load->view("view", $data);
}

public function delete(){
  $this->load->model('users');
  $this->users->delete($email);
  redirect(Controller/index);  
}



RE: Delete data by a form field - sujith - 11-14-2017

Code:
users model--
function no_of_users() {
$this->db->select("email");
$this->db->from("users");
return $this->db->get()->num_rows();
}

function select() {
$this->db->select("email");
$this->db->from("users");
$query=$this->db->get();
return $query;
}

function delete($email) {
$this->db->where("email", $email);
$this->db->delete("users");
return $this->db->affected_rows();
}



Controller--


Code:
public function index()
{
  $this->load->model('users');

  $data["select"]=$this->users->select();
  $this->load->view("view", $data);
}

public function delete(){

$email = $this->input->post('email',TRUE);
if($this->users->no_of_users($email)>0){
 $this->load->model('users');
 $this->users->delete($email);
$this->session->set_flashdata('msg','User-deleted');
}
else{
$this->session->set_flashdata('msg','No User with Such Email');
}
redirect(Controller/index);
}


View --

Code:
<?php
if($this->session->flashdata('msg')=='User-deleted'){
echo "Unsubscribed Succesfully";
}
?>

<form action="Controller/delete" method="post" id="form" name="form">
  <input id="email" name="email" type="email">
  <input name="submit" id="submit" type="submit" value="Unsubscribe">
</form>