doujiena0025 2016-04-30 03:50
浏览 18
已采纳

too long

Using this code I displayed the form

 echo "<form action='Stud_controller/updateData' method='POST'>";
 echo '<input type="hidden" name="sameId" value="'.$id.'">';
 echo 'Name: <input type="text" name="newName" value="'.$name.'"> &nbsp;';
 echo '<input type="submit" value="Save">';
 echo "</form>";

instead of using this code I posted earlier

 echo "<form action="Stud_controller/updateData" method="POST">";
 echo "<input type="hidden" name="sameId" value=".'"'.$id.'">';
 echo "Name: <input type="text" name="newName" value=".'"'.{$name}.'"> &nbsp;';
 echo "<input type="submit" value="Save">";
 echo "</form>";

Then this pops-out after I POSTED the values

Message: Undefined variable: id

Filename: views/Edit_view.php

Message: Undefined variable: name

Filename: views/Edit_view.php

This is the whole package

Stud_controller.php

<?php 
   class Stud_controller extends CI_Controller {  

    public function __construct()
{
        parent::__construct();
    $this->load->helper('url');
    $this->load->model('Stud_model');

 }

  public function index() { 
    $this->load->helper('form');
    $data['data'] = $this->Stud_model->getData();

    $this->load->view('Stud_view', $data);
  } 

  public function deleteData($row)
  {

    $this->Stud_model->delete($row);
    $this->redirect();
  }

  public function editData($row)
  { 
    $data['singleData'] = $this->Stud_model->getSingleData($row);
    $this->load->view('Edit_view', $data);
  } 

  public function updateData()
  {
    $data = array('id' => $this->input->post('sameId'), 'fname' => $this->input->post('newName'));
    $this->Stud_model->update($data);
    $this->redirect();

  }

  public function addData()
  { 
    $id = NULL;
    $name = $this->input->post('name');

    $data = array(
        'stud_id' => $id,
        'name' => $name,
    );

    $this->Stud_model->add($data);
    $this->redirect();
  } 

  public function redirect()
  {
    $this->load->helper('form');

    $data['data'] = $this->Stud_model->getData();

    redirect('http://localhost/gpdolotina/index.php/Stud_controller');

    $this->load->view('Stud_view', $data);
  }
  } 
 ?>

Edit_view.php

<!DOCTYPE html> 
<html lang = "en"> 

<head> 
    <meta charset = "utf-8"> 
    <title>Edit</title> 
</head>

<body> 
  <?php
    echo "This is the edit_view.";
    echo "<br /><br />";

    foreach ($singleData as $edit)
    {
        $id = $edit->stud_id;
        $name = $edit->name;
        echo $id;
    }

    echo "<form action='Stud_controller/updateData' method='POST'>";
    echo '<input type="hidden" name="sameId" value="'.$id.'">';
    echo 'Name: <input type="text" name="newName" value="'.$name.'"> &nbsp;';
    echo '<input type="submit" value="Save">';
    echo "</form>";

    ?>
  <a href="http://localhost/gpdolotina/index.php/Stud_controller">Home</a>


 </body>

 </html>

Stud_view.php

  <!DOCTYPE html> 
  <html lang = "en"> 
  <head> 
   <meta charset = "utf-8"> 
   <title>View Students</title> 
  </head>

  <body> 
  <?php
    echo "This is the view.";
    echo "<br /><br />";
  ?>

  <form method="post" accept-charset="utf-8" action="Stud_controller/addData">

     Name: <input type="text" name="name">&nbsp;
     <input type="submit" value="Add Name"><br><br>

  </form>


  <table border="1">
  <?php
    echo "<tr>";
    echo "<td>Student ID</td>"; 
    echo "<td>Name</td>"; 
    echo "<td>Edit</td>"; 
    echo "<td>Delete</td>"; 
    echo "<tr>"; 

     foreach ($data as $row)
    {
         echo "<tr>";
           echo "<td>".$row->stud_id."</td>"; 
           echo "<td>".$row->name."</td>"; 
           echo "<td><a href = '"."stud_controller/editData/"
              .$row->stud_id."'>Edit</a></td>"; 
           echo "<td><a href = '"."stud_controller/deleteData/"
              .$row->stud_id."'>Delete</a></td>"; 
           echo "<tr>"; 
           //
    }

  ?>
  </table>
 </body>

 </html>

Stud_model.php

 <?php 
  class Stud_Model extends CI_Model {

  function __construct() { 
     parent::__construct(); 
     $this->load->database();
  } 

  public function add($data) { 
     if ($this->db->insert("stud", $data)) { 
        return true; 
     } 
  } 

  public function delete($stud_id) { 
     if ($this->db->delete("stud", "stud_id = ".$stud_id)) { 
        return true; 
     } 
  } 

  public function update($data) { 
     $this->db->set("name", $data['name']); 
     $this->db->where("stud_id", $data['id']); 
     $this->db->update("stud", $data); 
  } 

  public function getSingleData($stud_id)
  {
     $getSingleData = $this->db->select("name");
     $getSingleData = $this->db->select("stud_id");
     $getSingleData = $this->db->from("stud");
     $getSingleData = $this->db->where("stud_id", $stud_id);
     $getSingleData = $this->db->get();

     return $getSingleData->result();
  }

  public function getData()
  {

     $getdata = $this->db->select("*");
     $getdata = $this->db->from("stud");
     $getdata = $this->db->get();

     return $getdata->result();
  }
 } 
 ?> 
  • 写回答

4条回答 默认 最新

  • dongshetao1814 2016-04-30 03:52
    关注

    Use like this :

    echo "<form action='Stud_controller/updateData' method='POST'>";
    echo '<input type="hidden" name="sameId" value="'.$id.'">';
    echo 'Name: <input type="text" name="newName" value="'.$name.'"> &nbsp;';
    echo '<input type="submit" value="Save">';
    echo "</form>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端