动作表单PHP上的调用方法

时间:2017-01-26 05:08:58

标签: php model-view-controller methods action

我正在尝试使用函数创建登录表单,我在不同的文件中使用类和函数。这是我的代码。

登录表格

<?php 
require_once("controller/ED_Setting.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Example Test</title>
</head>

<body>
<?php
if(isset($_REQUEST['failure']))
{
    echo "User/Password Wrong";
}
?>
<form action=""<?php echo "/mvc/controller/login.php";?>"" method="post">
  <label>Email:</label>
  <input type="text" name="username" />
  <br />
  <label>Password:</label>
  <input type="password" name="password" />
  <br />
  <input type="submit" name="cmdlogin" value="Login" />
</form>
</body>
</html>

我登录的功能

<?php
require_once("ED_Setting.php");

class login
{   
    function index()
    {
        $db = new ED_Setting();  
        $db->connect();
        if(isset($_REQUEST['cmdlogin']))
        {
            $rs = $db->select("SELECT * FROM tbl_user where username = '".$_REQUEST['username']."' and password= '".($_REQUEST['password'])."'");
            $res = $db->getResult();
            if($res)
            {
                header('location: http://blup2h.rf.gd');
            }
            else
            {
                header('location: http://localhost/project/index.php?failure');
            }
        }
    }
} 
?>

设置连接就像这样

<?php 
class ED_Setting
{
    private $db_host = "localhost";  // Change as required
    private $db_user = "root";  // Change as required
    private $db_pass = "";  // Change as required
    private $db_name = "db_pembukuan";  // Change as required

    private $con = false; // Check to see if the connection is active
    private $result = array(); // Any results from a query will be stored here


    // Function to make connection to database
    public function connect(){
        if(!$this->con){
            $myconn = @mysqli_connect($this->db_host,$this->db_user,$this->db_pass,$this->db_name);  // mysql_connect() with variables defined at the start of Database class
            if($myconn){
                return true;
            }else{
                array_push($this->result,mysqli_error());
                return false; // Problem connecting return FALSE
            }  
        }else{  
            return true; // Connection has already been made return TRUE 
        }   
    }

    // Function to disconnect from the database
    public function disconnect(){
        // If there is a connection to the database
        if($this->con){
            // We have found a connection, try to close it
            if(@mysql_close()){
                // We have successfully closed the connection, set the connection variable to false
                $this->con = false;
                // Return true tjat we have closed the connection
                return true;
            }else{
                // We could not close the connection, return false
                return false;
            }
        }
    }

    public function select($sql){
        $query = @mysqli_query($sql);
       // $this->myQuery = $sql; // Pass back the SQL
        if($query){
            // If the query returns >= 1 assign the number of rows to numResults
            $this->numResults = mysqli_num_rows($query);
            // Loop through the query results by the number of rows returned
            for($i = 0; $i < $this->numResults; $i++){
                $r = mysqli_fetch_array($query);
                $key = array_keys($r);
                for($x = 0; $x < count($key); $x++){
                    // Sanitizes keys so only alphavalues are allowed
                    if(!is_int($key[$x])){
                        if(mysqli_num_rows($query) >= 1){
                            $this->result[$i][$key[$x]] = $r[$key[$x]];
                        }else{
                            $this->result = null;
                        }
                    }
                }
            }
            return true; // Query was successful
        }else{
            array_push($this->result,mysqli_error());
            return false; // No rows where returned
        }
    }

    // Function to update and delete into the database
    public function query($sql)
    {
            if($query = @mysql_query($sql)){
                array_push($this->result,mysql_affected_rows());
                return true; 
            }else{
                array_push($this->result,mysql_error());
                return false; 
            }

    }

    // Public function to return the data to the user
    public function getResult(){
        $val = $this->result;
        $this->result = array();
        return $val;
    }
}
 ?>

但它确实有效。有什么答案吗?

1 个答案:

答案 0 :(得分:0)

问题可能是""使用表单操作属性。

使用此行

<form action="<?php echo "/mvc/controller/login.php";?>" method="post">

而不是

<form action=""<?php echo "/mvc/controller/login.php";?>"" method="post">