Home.php 这是我的控制器
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
session_start();
class home extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('shakiladb');
}
public function index() {
$this->load->view('index');
//loading session library
$this->load->library('session');
//adding data to session
$this->session->set_userdata('username', 'password');
$this->load->view('session_view');
}
public function register() {
$this->load->view('register');
}
public function checkdb() {
//contact the model to run the query
$this->shakiladb->savetodb();
}
public function unset_session_data() {
//loading session library
$this->load->library('session');
//removing session data
$this->session->unset_userdata('username');
$this->load->view('session_view');
}
}
shakiladb.php 这是我的模特
<?php
class shakiladb extends CI_Model {
public function __construct() {
parent::__construct();
$this->load->database();
}
public function savetodb() {
//capture the username and password
$username = $this->input->post('Username');
$password = $this->input->post('Password');
//query to save in the table
//$query = $this->db->query("INSERT INTO table1 (USERNAME, PASSWORD) VALUES ('$username', '$password')");
$validate = $this->db->query("SELECT USERNAME FROM table1 WHERE USERNAME = '".$username."'");
$count = count($validate);
if ($validate->num_rows() > 0) {
echo "Not working";
} else {
$this->db->query("INSERT INTO table1 (USERNAME, PASSWORD) VALUES ('$username', md5('$password'))");
echo "Works";
}
}
}
register.php 这是我的观点
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<?php
echo $this->session->userdata('session');
?>
<div align="center">
<form id="reg">
Username : <input type="text" name="Username" id="Username"><br><br>
Password : <input type="password" name="Password" id="Password"><br><br>
<input type="submit" name="btn" id="btn" value="OK">
<input type="reset" name="Reset" name="Reset" value="Reset"><br><br>
</form>
</div>
<script type="text/javascript">
$(function() {
function validation() {
var username = $("#Username").val();
var password = $("#Password").val();
if (username == "") {
alert("Please fill in the username field!");
return false;
}
if (password == "") {
alert("Please fill in the password field!");
return false;
}
return true;
}
$('#btn').click(function() {
var method = validation();
if (method == true) {
$.ajax({
url: "http://localhost/shark/home/checkdb",
type: 'POST',
data: $("#reg").serialize(),
success: function(result) {
if (result == "Not working") {
alert("Already");
}
if (result == "Works") {
alert("Not existing");
}
}
});
return false;
}
return false;
});
});
</script>
</body>
</html>
这是我的代码,我需要知道如何使用会话并实现从数据库中检索数据的目标?
答案 0 :(得分:0)
Home.php 这是您的控制器
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class home extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('session');
$this->load->model('shakiladb');
}
public function index() {
$this->load->view('index');
//loading session library
//adding data to session
$this->session->set_userdata('username', 'password');
$this->load->view('session_view');
}
public function register() {
$this->load->view('register');
}
public function checkdb() {
//contact the model to run the query
$username = $this->input->post('Username');
$password = $this->input->post('Password');
$this->shakiladb->savetodb($username,$password);
}
public function unset_session_data() {
//loading session library
//removing session data
$this->session->unset_userdata('username');
$this->load->view('session_view');
}
}
shakiladb.php 这是你的模特
<?php
class shakiladb extends CI_Model {
public function __construct() {
parent::__construct();
$this->load->database();
}
public function savetodb($username,$password) {
//capture the username and password
$password=md5($password);
//query to save in the table
//$query = $this->db->query("INSERT INTO table1 (USERNAME, PASSWORD) VALUES ('$username', '$password')");
$validate = $this->db->query("SELECT USERNAME FROM table1 WHERE USERNAME = '".$username."'");
$count = count($validate);
if ($validate->num_rows() > 0) {
echo "Not working";
} else {
$this->db->query("INSERT INTO table1 (USERNAME, PASSWORD) VALUES ('".$username."', '".$password."')");
echo "Works";
}
}
}