Welcome Guest, Not a member yet? Register   Sign In
updating data in database using forms
#1

[eluser]Unknown[/eluser]
Hi,

I am student and new to CodeIgniter and even PHP.
I do have an assignment on updating the books in the database(phpmyadmin).

and not sure what I am doing wrong.
I am attaching the files with this email.
Can anyone help me with this?

Part Five [Create/ Read / Update / Delete]
Apply the concepts were learned in the previous parts of the assignment to complete the following:
1.) Add “Edit/Delete” links next the records rendered in “book_view.php”
a. The link should take the user to an “Edit/Delete” form
i. Make use of anchor_popup() method to open the form
ii. Make use of the book record’s primary key to retrieve correct record to edit
2.) Create an “Edit/Delete” book form that allows users to edit, update and delete book information
a. Make use of the CI validation library
b. * Review primary key/foreign key database concepts
3.) Create a “Book Category” form that allows users to add book categories to the application
4.) Create a “Book Category” form that allows users to update and delete book categories from the application
5.) Modify “add_book_form_view.php” so that it makes use of book categories retrieved from the “bookCategories” table
a. Add code to existing controllers, views, and model as needed

controller(dashboard.php)
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Dashboard extends CI_Controller{
        public function index(){
     $this->load->view("dashboard_view");
}
        public function get_add_books_form(){  
  $this->load->view("add_books_form_view");
}
public function edit_books_form_view(){  
  $this->load->view("get_edit_books_form");
}
public function save_books(){
  $this->form_validation->set_rules('author', 'Author', 'required');
        $this->form_validation->set_rules('bookTitle', 'Book Title', 'required');
        $this->form_validation->set_rules('description', 'Description', 'required');
  if($this->form_validation->run() == FALSE){
   $this->get_add_books_form();
  } else {
   $this->load->model("BooksModel");
   $this->BooksModel->save_books($_POST);
   $this->session->set_flashdata('message' , 'Book Saved');
   redirect("dashboard/get_add_books_form");
  
  }
}
public function get_books(){
  $this->load->model("BooksModel");
  $data['bookinformation'] = $this->BooksModel->get_books();
  $this->load->view("books_view", $data);
}
public function get_edit_books_form( $bookID="" ) {
  if (empty( $bookID )) {
  $bookID = $this->uri->segment(3, 0);
  }
  $this->load->model("BooksModel");
  $data['bookinformation'] = $this->BooksModel->get_book( $bookID );
  $this->load->view("edit_books_form_view", $data);
  }
public function update_books() {
    $this->load->model("BooksModel");
    $this->BooksModel->update_books( $_POST );
    $this->session->set_flashdata('message', 'Books data updated.');
    redirect("dashboard/get_edit_books_form/" . $_POST['bookID']);
}
}

Model(booksmodel.php)
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class BooksModel extends CI_Model {
public function save_books($formData){
$this->db->insert("bookinformation", $formData);}
public function get_books(){
return $this->db->get('bookinformation');}
public function get_book( $bookID ){
return $this->db->get('bookinformation', $bookID);}
}

view(books_view.php)
Code:
<?php
$popupAttributes = array(
'width' => '800',
'height' => '600');
foreach( $bookinformation->result() as $bookinformation ) {
echo "<p>" . $bookinformation->bookID . "</p>";
echo "<p>" . $bookinformation->bookTitle . "</p>";
echo "<p>" . $bookinformation->author . "</p>";
echo "<p>" .  $bookinformation->description . "</p>";
echo "<p>" .  $bookinformation->isbn . "</p>";
echo anchor_popup('dashboard/get_edit_form_view'.$bookinformation->bookID, 'Edit/Delete', $popupAttributes );}?&gt;

view(get_edit_books_form.php)
Code:
&lt;?php
echo $this->session->flashdata('message');
echo validation_errors();
$id = array('id' => 'BookForm');
echo form_open('dashboard/update_books', $id);
$bookinformation = $bookinformation->row();  
?&gt;
&lt;?php echo "EDIT/UPDATE" ?&gt;
<br />
<br />
&lt;input name="bookID" type="hidden" value="&lt;?php echo $bookinformation-&gt;bookID; ?&gt;" /&gt;
<label for="bookTitle">Book Title</label><br />
&lt;input name="bookTitle" id="bookTitle" type="text" value="&lt;?php echo $bookinformation-&gt;bookTitle; ?&gt;" /&gt;&lt;br />
   <label for="author">Author:  </label><br />
