简体   繁体   English

带PHP CodeIgniter的MySQL表编辑

[英]MySQL Table Editing w/ PHP CodeIgniter

I'm unsure how to get my editfields.php file to load when an administrator selects the Edit button for whatever row in my SQL table (adminlogin.php). 当管理员为我的SQL表(adminlogin.php)中的任何行选择“编辑”按钮时,我不确定如何加载editfields.php文件。

I feel my editfields.php file has the right stuff, but I'm stuck when it comes to getting the data and having that data display in my view as opposed to what I have printing according to my edituser function. 我感觉自己的editfields.php文件具有正确的内容,但是在获取数据并在我的视图中显示该数据而不是根据我的edituser函数进行打印时,我陷入了困境。

** PLEASE ignore the approve function for now, as well as the if conditionak in adminlogin.php, both items for separate discussion ** **请暂时忽略批准功能,以及adminlogin.php中的if conditionak,这两个项目都将单独讨论**

Below are the following files: 以下是以下文件:

Controller (Myform.php) 控制器(Myform.php)

<?php

class Myform extends CI_Controller{

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

  public function index(){
    $this->load->helper(array('form', 'url'));
    $this->load->library('form_validation');

    $this->form_validation->set_rules('user[first_name]', 'First Name', 'required'); 
    $this->form_validation->set_rules('user[last_name]', 'Last Name', 'required'); 
    $this->form_validation->set_rules('user[email]', 'Email Address', 'required|trim'); 

          if ($this->form_validation->run() == FALSE){
            $this->load->view('myform');
          }
              else{
                $user = $this->input->post('user');
                $company = $this->input->post('company'); 
                echo '<pre>';
                print_r($user);
                print_r($company); 
                $this->db->insert("User_Inputs", $user);        
                $this->db->insert("companies", $company);       
                redirect("/myform/successmessage");
              }
  }

    public function successmessage(){
        $this->load->view('formsuccess');
    }          

  public function getDatabase(){
    $data['users'] = $this->db->get("User_Inputs")->result();
    $this->load->view('adminlogin', $data);

  }

public function approve(){
    $id = $this->input->get("user_id"); 
    $this->db->where("id", $id); 
  $data['user'] = $this->db->get("User_Inputs")->row();
    echo "<pre>"; 
  print_r($this->db->last_query()); 
    print_r($data);
    echo "<h1>". $data['user']->email."</h1>";  

    die();
}

public function edituser(){
    $data['user'] = $this->db->get("User_Inputs")->row();
  echo "<pre>"; 
  print_r($this->db->last_query()); 
  print_r($data);
  echo "<h3>Showing results for ID #". $data['user']->id."</h3>";  

  die();

  /* $data = array($id);
  $this->db->where('id', $id);
  $this->db->update('users', $data); */

}

}

Table View (adminlogin.php) 表格检视(adminlogin.php)

<html>
<head>
    <title>Admin Page</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body background=http://wallpapercave.com/wp/ItmVuT6.jpg>


<div class="container">
<table class="table table-bordered">
    <center>
    <thead> 
        <tr style="font-size: 24; color: white;">
            <th>First Name</th>
            <th>Last Name</th>
            <th>Email</th>
            <th colspan="2">Actions</th>
        </tr>
    </thead>
    </center>
    <tbody style="background-color: #F0F8FF;">  
        <?php foreach($users as $u){
        $class = "btn btn-primary";
        $button = "Approve";  
             if ($u->id == 7){
                $class = "btn btn-danger"; 
                $button = "Deny";
        } ?>
        <tr style="font-size: 20;">
            <td><?php echo $u->first_name; ?></td>
            <td><?php echo $u->last_name; ?></td>
            <td><?php echo $u->email; ?></td>
            <td><a class="btn btn-success" href="/ci/index.php/myform/edituser?user_id=<?php echo $u->id;?>">Edit</a></td>
            <td><a class="<?php echo $class;?>" href="/ci/index.php/myform/approve?user_id=<?php echo $u->id;?>"><?php echo $button;?></a></td>
        </tr>
        <?php } ?>
    </tbody>
</table>
</div>


</body>
</html>

Edit View (editfields.php) 编辑视图(editfields.php)

<html>
<head>
    <title>Edit User Info</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>

<body background=http://wallpapercave.com/wp/ItmVuT6.jpg>
<form action="" method="post">

<input type="hidden" name="id" value="<?php echo $id; ?>"/>

