PHP:使用其他文件中的函数

时间:2015-12-02 16:30:26

标签: php mysqli

所以我试图创建一个包含访问我的数据库的函数的文件,它似乎不起作用。它连接到我的数据库我想但是不会通过插入查询来完成,这对我来说都是新的,所以我很丢失。这是我的两个文件,其中大部分的PHP是。我有另一个带有表单和CreateSubmit按钮,它使用POST传递值。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Authorizing Login</title>
</head>
<body>
<?php
session_start();



if(isset($_POST["CreateSubmit"]))
{
   validate_form();
}
else
{
    $messages = array();
    show_form($messages);
}

?>
<?php
require_once('db.php');
//$db = new db();
function validate_form()
{
    $messages = array();
    $redisplay = false;
    $username = $_POST["username"];
    $email = $_POST["email"];
    $password = $_POST["password"];
    $user = new UserClass($username,$email,$password);
    $count = countUser($user);
    if($count==0)
    {
        $res = insertUser($user);
        echo "Welcome $username";
        header("Location:products.php");
    }
    else
    {
        echo "Username already exists";
    }
}
function countUser($user)
{
    $mysqli = connectdb();
    $username = $user->getUsername();
    $email = $user->getEmail();
    $password = $user->getPassword();
    $mysqli = $db->connectdb();
    $Myquery = "SELECT count(*) as count from Users where username =        '$username";
    if($result = $mysqli->query($Myquery))
    {
        while($row = $result->fetch_assoc())
        {
            $count = $row["count"];
        }
        $result->close();
    }
    $mysqli->close();
    return $count;
}
function insertUser($user)
{
    $mysqli = connectdb();
    $username = $user->getUsername();
    $email = $user->getEmail();
    $password = $user->getPassword();
    $Query ="INSERT INTO Users (username, email, password) VALUES
                    ('$username', '$email', '$password')";
    $success=false;
    if($result = $mysqli->query($Query))
    {
        $success=true;
        echo "Success";
    }
    $mysqli->close();
    return $success;
}
class UserClass
{
    private $username="";
    private $email="";
    private $password="";
    public function __construct($username, $email, $password)
    {
        $this->username = $username;
        $this->email = $email;
        $this->password = $password;
    }
    //Getters
    public function getUsername()
    {
        return $this->username;
    }
    public function getEmail()
    {
        return $this->email;
    }
    public function getPassword()
    {
        return $this->password;
    }
    //Setters
    public function setUsername($value)
    {
        $this->username = $value;
    }
    public function setEmail($value)
    {
        $this->email = $value;
    }
    public function setPassword($value)
    {
        $this->password = $value;
    }
}
?>
<?php 
function show_form($messages)
{
    $username ="";
    $email = "";
    $password = "";

    if(isset($_POST["username"]))
        $username = $_POST["username"];
    if(isset($_POST["email"]))
        $email = $_POST["email"];
    if(isset($_POST["password"]))
        $password = $_POST["password"];

    $_SESSION['appusername'] = $username;
    $_SESSION['appemail'] = $email;
    $_SESSION['apppassword'] = $password;
}

/*
$timeout = 1800;
if(isset($_SESSION['timeout']))
{
$duration = time() - (int)$_SESSION['timeout'];
if($duration > $timeout)
{
session_destroy();
session_start();
}    

}
if(isset($_SESSION["username"])) 
{
header("Location:products.php");
}
 */
?>


</body>
</html>

这是db.php文件     

function getDbParms()
{

    $trimmed = file('parms/dbparms.txt', FILE_IGNORE_NEW_LINES |    FILE_SKIP_EMPTY_LINES);
    $key = array();
    $vals = array();
    foreach($trimmed as $line)
    {
        $pairs = explode("=",$line);
        $key[]=$pairs[0];
        $vals[]=$pairs[1];
    }

    $mypairs = array_combine($key,$vals);

    $myDbparms = new DBparmsClass($mypairs['username'],$mypairs['password'],
                    $mypairs['host'],$mypairs['db']); 
    return $myDbparms;     
}

function connectdb()
{
    $mydbparms = getDbParms();
    $mysqli = new mysqli($mydbparms->getHost,$mydbparms->getUsername(),
                    $mydbparms->getPassword(),$mydbparms->getDb()); 

    if($mysqli->connect_error)
    {
        die('Connect Error (' . $mysqli->connect_errno . ')' . $mysqli-   >connect_error);
    }

    return $mysqli;

}

class DBparmsClass
{
    private $username="";
    private $password="";      
    private $host="";      
    private $db="";

    // Constructor      
    public function __construct($myusername,$mypassword,$myhost,$mydb)      
    {       
        $this->username = $myusername;        
        $this->password = $mypassword;     
        $this->host = $myhost;    
        $this->db = $mydb;     
    } 

    // Get methods     
    public function getUsername ()      
    {       
        return $this->username;      
    }  

    public function getPassword ()      
    {       
        return $this->password;      
    }      
    public function getHost ()      
    {       
        return $this->host;      
    }      
    public function getDb ()      
    {       
        return $this->db;      
    }    

    // Set methods       
    public function setUsername ($myusername)      
    {       
        $this->username = $myusername;           
    }      
    public function setPassword ($mypassword)      
    {       
        $this->password = $mypassword;           
    }      
    public function setHost ($myhost)      
    {       
        $this->host = $myhost;           
    }      
    public function setDb ($mydb)      
    {       
        $this->db = $mydb;          
    }            
}

0 个答案:

没有答案