【问题标题】:Fetch single record from database and display in text box using Codeigniter使用 Codeigniter 从数据库中获取单个记录并显示在文本框中
【发布时间】:2015-10-21 09:42:54
【问题描述】:

我在数据库中有多个记录。 当我单击任何 id 时,所有记录都应使用 Codeigniter 显示在文本框中。 当我单击用户 ID 页面时,我有两个选项卡自动刷新,并且我在 URL 中获取用户 ID,并且选项卡重定向到第一个选项卡。

我不想这样。只有当我点击用户 ID 时,我才必须在第二个选项卡上显示记录

我试过下面的代码,为什么不行?

控制器

public function update_retrive()
{
$id = $this->uri->segment(3);
$this->load->model("model_add_user");
$d=$this->model_add_user->update_retrive($id);
$data['posts'] = $d;
}

型号

public function update_retrive($userId)
{
$this->db->where('userId',$userId);
$this->db->from('add_user');
$q = $this->db->get();
return $q;
}

查看

<div id="btnclick" style="display: none">
<?php
foreach ($posts->result() as $post)
{
?>
<input type="text" name="userId" placeholder="USER ID" style="width:200px" value='<?php echo $post->userId;?>'>&nbsp;
<input type="date" name="date" style="width:200px" value='<?php echo $post->date;?>'>
<input type="text" name="firtsname" placeholder="FIRST NAME" style="width:200px" value='<?php echo $post->firtsname;?>'>&nbsp;
<input type="text" name="middlename" placeholder="MIDDLE NAME" style="width:200px" value='<?php echo $post->middlename;?>'>&nbsp;
<input type="text" name="lastname" placeholder="LAST NAME" style="width:200px" value='<?php echo $post->lastname;?>'>&nbsp;
<input type="text" name="mobileno" placeholder="ENTER MOBILE NO." style="width:200px" value='<?php echo $post->mobileno;?>'>&nbsp;
<input type="text" name="landline" placeholder="LANDLINE No." style="width:410px" value='<?php echo $post->landline;?>'>
<textarea name="address" placeholder="ENTER ADDRESS" style="width:410px" ><?php echo $post->address;?></textarea>
<input type="text" name="city" placeholder="CITY" style="width:200px" value='<?php echo $post->city;?>'>&nbsp;
<input type="text" name="locality" placeholder="LOCALITY" style="width:200px" value='<?php echo $post->locality;?>'>
<input type="text" name="email" placeholder="ENTER EMAIL" style="width:410px" value='<?php echo $post->email;?>'></br>
<input type="submit" class="submit" name="UPDATE" value="UPDATE" >
<input type="submit" class="submit" name="cancel" value="Cancel" >
<?php }
?>
</div>

【问题讨论】:

    标签: html codeigniter


    【解决方案1】:

    你的模型兄弟应该是这样的

    public function update_retrive($userId)
    {
    $this->db->where('userId',$userId);
    $this->db->from('add_user');
    $q = $this->db->get();
    return $q->result();// this will get all the result inside the database
    }
    

    在你看来会很自在。

    控制器应该是这样的

    public function update_retrive()
    {
    $id = $this->uri->segment(3);
    $this->load->model("model_add_user");
    $d=$this->model_add_user->update_retrive($id);
    $data['posts'] = $d;
    $this->load->view('yourview',$data);
    }
    

    控制器回答:@ricky

    注意:重新检查您尝试访问的页面。

    【讨论】:

    • 我遇到了问题,请检查有问题的图片和描述
    • 你能发布一个屏幕截图来说明视图是怎样的
    • 如果我点击一个用户,我应该会看到该用户的所有信息,这些信息将放在文本框上
    • 查看,如果我单击任何用户 ID 记录应显示在同一选项卡上,则用户列表选项卡上的所有用户 ID 记录都可用
    • 我没有在那个标签上出错
    【解决方案2】:

    在控制器中

    public function update_retrive()
    {
    $id = $this->uri->segment(3);
    $this->load->model("model_add_user");
    $d=$this->model_add_user->update_retrive($id);
    $data['posts'] = $d;
    $this->load->view('yourview',$data);
    }
    

    在你看来

    <div id="btnclick" style="display: none">
    <?php
    foreach ($posts as $post)
    {
    ?>
    <input type="text" name="userId" placeholder="USER ID" style="width:200px" value='<?php echo $post->userId;?>'>&nbsp;
    <input type="date" name="date" style="width:200px" value='<?php echo $post->date;?>'>
    <input type="text" name="firtsname" placeholder="FIRST NAME" style="width:200px" value='<?php echo $post->firtsname;?>'>&nbsp;
    <input type="text" name="middlename" placeholder="MIDDLE NAME" style="width:200px" value='<?php echo $post->middlename;?>'>&nbsp;
    <input type="text" name="lastname" placeholder="LAST NAME" style="width:200px" value='<?php echo $post->lastname;?>'>&nbsp;
    <input type="text" name="mobileno" placeholder="ENTER MOBILE NO." style="width:200px" value='<?php echo $post->mobileno;?>'>&nbsp;
    <input type="text" name="landline" placeholder="LANDLINE No." style="width:410px" value='<?php echo $post->landline;?>'>
    <textarea name="address" placeholder="ENTER ADDRESS" style="width:410px" ><?php echo $post->address;?></textarea>
    <input type="text" name="city" placeholder="CITY" style="width:200px" value='<?php echo $post->city;?>'>&nbsp;
    <input type="text" name="locality" placeholder="LOCALITY" style="width:200px" value='<?php echo $post->locality;?>'>
    <input type="text" name="email" placeholder="ENTER EMAIL" style="width:410px" value='<?php echo $post->email;?>'></br>
    <input type="submit" class="submit" name="UPDATE" value="UPDATE" >
    <input type="submit" class="submit" name="cancel" value="Cancel" >
    <?php }
    ?>
    </div>
    

    【讨论】:

    • 我在 url 上获取用户 ID,输出是 404 Page Not Found The page you request was not found.
    猜你喜欢
    • 1970-01-01
    • 2013-12-03
    • 2015-06-13
    • 1970-01-01
    • 2019-05-07
    • 2018-07-26
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    相关资源
    最近更新 更多