duanqin9631 2018-12-04 14:33
浏览 55

如何使用codeIgniter在PHP中使用控制器访问三个视图页面?

I am attempting to build a website where the user will input his/her first and last name and the database will store the data. Also, there will also be an option where the user can choose to add data or view all the records. Now my problem is that whenever I access http://localhost/assign9/ which is my index, only signup.php shows up then will not store data after I click submit. How do I make the code as if the 3 codes will work in a sequence? signContr.php(controller for assign9) class signContr extends CI_Controller{

public function index(){
$this->load->view('signup');
}
public function insert(){
$this->load->view('saveinfo');
}
public function display(){
$this->load->view('displayinfo');
}
 public function _construct(){
      parent::_construct();
      $this->load->helper('url');
     }
}

signup.php

    <h1>Sign in</h1>
    <div class="container">
        <div class="sign-up-content">
            <form method="POST" class="signup-form">

                <div class="form-textbox">
                    <label for="fname">First Name:</label>
                    <input type="text" name="fname" id="fname" />
                </div>
                <div class="form-textbox">
                    <label for="lname">Last Name:</label>
                    <input type="text" name="lname" id="lname" />
                </div>

                <div class="form-textbox">
                    <input type="submit" name="submit" id="submit" class="submit" value="Create account" />
                </div>
            </form>
        </div>
    </div>
</div>

saveinfo.php

$fname = filter_input(INPUT_POST, 'fname');
$lname= filter_input(INPUT_POST, 'lname');
if (!empty($fname)||!empty($lname)){
$host = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbname = "information";
//create connection
$conn = new mysqli($host, $dbUsername, $dbPassword, $dbname);
if (mysqli_connect_error()) {
 die('Connect Error('. mysqli_connect_errno().')'. mysqli_connect_error());
} else {
  $sql = "INSERT Into name(fname,lname) values('$fname','$lname')";

 if ($conn->query($sql)) {
  echo '"New record inserted sucessfully"  <br>';

  echo '<a href=\assign9\application\views\displayinfo.php>'
  . 'Click here to view all usernames and passwords</a>';
 } else {
  echo "Error!";
 }

 $conn->close();
}
} else {
 echo "All fields are required";
 die();
}
?>
}

?>

displayinfo.php

<body>
<h1>Records</h1>
<br><br><br>
<table>
    <tr>

        <th>First Name</th>
        <th>Last Name</th>
    </tr>

    <?php

        $conn = mysqli_connect("localhost", "root","", "information");

        if($conn->connect_error){
            die("Connection failed: " .$conn->connect_error);
        }


        $result = mysqli_query($conn,"SELECT * FROM name");

        if($result->num_rows>0){
            while($row = mysqli_fetch_array($result)){
                echo
                "<tr><td>".  $row["fname"].
                "</td><td>". $row["lname"].
                "</td></tr>";
            }
            echo "</table>";

        } else{
                echo "0 result";
            }
        $conn->close();
    ?>
</table>
<br><br>
<input type="button" value="Add Data"                    
 onclick="window.location.href='http://localhost/assign9/'"/>
</body>
</html>
  • 写回答

2条回答 默认 最新

  • donglun2024 2018-12-04 16:07
    关注

    Before addressing your problem, I'd really think you would benefit from reading a bit about how MVC works and the first chapters of CodeIgniter's user guide. I'll clear a lot of doubts for you.

    Having said that, there's a lot of issues in your code:

    1.- constructor methods are much better placed at the top of the controller (before any user methods) 2.- your main index() method is invoking views it doesn't need. If you need to display the form, just invoke $this->load-view('signup'); Views should have all the logic for storing data on a database (that's between the controller and a model)

    3.- your signup view creates a form but doesn't have any action defined (<form method="post" action="controller/method"> would be advisable) 4.- the form should point to a different method (not index()) which would take the user input, validate it and process it by inserting it in the database. For example, if the form points to 'signcontr/process', you'd define:

    public function process()
    {
        // code that validates and processes user input
    }
    

    After having the process() method do its work, you may invoke a confirmation view ($this->load->view('signup_confirmation'); for example) which displays the result of the user action.

    There's a lot that could be written about your code. I strongly suggest to follow the CI user guide (it has basic examples that'll allow you to get a better grasp of how CI can make your life easier) and rewrite your code accordingly

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题