我是codeigniter的新手。 最初我忽略了,但问题是当我点击输入类型时,ask_ques数据的提交在数据库中进入两次。 user.php是模型文件,insert.php控制器的savedata1方法是调用模型的insertques方法。 请帮助我,并解释我的编程中的缺陷 提前谢谢。
login_client.php
<html ">
<head>
<title>Simple Login with CodeIgniter</title>
</head>
<body>
<h1>User Login </h1>
<?php echo validation_errors(); ?>
<?php echo form_open('client_login'); ?>
<label for="username">Username:</label>
<input type="text" size="20" id="username" name="username"/>
<br/>
<label for="password">Password:</label>
<input type="password" size="20" id="passowrd" name="password"/>
<br/>
<input type="submit" value="Login"/>
</form>
</body>
</html>
client_login.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Client_login extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('user','',TRUE);
}
function index()
{
//This method will have the credentials validation
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
if($this->form_validation->run() == FALSE)
{
//Field validation failed. User redirected to login page
$this->load->view('client_login');
}
else
{
//Go to private area
redirect('home2', 'refresh');
}
}
function check_database($password)
{
//Field validation succeeded. Validate against database
$username = $this->input->post('username');
//query the database
$result = $this->user->login($username, $password);
if($result)
{
$sess_array = array();
foreach($result as $row)
{
$sess_array = array(
'userid' => $row->userid,
'username' => $row->email
);
$this->session->set_userdata('logged_in', $sess_array);
}
return TRUE;
}
else
{
$this->form_validation->set_message('check_database', 'Invalid username or password');
return false;
}
}
}
?>
home2.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start(); //we need to call PHP's session object to access it through CI
class Home2 extends CI_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['userid'] = $session_data['userid'];
$this->load->view('home_page1', $data);
//
}
else
{
//If no session, redirect to login page
redirect('calllogin', 'refresh');//
}
}
function logout()
{
$this->session->unset_userdata('logged_in');
session_destroy();
redirect('home2', 'refresh');
}
function askques()
{
$this->load->helper("form");
$this->load->view('ask_ques');
}
}
?>
calllogin.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Calllogin extends CI_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->helper('form');
$data["email"]="";
$this->load->view('login_client',$data);
}
}
?>
home_page1.php
<?phpsession_start(); ?>
<div align="center" style="background-color: #b0c4de;height:50px">
<h2>Welcome <?php ?>!</h2>
<span style="color:blue;"><a href="home2/logout">Logout</a></span>
<!--<span style="color:blue">login</a></span> -->
<span style="color:blue"><input type="text" id=""style="height:30px;width:300px">search</span>
<span style="color:blue;margin-left:50px" id="notification" >
<input type="hidden" id="h_v" value="<?php echo $userid ;?>"/>
</span>
</div>
<div align="center" style="background-color: #b0c4de;height:100px;">
<span style="color:blue;"> <img src="<?php echo base_url('images/myimage.png');?>" width="50" height="60">
</span>
<span style="color:blue" id="questions"> Question
</span>
<span style="color:blue"> Tag
</span>
<span style="color:blue"> User
</span>
<span style="color:blue"> Unanswered
</span>
<span style="color:blue;margin-left:300px" > <a href="<?php echo site_url('home2/askques')?>">Askquestions</a>
</span>
</div>
<div id="title" style="background-color: #b0c4de;height:80px;width:800px;margin-left:100px;float:left">
</div>
<div id="date" style="background-color: #b0c4de;height:80px;width:80px;float:left;margin-left:100px">
</div>
<div id="ques" style="background-color:#b0c4de ;height:500px;width:500px; margin-top: 100px;margin-left:100px;overflow: auto;
scrollbar-base-color:#ffeaff;-ms-overflow-y: hidden;">
<!--<p style="height:800px;width:600px"></p> -->
</div>
<div id="tag" style="background-color: #b0c4de;height:50px;width:100px; margin-top:10px;margin-left:100px;">
</div>
<div hidden id="oldans" style="background-color:#b0c4de ;height:400px;width:600px; margin-top: 20px;margin-left:100px;overflow: auto;
scrollbar-base-color:#ffeaff;-ms-overflow-y: hidden;">
</div>
<div id="ans" style="background-color:#b0c4de ;height:400px;width:600px; margin-top: 20px;margin-left:100px;overflow: auto;
scrollbar-base-color:#ffeaff;-ms-overflow-y: hidden;">
<textarea name='quesans' id='quesans' style="height:400px;width:600px;overflow: auto; scrollbar-base-color:#ffeaff;-ms-overflow-y: hidden;" rows="80000"/></textarea>
</div>
<span id="pspan">Answer your:</span>
<span id="poans"></span>
ask_ques.php
<html>
<?php echo validation_errors(); ?>
<form name="" id="" method="post" action="<?php echo base_url();?>index.php/insert/savedata1" >
<div align="center" style="background-color: #b0c4de;height:50px">
<!--<span style="color:blue;">signup</span> -->
<span style="color:blue"><input type="text" id=""style="height:30px;width:300px"> search</span>
</div>
<div align="center" style="background-color: #b0c4de;height:100px;">
<span style="color:blue;"> <img src="<?php echo base_url('images/myimage.png');?>" width="50" height="60">
</span>
<span style="color:blue"> Question
</span>
<span style="color:blue"> Tag
</span>
<span style="color:blue"> User
</span>
<span style="color:blue"> Unanswered
</span>
</div>
<div align="" style="background-color: #b0c4de;height:80px;width:800px;margin-left:100px;float:left">
<span style="color:blue;margin-left:100px">Title:<input type="text" name="title" id="title" style="height:30px;width:400px;"></span>
</div>
<div align="" style="background-color: #b0c4de;height:80px;width:80px;float:left;margin-left:100px">
</div>
<div align="" style="background-color: #b0c4de;height:500px;width:500px; margin-top: 100px;margin-left:100px;">
<textarea name='comment' id='comment' style="height:500px;width:900px;overflow: auto; scrollbar-base-color:#ffeaff;-ms-overflow-y: hidden;" rows="80000"/></textarea>
<!--<p style="height:800px;width:600px"></p> -->
</div>
<div align="" style="background-color: #b0c4de;height:50px;width:200px; margin-top:10px;margin-left:100px;">
Tag:<input type="text" value="" name="tag" >
<input type="submit" value="Post question" >
</div>
</form>
</html>
insert.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start(); //we need to call PHP's session object to access it through CI
class Insert extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->library("form_validation");
$this->load->model('user');
}
function index()
{
$title=$this->input->post("title");
$comment=$this->input->post("comment");
$tag=$this->input->post("tag");
$this->form_validation->set_rules('title', 'Title ', 'required');
$this->form_validation->set_rules('comment', 'Question is', 'required');
$this->form_validation->set_rules('tag', 'Tag ', 'required|callback_savedata1');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('ask_ques');
}
}
function savedata1(){
$title=$this->input->post("title");
$comment=$this->input->post("comment");
$tag=$this->input->post("tag");
$bool=$this->user->insertques($title,$comment,$tag);
if($bool == true){
$session_data = $this->session->userdata('logged_in');
$data['userid'] = $session_data['userid'];
$this->load->view('home_page1', $data);
}
else
{
$this->form_validation->set_message('savedata','please check ur question');
//echo "hi";
//echo 'enter unique no';
return FALSE;
}
}
function getdata()
{
$session_data = $this->session->userdata('logged_in');
$data = $session_data['userid'];
$this->user->get_ques($data);
}
function getalldata()
{
$this->user->get_quest();
}
function display()
{
$this->user->get_single();
}
function inserans()
{
$this->user->set_ans();
}
function selectans()
{
$this->user->select();
}
function data()
{
$this->user->data();
}
function status()
{
$this->user->gets();
}
}
?>
user.php // model file
<?php
//session_start(); //we need to call PHP's session object to access it through CI
Class User extends CI_Model
{
function login($username, $password){
$this -> db -> select('userid,email, password');
$this -> db -> from('clients');
$this -> db -> where('email = ' . "'" . $username . "'");
$this -> db -> where('password = ' . "'" . $password . "'");
$this -> db -> limit(1);
$query = $this -> db -> get();
if($query -> num_rows() == 1)
{
return $query->result();
}
else
{
return false;
}
}
function saveemail($email,$pass){
$sql="INSERT INTO admin(email,password) VALUES ('$email','$pass')";
if(mysql_query($sql))
return true;
else
return false;
}
function savedetails($email,$pass){
$sql="INSERT INTO clients(email,password) VALUES ('$email','$pass')";
if(mysql_query($sql))
return true;
else
return false;
}
function insertques($title,$comment,$tag){
$session_data = $this->session->userdata('logged_in');
$data = $session_data['userid'];
//echo $data+"hisd";
$myText = (string)$comment;
$sql="INSERT INTO Question(title,question,data,datetime,tag) VALUES ('$title','$myText','$data',NOW(),'$tag')";
mysql_query($sql);
if(mysql_query($sql))
{
$sql= "SELECT * FROM Question WHERE qid=(Select MAX(qid) from question WHERE data =$data)";
$recordset = mysql_query($sql);
while($row=mysql_fetch_array($recordset))
{
$d= $row['qid'];
}
$sql1="INSERT INTO status(qid,data,status) VALUES ('$d','$data','0')";
mysql_query($sql1);
return true;
}
else
return false;
}
function get_ques($data)//intially call
{
$sql= "SELECT * FROM Question WHERE qid=(Select MAX(qid) from question WHERE data =$data)";
$recordset = mysql_query($sql);
// $data = array();
if(mysql_num_rows($recordset) > 0) //if record exists
{
while ($curr_record = mysql_fetch_assoc($recordset))
{
//$data[] = $curr_record;
echo json_encode($curr_record );
}
// echo json_encode($data);
}
}
function get_quest()
{
$sql= "SELECT title,qid ,datetime FROM Question ";
$data = array();
$recordset = mysql_query($sql);
if(mysql_num_rows($recordset) > 0) //if record exists
{
while ($curr_record = mysql_fetch_assoc($recordset))
{
$data[] = $curr_record;
//echo json_encode($curr_record);
}
echo json_encode($data);
}
else
echo json_encode($data);
}
function get_single()
{
$id=$_REQUEST['qid'];
$sql= "SELECT * FROM Question WHERE qid='$id' ";
$recordset = mysql_query($sql);
if(mysql_num_rows($recordset) > 0) //if record exists
{
while ($curr_record = mysql_fetch_assoc($recordset))
{
echo json_encode($curr_record);
}
}
else
echo json_encode();
}
function set_ans()
{
$id=$_REQUEST['qid'];
$ans=$_REQUEST['ans'];
$data=$_REQUEST['data'];
$sql="INSERT INTO answer(qid,answer,datetime,data) VALUES ('$id','$ans',NOW(),'$data')";
mysql_query($sql);
if(mysql_query($sql))
{
$sql1="Update status set status= 1 where qid='$id'";
mysql_query($sql1);
echo yes;
}
}
function select()
{
$id=$_REQUEST['qid'];
$sql= "SELECT * FROM answer WHERE qid='$id' ";
$data = array();
$recordset = mysql_query($sql);
if(mysql_num_rows($recordset) > 0) //if record exists
{
while ($curr_record = mysql_fetch_assoc($recordset))
{
$data[] = $curr_record;
}
echo json_encode($data);
}
else
echo "no";
}
function data()
{
$id=$_REQUEST['qid'];
$sql= "SELECT * FROM question WHERE qid='$id' ";
$data = array();
$recordset = mysql_query($sql);
if(mysql_num_rows($recordset) > 0) //if record exists
{
while ($curr_record = mysql_fetch_assoc($recordset))
{
$data[] = $curr_record;
}
echo json_encode($data);
}
else
echo "no";
}
function gets()
{
$id=$_REQUEST['data'];
$s=1;
$sql= "SELECT * FROM status WHERE data='$id' and status='$s'";
$data = array();
$recordset = mysql_query($sql);
if(mysql_num_rows($recordset) > 0) //if record exists
{
while ($curr_record = mysql_fetch_assoc($recordset))
{
$data[] = $curr_record;
}
echo json_encode($data);
}
else
echo "no";
}
}
?>
答案 0 :(得分:1)
你的问题就在这里:
mysql_query($sql);
if(mysql_query($sql))
{
$sql= "SELECT * FROM Question WHERE qid=(Select MAX(qid) from question WHERE data =$data)";
$recordset = mysql_query($sql);
while($row=mysql_fetch_array($recordset))
{
$d= $row['qid'];
}
$sql1="INSERT INTO status(qid,data,status) VALUES ('$d','$data','0')";
mysql_query($sql1);
return true;
}
else
return false;
请记住,if()中的代码正在执行 - 所以你在第一行运行INSERT语句,然后在if(mysql_query($ sql))行上第二次意外运行它。只需删除第一个mysql_query行,你应该没问题(虽然mysql_query本身已经过时,不应该使用)。