dongluo8303 2017-04-22 19:36
浏览 72
已采纳

MySQL和PHP解析错误

My program is suppose to store the e-mail of a user and allow them to login in if it's a new e-mail or if the e-mail has already been used to login, the program is to alert the user that the e-mail has already been logged in. My issue is the e-mail does not seem to be stored in the table in order to validate if it's been used before. Every time I log in with the same credentials, I get the congratulations statement instead of the Error statement. Does anyone see where I am incorrect? Perhaps my syntax (I've been starring at this for hours and I can not seem to pick out the issue). Thanks in advanced! Any input is appreciated!

dbparams.txt

username=sdev_owner
password=sdev300
host=localhost
db=sdev

Disc_7.php

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Discussion 7 </title>
</head>
<body OnLoad="document.createlogin.uname.focus();">
  <?php     
    if(isset($_POST["CreateSubmit"])) {                     
        validate_form();             
    } else {                
        $messages = array();
    show_form($messages);  
    } 
?>

  <?php
function show_form($messages) {         
    // Assign post values if exist
    $uname="";
    $email="";
    if (isset($_POST["uname"])) 
          $uname=$_POST["uname"];

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


    echo "<p></p>";
    echo "<h2> Enter Login Information</h2>";
    echo "<p></p>";     
?>
    <form name="createlogin" method="POST" action="Disc_7.php">
      <table border="1" width="100%" cellpadding="0">
        <tr>
          <td width="157">Username:</td>
          <td><input type="text" name="uname" value='<?php echo $uname ?>' size="30"></td>
        </tr>
        <tr>
          <td width="157">Email:</td>
          <td><input type="text" name="email" value='<?php echo $email ?>' size="30"></td>
        </tr>
        <tr>
          <td width="157"><input type="submit" value="Submit" name="CreateSubmit"></td>
          <td>&nbsp;</td>
        </tr>
      </table>
    </form>

    <?php
} 
?>
      <?php
function validate_form(){

$messages = array();
$redisplay = false;
$uname = $_POST["uname"];
$email = $_POST["email"];

  $login = new LoginClass($uname,$email);   
  $count = countLogin($login);   

    if ($count==0) {    
        $res = insertLogin($login); 
        echo "<h3>Congratulations, you've logged in!</h3> ";         
    } else {
        echo "<h3>Error: A login with that e-mail has already logged in.</h3> ";        
    }   
}

function countLogin ($login){        

$mysqli = connectdb();
$uname = $login->getUname();
$email = $login->getEmail();

$mysqli = connectdb();

$Myquery = "SELECT count(*) as count from LoginData where eMail='$email'";   

if ($result = $mysqli->query($Myquery)) {
    while( $row = $result->fetch_assoc()) {
        $count=$row["count"];                                             
    }    
    $result->close();         
}
    $mysqli->close();   

    return $count;      
}

function insertLogin ($login){

$mysqli = connectdb();

$uname = $login->getUname();
$email = $login->getEmail();

$Query = "INSERT INTO LoginData (userName,eMail) VALUES ('$uname', '$email')";

    $Success=false;           
    if ($result = $mysqli->query($Query)) { 
      $Success=true;
    }
    $mysqli->close();

        return $Success;
    }

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="";

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

          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;      
        }    

    } // End DBparms class

 // Class to construct Students with getters/setter
class LoginClass
{
    // property declaration
    private $uname="";
    private $email="";

    // Constructor
    public function __construct($uname,$email)
    {
      $this->uname = $uname;
      $this->email = $email;     
    }

    // Get methods 
      public function getUname ()
    {
        return $this->uname;
    } 
      public function getEmail ()
    {
        return $this->email;
    } 

    // Set methods 
    public function setUname ($value)
    {
        $this->uname = $value;      
    }
    public function setEmail ($value)
    {
        $this->email = $value;      
    }

} 
?>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • dove2199 2017-04-23 04:54
    关注

    I figured out my issue, I had to log into MySQL and create my table there. I was unsure where to create my table at and when I tried to add it into my program, it was not being recognized.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器