&lt;input name='author' id="author" value="&lt;?php echo $bookinformation-&gt;author; ?&gt;" /&gt;&lt;br />
<label for="ISBN">ISBN </label> <br />
&lt;input name='ISBN' id="ISBN" type='text' value="&lt;?php echo $bookinformation-&gt;isbn; ?&gt;" /&gt;&lt;br />
<label for="description">Description:<label><br />
&lt;textarea rows="10" cols="30" id="description" name="description" value="&lt;?php echo $bookinformation-&gt;description; ?&gt;"&gt;&lt;/textarea><br /><br />
<label for="categoryID">Categories: </label> <br />
<select name="categoryID" id="categoryID">
<option value="1">1. Arts & Photography</option>
<option value="2">2. Children's Books</option>
<option value="3">3. Computers & Technology</option>
<option value="4">4. Science & Math</option>
</select>
&lt;input id="submit" type="submit" value="Submit" /&gt;
&lt;/form&gt;
&lt;?php echo "<strong>Book ID</strong>: " . $bookinformation->bookID . "<br />"; ?&gt;
&lt;?php echo "<strong>Book Author</strong>: " . $bookinformation->author . "<br />"; ?&gt;
&lt;?php echo "<strong>Book Title</strong>: " . $bookinformation->bookTitle . "<br />"; ?&gt;
&lt;?php echo "<strong>Book ISBN</strong>: " . $bookinformation->isbn . "<br />"; ?&gt;
&lt;?php echo "<strong>Book Description</strong>: " . $bookinformation->description . "<br />"; ?&gt;
&lt;?php echo "<strong>Book Category</strong>: " . $bookinformation->categoryID . "<br />"; ?&gt;
?php// echo "DELETE" ?&gt;
  &lt;?php //echo anchor("dashboard/delete_books/$bookinformation->bookID", "Delete"); ?&gt;
#2

[eluser]InsiteFX[/eluser]
You will not be able to edit/delete using the anchor_popup like that!

./application/core/MY_Controller.php

Etend your controllers from this MY_Controller.
Code:
class MY_Controller extends CI_Controller {

public function __construct()
{
  parent::__construct();

  $this->load->model('booksmodel', 'books');

  $tmpl = array (
   'table_open'   => '<table border="0" cellpadding="4" cellspacing="0">',

   'thead_open'   => '<thead>',
   'thead_close'   => '</thead>',

   'heading_row_start'  => '<tr>',
   'heading_row_end'  => '</tr>',
   'heading_cell_start' => '<th>',
   'heading_cell_end'  => '</th>',

   'tbody_open'   => '<tbody>',
   'tbody_close'   => '</tbody>',

   'row_start'    => '<tr>',
   'row_end'    => '</tr>',
   'cell_start'   => '<td>',
   'cell_end'    => '</td>',

   'row_alt_start'   => '<tr class="alt">',
   'row_alt_end'   => '</tr>',
   'cell_alt_start'  => '<td>',
   'cell_alt_end'   => '</td>',

   'table_close'   => '</table>'
        );

  $this->table->set_template($tmpl);
    }

}


Create a manage methon/function something like this to setup yuour edit/delete form.
Code:
// -----------------------------------------------------------------------

/**
  * manage()
  *
  * Description:
  *
  * @access public
  * @return void
  */
public function manage()
{
  // Get an array of users from the database
  $data = $this->books->get_books();

  // Set headings for the table
  $this->table->set_heading('ID', 'Author', 'Title', 'ISBN', 'Description', 'Category', 'Actions');

  foreach ($data as $value => $key)
  {
   // Build action links
   $actions =  anchor("dashboard/edit/".$key['BookID']."/", "Edit");

   // Add the row to table
   $this->table->add_row($key['BookID'], $key['author'], $key['bookTitle'], $key['isbn'], $key['description'], $key['categoryID'], $actions);
  }

  // Load the view
  $this->load-view('your_edit_delete_view')
}


In your view to generate the table ad this:
View
Code:
&lt;?php echo $this->table->generate(); ?&gt;

See:

CodeIgniter Users Guide - HTML Table Class

In your controller add the following functions and point them to your model:
Code:
add
edit
update
delete




Theme © iAndrew 2016 - Forum software by © MyBB