<div>

<p><strong>ID:</strong><?php echo $id; ?></p>

<strong>First Name</strong><input type="text" name='first_name' value="<?php echo $first_name; ?>"?>"/><br/>

<strong>Last Name</strong><input type="text" name='last_name' value="<?php echo $last_name; ?>"/><br/>

<strong>Email</strong><input type="hidden" name='email' value="<?php echo $email; ?>"/><br/>

<input type="submit" name="submit" value="Update">

</div>
</form>
</body>
</html>

The most basic, fundamental example I want to achieve at this juncture is at the following, though this example may not fit according to my code: 在此,我想实现的最基本的基本示例如下,尽管根据我的代码,该示例可能不适合:

http://www.killersites.com/community/index.php?/topic/1969-basic-php-system-vieweditdeleteadd-records/ http://www.killersites.com/community/index.php?/topic/1969-basic-php-system-vieweditdeleteadd-records/

I've updated my answer with the full test code for controller and views: 我已经用控制器和视图的完整测试代码更新了答案:

Myform.php Myform.php

  <?php


class Myform extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        // Added form and url helper so all functions can use them.
        $this->load->helper(array('form', 'url'));
    }

    public function index()
    {
        $this->load->library('form_validation');

        $this->form_validation->set_rules('user[first_name]', 'First Name', 'required');
        $this->form_validation->set_rules('user[last_name]', 'Last Name', 'required');
        $this->form_validation->set_rules('user[email]', 'Email Address', 'required|trim|valid_email');

        // The only reason i store the values in this variable is to
        // allow the re-use of the same form for both index and edituser
        // functions. This allows the re-population rule to work.
        $form['dbValue'] = [
            'first_name' => $this->input->post('user[first_name]'),
            'last_name'  => $this->input->post('user[last_name]'),
            'email'      => $this->input->post('user[email]'),
            'id'         => $this->input->post('id'),
        ];

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('myform', $form);
        }
        else
        {
            // I've disabled company here as i could not find any reference to it
            // in the form you have given as an example
            $user = $this->input->post('user');
            // $company = $this->input->post('company');
            echo '<pre>';
            print_r($user);
            // print_r($company);


            if ($this->input->post('id') !== '' && $this->input->post('id') !==NULL)
            {
                // id is defined so we update the user
                $this->db
                    ->where('id', (int) $this->input->post('id'))
                    ->update("User_Inputs", $user);
            }
            else
            {
                // id is not define so we create the user
                $this->db->insert("User_Inputs", $user);
            }


            // $this->db->insert("companies", $company);
            redirect("/myform/getDatabase");
        }
    }

    public function successmessage()
    {
        $this->load->view('formsuccess');
    }

    public function getDatabase()
    {
        $data['users'] = $this->db->get("User_Inputs")->result();
        $this->load->view('adminlogin', $data);
    }

    public function approve()
    {
        $id           = $this->input->get("user_id");
        $this->db->where("id", $id);
        $data['user'] = $this->db->get("User_Inputs")->row();
        echo "<pre>";
        print_r($this->db->last_query());
        print_r($data);
        echo "<h1>" . $data['user']->email . "</h1>";

        die();
    }

    public function edituser($user_id = NULL)
    {
        // Just making sure we are capturing the id as an integer
        $user_id = (int) $user_id;

        // Get the user based on the $user_id
        $data['user'] = $this->db
            ->where('id', $user_id)
            ->get("User_Inputs")
            ->row();

        // We will use the below variable to set the form fields to the correct values.
        $form['dbValue'] = [
            'first_name' => $data['user']->first_name,
            'last_name'  => $data['user']->last_name,
            'email'      => $data['user']->email,
            'id'         => $user_id
        ];

        $this->load->view('myform', $form);
    }

}

View: adminlogin.php 查看: adminlogin.php

