• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Delete data by a form field

#1
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);  
}
Reply

#2
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>
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.