只需将数据插入数据库

时间:2017-04-22 18:48:22

标签: php

我对php很新,并且一直在尝试使用它创建一个网站。我创建了一个登录页面,并尝试通过插入用户名和提供创建新用户的选项。传入DB。我觉得这很简单,我很遗憾,但我不确定,所以希望有人可以提供帮助:)

php~

<?php
session_start();

unset($_SESSION["currentUser"]);
unset($_SESSION["currentUserID"]);

if (isset($_POST["action"]) && $_POST["action"]=="create") {

  $formUserC=$_POST["name"];
  $formPassC=$_POST["pword"];

  include("dbConnect.php");
  $dbQuery=$db->prepare("select * from users"); 
  $newAcc = "INSERT INTO users (username, password) VALUES ('$formUserC', '$formPassC')";
  if($newAcc){
      echo "<p>Your account was successfully created.";
  }  
  else{
   echo "<p>Error</p>";
  }
 }

if (isset($_POST["action"]) && $_POST["action"]=="login") {

  $formUser=$_POST["username"];
  $formPass=$_POST["password"];

  include("dbConnect.php");
  $dbQuery=$db->prepare("select * from users where username=:formUser"); 
  $dbParams = array('formUser'=>$formUser);
  $dbQuery->execute($dbParams);
  $dbRow=$dbQuery->fetch(PDO::FETCH_ASSOC);
  if ($dbRow["username"]==$formUser) {       
     if ($dbRow["password"]==$formPass) {
        $_SESSION["currentUser"]=$formUser;
        $_SESSION["currentUserID"]=$dbRow["id"];
        if (isset($_SESSION["homePage"])) 
             header("Location: addToBasket.php");
        else header("Location: homePage.html");    
     }
     else {
        header("Location: login.php?failCode=2");
     }
  } else {
        header("Location: login.php?failCode=1");
  }

 } else {

?>

html(主要是)〜

    <html >
    <head>
   <meta charset="UTF-8">
   <title>Flat HTML5/CSS3 Login Form</title>



      <link rel="stylesheet" href="css/loginstyle.css">


</head>

<body>


  <div class="login-page">
  <div class="form">
    <form class="register-form" name="create" method="post" action="login.php">
      <input type="text" name="name" placeholder="name"/>
      <input type="password" name="pword" placeholder="pword"/>
      <input type="hidden" name="action" value="create">
      <input type="submit" value="create"/>
      <p class="message">Already registered? <a href="#">Sign In</a></p>
    </form>
    <form name="login" method="post" action="login.php">
      <input type="text" name="username" placeholder="username"/>
      <input type="password" name="password" placeholder="password"/>
      <input type="hidden" name="action" value="login">
      <input type="submit" value="login"/>
      <p class="message">Not registered? <a href="#">Create an account</a></p>
    </form>
    <?php
   if (isset($_GET["failCode"])) {
      if ($_GET["failCode"]==1)
         echo "<h3 style='color:red;'>Invalid username entered</h3>";
      if ($_GET["failCode"]==2)
         echo "<h3 style='color:red;'>Invalid password entered</h3>";
   }     
?>
  </div>
</div>
  <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>

   <script src="js/loginindex.js"></script>



</body>
</html>

<?php
    }
?>

P.S。我抓住了其中一些代码。提前谢谢。

0 个答案:

没有答案