<html>
    <head>
        <title>Admin Page</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    </head>
    <body background=http://wallpapercave.com/wp/ItmVuT6.jpg>


        <div class="container">
            <table class="table table-bordered">
                <center>
                    <thead>
                        <tr style="font-size: 24; color: white;">
                            <th>First Name</th>
                            <th>Last Name</th>
                            <th>Email</th>
                            <th colspan="2">Actions</th>
                        </tr>
                    </thead>
                </center>
                <tbody style="background-color: #F0F8FF;">
                    <?php
                    foreach ($users as $u)
                    {
                        $class  = "btn btn-primary";
                        $button = "Approve";
                        if ($u->id == 7)
                        {
                            $class  = "btn btn-danger";
                            $button = "Deny";
                        }
                        ?>
                        <tr style="font-size: 20;">
                            <td><?php echo $u->first_name; ?></td>
                            <td><?php echo $u->last_name; ?></td>
                            <td><?php echo $u->email; ?></td>
                            <td><a class="btn btn-success" href="<?php echo site_url('myform/edituser/' . $u->id); ?>">Edit</a></td>
                            <td><a class="<?php echo $class; ?>" href="<?php echo site_url('myform/approve/' . $u->id); ?>"><?php echo $button; ?></a></td>
                        </tr>
                    <?php } ?>
                </tbody>
            </table>
        </div>


    </body>
</html>

View: myform.php 查看: myform.php

  <?php
// if the set_value default values are not set set them to NULL


echo form_open('myform');

echo form_label('First Name', 'first_name');
echo form_input('user[first_name]', set_value('user[first_name]', isset($dbValue['first_name'])) ? $dbValue['first_name'] : '' );

echo form_label('Last Name', 'last_name');
echo form_input('user[last_name]', set_value('user[last_name]', isset($dbValue['last_name'])) ? $dbValue['last_name'] : '' );

echo form_label('Email', 'email');
echo form_input('user[email]', set_value('user[email]', isset($dbValue['email'])) ? $dbValue['email'] : '' );

echo form_hidden('id', set_value('id', isset($dbValue['id'])) ? $dbValue['id'] : '' );

echo form_submit('Submit', 'submit');

echo form_close();
echo validation_errors();

OK, for ajax see the below modifications: OK,对于ajax,请参见以下修改:

I've added jquery library and bootstrap library, 我添加了jquery库和bootstrap库,

adminlogin.php adminlogin.php

<html>
    <head>
        <title>Admin Page</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <!-- Latest compiled and minified JavaScript -->
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

    </head>
    <body background=http://wallpapercave.com/wp/ItmVuT6.jpg>


        <div class="container">
            <table class="table table-bordered">
                <center>
                    <thead>
                        <tr style="font-size: 24; color: white;">
                            <th>First Name</th>
                            <th>Last Name</th>
                            <th>Email</th>
                            <th colspan="2">Actions</th>
                        </tr>
                    </thead>
                </center>
                <tbody style="background-color: #F0F8FF;">
                    <?php foreach ($users as $u): ?>
                        <?php
                        // added approve class
                        $class  = "approve btn btn-primary";
                        $button = "Approve";
                        if ($u->id == 7)
                        {
                            $class  = " deny btn btn-danger";
                            $button = "Deny";
                        }

                        ?>
                        <!-- id is added to each row to uniquely identify them so we can change it's color-->
                        <tr id="row_<?= $u->id ?>"style="font-size: 20;">
                            <td><?php echo $u->first_name; ?></td>
                            <td><?php echo $u->last_name; ?></td>
                            <td><?php echo $u->email; ?></td>
                            <td><a class="btn btn-success"  href="<?php echo site_url('myform/edituser/' . $u->id); ?>">Edit</a></td>
                            <!-- data-id was added to store the user id -->
                            <td><a class="<?php echo $class; ?>" data-id="<?= $u->id ?>" href="<?php echo site_url('myform/approve/' . $u->id); ?>"><?php echo $button; ?></a></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>

        <script>
            $('.approve').on("click", function (e) {
                // prevent the browser to loading the page directly
                e.preventDefault();

                // get the user id from the button we clicked.
                var userid = $(this).data('id');

                //initiate ajax request
                $.ajax({
                    url: $(this).attr('href'),
                    method: 'POST',
                    success: function (data) {
                        // on success add the bootstrap success class to the row
                        $('#row_' + userid).addClass('success')
                    },
                    error: function (data) {
                        // on error add boorstrap dange class to the row
                        $('#row_' + userid).addClass('danger')
                    }
                });
            });
        </script>



    </body>
</html>

for testing purposes i've changed the myform/approve function 出于测试目的,我更改了myform / approve函数

public function approve()
    {

        //do something this is for testing
        $result = true;

        if ($result)
        {
            // success you have done was OK and we send back success code
            $this->output->set_status_header(200);
        } else
        {
            // failure we send back an 4* cient error code
            $this->output->set_status_header(400);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM