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>