gpt4 book ai didi

php - 使用不同的模型/页面插入一行 - CodeIgniter

转载 作者:行者123 更新时间:2023-11-28 23:21:37 24 4
gpt4 key购买 nike

我是 CodeIgniter 的新手,我正在构建一个简单的 I/O 网站。我只有一个数据库,比如 test 和一张表 results,看起来像这样:

表格“results”的截图

enter image description here

我有两个 View personal.phpsongs.php。在第一个 View 中,我收集要插入到字段 2、3 和 4 中的数据值,其余值在第二个 View 中收集。然后通过相关模型 personal_modelsongs_model 将它们插入到表中。现在很明显,它们将被插入到 2 个不同的行中,这不是我想要的。这里有什么诀窍?我应该如何管理它?到目前为止,我一直想获得最后一个 ID,但我不知道该怎么做。提前致谢!

personal.php(第一 View )

<?php echo validation_errors(); ?>
<?php echo form_open('data/personal'); ?> //passes the data to the controller that loads the personal_model.php
"some input fields"
<button type="submit" name="submit">Submit Data</button>

songs.php(第二个 View )

<?php echo validation_errors(); ?>
<?php echo form_open('data/songs'); ?> //passes the data to the controller that loads the songs_model.php
"some input fields"
<button type="submit" name="submit">Submit Rating</button>

personal_model.php(第一个模型)

<?php
class Personal_model extends CI_Model {
public function __construct()
{
$this->load->database();
}

public function insert_personal()
{
$this->load->helper('url');

$data = array(
'age' => $this->input->post('user_age'),
'education' => $this->input->post('user_edu'),
'twitter' => $this->input->post('user_twitter'),
'facebook' => $this->input->post('user_facebook')
);

return $this->db->insert('results', $data);
}
}

songs_model.php(第二个模型)

<?php
class Ratings_model extends CI_Model {
public function __construct()
{
$this->load->database();
}

public function insert_ratings()
{
$this->load->helper('url');
#$this->load->database();
$data = array(
'score' => $this->input->post('rating'),
'song1' => $this->input->post('rnd1'),
'song2' => $this->input->post('rnd2')
);

return $this->db->insert('results', $data);
}
}

最佳答案

你的 Controller 函数应该是这样的。

public function personal()
{
$lastInsertedID = $this->Personal_model->insert_personal();
$this->session->set_userdata("personalID",$lastInsertedID);
}

在上面的 Controller 函数中将最后插入的 id 设置到 session 中,它应该从您的 Personal_model 返回。这是代码。

public function insert_personal()
{
$this->load->helper('url');

$data = array(
'age' => $this->input->post('user_age'),
'education' => $this->input->post('user_edu'),
'twitter' => $this->input->post('user_twitter'),
'facebook' => $this->input->post('user_facebook')
);

$this->db->insert('results', $data);
return $this->db->insert_id();
}

然后更新 insert_ratings 函数中的现有行,而不是插入记录。这是代码。

public function insert_ratings()
{
$data = array(
'score' => $this->input->post('rating'),
'song1' => $this->input->post('rnd1'),
'song2' => $this->input->post('rnd2')
);
$personalID = $this->session->userdata("personalID");
$this->db->where("id",$personalID);
$this->db->update('results', $data);
return $this->db->affected_rows();
}

然后在提交您的 song.php 表单时不会将新记录插入表中,它将更新现有记录。

关于php - 使用不同的模型/页面插入一行 - CodeIgniter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41324181/

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