gpt4 book ai didi

php - 使用 PHP CodeIgniter 编辑 MySQL 表

转载 作者:行者123 更新时间:2023-11-29 07:03:06 26 4
gpt4 key购买 nike

当管理员为 SQL 表 (adminlogin.php) 中的任意行选择“编辑”按钮时,我不确定如何加载 editfields.php 文件。

我觉得我的 editfields.php 文件有正确的内容,但在获取数据并将该数据显示在我的 View 中而不是根据我的 edituser 函数打印的内容时,我陷入了困境。

** 请暂时忽略approve 函数,以及adminlogin.php 中的if 条件,这两个项目单独讨论**

以下是以下文件:

Controller (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); */

}

}

表格 View (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>

编辑 View (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>

我现在想要实现的最基本的示例如下,尽管这个示例可能不适合我的代码:

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

最佳答案

我已经用 Controller 和 View 的完整测试代码更新了我的答案:

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);
}

}

查看: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>

查看: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();

关于php - 使用 PHP CodeIgniter 编辑 MySQL 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42658282/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com