我有一些挑战更新表格与另一个表格的会话用户数据相关联。每次运行时,都会显示未定义的功能。请让我知道我在哪里弄错了所以可以解决它。请抽空查看它 我是新手。 这是控制器
public function index()
{
if ( $this->session->userdata('logged_in') )
{
$id = $this->session->userdata('id');
$this->load->model('Userinfo_model');
$data['result'] = $this->Userinfo_model->get_users($id);
$this->load->view("usermain_info", $data);
}
}
public function update_user (){
$data = $this->input->post('userId');
$id = array(
'balance' => $this->input->post('balance'),
'id' => $this->input->post('id')
);
$this->Userinfo_model->update_user($id, $data);
$this->index();
}
模型
function get_users($userid)
{
if (isset($this->session->userdata['logged_in'])) {
$userid = ($this->session->userdata['logged_in']['id']);
} else {
header("location: nwpgroup/nwp2/index.php");
}
/* all the queries relating to the data we want to retrieve will go in here. */
$query = $this->db->query("SELECT * FROM walletbalance WHERE userId='$userid'");
return $query;
}
function update_user($id,$data){
if (isset($this->session->userdata['logged_in'])) {
$data = ($this->session->userdata['logged_in']['id']);
} else {
header("location: nwpgroup/nwp2/index.php");
}
$this->db->where('userId', $data);
$this->db->update('walletbalance', $id);
}
查看
<form method="post" action="<?php echo base_url() . "index.php/userinfo/update_user"?>">
<?php if($result->num_rows() == 0){
echo 'No user found';
}
else {
foreach ( $result->result_array() as $new_user ){ ?>
<h4>Your name:<input value=" <?php echo $new_user['balance'] ?>" type="text" /> </h4><br />
<h4>Your name:<input value=" <?php echo $new_user['id'] ?>" type="text"/> </h4><br/>
<h4>Your name: <input value="<?php echo $new_user['userId'] ?>" type="hidden"/> </h4>
<input type="submit" id="submit" name="dsubmit" value="Update">
<?php }
}
?>
错误消息
Message: Undefined property: Userinfo::$Userinfo_model
和
Message: Call to a member function update_user() on null
谢谢,我很感激
答案 0 :(得分:0)
在update_user
功能中,您没有包含usermain_info
模型。这就是错误即将来临的原因。
请创建构造函数并在其中包含模型。我已经更新了你的代码。试试这个。
<?php
function __construct() {
parent::__construct();
$this->load->library('session');
$this->load->model('Userinfo_model');
}
public function index(){
if ( $this->session->userdata('logged_in') ){
$id = $this->session->userdata('id');
$data['result'] = $this->Userinfo_model->get_users($id);
$this->load->view("usermain_info", $data);
}
}
public function update_user (){
$data = $this->input->post('userId');
$id = array(
'balance' => $this->input->post('balance'),
'id' => $this->input->post('id')
);
$this->Userinfo_model->update_user($id, $data);
$this->index();
}
?>
答案 1 :(得分:0)
按如下方式更改您的观点:
<form method="post" action="<?php echo base_url() . "index.php/userinfo/update_user"?>">
<?php if($result->num_rows() == 0){
echo 'No user found';
}
else {
foreach ( $result->result_array() as $new_user ){ ?>
<h4>Your name:<input value=" <?php echo $new_user['balance'] ?>" type="text" name="balance" /> </h4><br />
<h4>Your name:<input value=" <?php echo $new_user['id'] ?>" type="text" name="id" /> </h4><br/>
<h4>Your name: <input value="<?php echo $new_user['userId'] ?>" type="hidden" name="userId"/> </h4>
<input type="submit" id="submit" name="dsubmit" value="Update">
<?php }
}
?>
</form>
仅当元素具有名称时,表单才会将数据发送到服务器
您无法多次提交表单。上面的代码将为每一行创建更新按钮。因此,如果要在单个更新中更新所有记录,请在codeigniter中使用update_batch()
。并按如下方式更改视图:
foreach ( $result->result_array() as $new_user ){ ?>
<h4>Your name:<input value=" <?php echo $new_user['balance'] ?>" type="text" name="balance" /> </h4><br />
<h4>Your name:<input value=" <?php echo $new_user['id'] ?>" type="text" name="id" /> </h4><br/>
<h4>Your name: <input value="<?php echo $new_user['userId'] ?>" type="hidden" name="userId"/> </h4>
<?php } ?>
<input type="submit" id="submit" name="dsubmit" value="Update">
供参考:https://www.codeigniter.com/userguide3/database/query_builder.html#updating-data