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>
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
well first we need to understand what is MVC
means if you are going to make a form and on submit, data should be saved into database.
so here you need to define few stuff.
like codeigniter have few available libraries / helpers - database (update to your credentials) - form (load on all form related pages)
Automatically your database is accessible in your model so no need to connect again.
so your first page localhost/assign9
in your routes.php
, you have to define default router. which will be like:
$route['default_controller'] = 'welcome/index'; // here welcome is controller name and index is function/method name
now in views, create folder of controller's name welcome
and then create file index.php
which is your view file means your all html or php related code will be here.
Here you need to use Codeigniter's Helper FORM and create form using that. No need to define form manually one by one elements. Use Form, it will be easy and quick.
now, in controller use same function or new to process form data. you may add various conditions like form submit or not, empty or not etc
now in Models, create a model file of your database table name and define table name, primary key, null allowed etc and unique.
Codeigniter already have feature to use model's setting and insert or it will return an error.
So use that :)