表单函数注册不插入数据库

时间:2013-07-25 15:00:44

标签: php html mysql forms function

在我最近的主题之后,我发现我的注册页面没有插入到mysql中 首先我知道我还在使用mysql但是当evrything工作时我将它改为pdo。

所以这就是事情

以下部分用于将其插入数据库

形式

    <div id="registerform">
    <form action="process.php" method="POST">
    <ul>
        <li>Username :
        <br />
        <?php echo $form->error("user"); ?></li>
        <li><input type="text" name="user" maxlength="30" value="<?php echo $form->value("user"); ?>"></li>
        <li>Password :
        <br />
        <?php echo $form->error("pass"); ?></li>
        <li><input type="password" name="pass" maxlength="30" value="<?php echo $form->value("pass"); ?>"></li>
        <li>Email :
        <br />
        <?php echo $form->error("email"); ?></li>
        <li><input type="text" name="email" maxlength="50" value="<?php echo $form->value("email"); ?>"></li>
        <li>First Name :<br />
        <?php echo $form->error("clan"); ?></li>
        <li><input type="text" name="clan" maxlength="30" value="<?php echo $form->value("clan"); ?>" /></li>
        <li>Last Name :<br />
        <?php echo $form->error("namers"); ?></li>
        <li><input type="text" name="namers" maxlength="30" value="<?php echo $form->value("namers"); ?>" /></li>   
        <li><input type="hidden" name="subjoin" value="1"><input type="submit" value="Register!"></li>

    </ul>
    </form>
</div>

以下代码是

process.php

   function procRegister(){
  global $session, $form;
  /* Convert username to all lowercase (by option) */
  if(ALL_LOWERCASE){
     $_POST['user'] = strtolower($_POST['user']);
  }
  /* Registration attempt */
  $retval = $session->register($_POST['user'], $_POST['pass'], $_POST['email'], $_POST['clan'], $_POST['namers']);

  /* Registration Successful */
  if($retval == 0){
     $_SESSION['reguname'] = $_POST['user'];
     $_SESSION['regsuccess'] = true;
     header("Location: ".$session->referrer);
  }
  /* Error found with form */
  else if($retval == 1){
     $_SESSION['value_array'] = $_POST;
     $_SESSION['error_array'] = $form->getErrorArray();
     header("Location: ".$session->referrer);
  }
  /* Registration attempt failed */
  else if($retval == 2){
     $_SESSION['reguname'] = $_POST['user'];
     $_SESSION['regsuccess'] = false;
     header("Location: ".$session->referrer);
  }
   }

session.php (检查表单中的错误)

    function register($subuser, $subpass, $subemail, $subclan, $subnamers){
      global $database, $form, $mailer;  //The database, form and mailer object


          /* Username error checking */
          $field = "user";  //Use field name for username
          if(!$subuser || strlen($subuser = trim($subuser)) == 0){
             $form->setError($field, "* Username not entered");
      }
      else{
         /* Spruce up username, check length */
         $subuser = stripslashes($subuser);
         if(strlen($subuser) < 5){
            $form->setError($field, "* Username below 5 characters");
         }
         else if(strlen($subuser) > 30){
            $form->setError($field, "* Username above 30 characters");
         }
         /* Check if username is not alphanumeric */
         else if(!ctype_alnum($subuser)){
            $form->setError($field, "* Username not alphanumeric");
         }
         /* Check if username is reserved */
         else if(strcasecmp($subuser, GUEST_NAME) == 0){
            $form->setError($field, "* Username reserved word");
         }
         /* Check if username is already in use */
         else if($database->usernameTaken($subuser)){
            $form->setError($field, "* Username already in use");
         }
         /* Check if username is banned */
         else if($database->usernameBanned($subuser)){
            $form->setError($field, "* Username banned");
         }
      }

      /* Password error checking */
      $field = "pass";  //Use field name for password
      if(!$subpass){
         $form->setError($field, "* Password not entered");
      }
      else{
         /* Spruce up password and check length*/
         $subpass = stripslashes($subpass);
         if(strlen($subpass) < 4){
            $form->setError($field, "* Password too short");
         }
         /* Check if password is not alphanumeric */
         else if(!preg_match("/^([0-9a-z])+$/", ($subpass = trim($subpass)))){
            $form->setError($field, "* Password not alphanumeric");
         }
         /**
          * Note: I trimmed the password only after I checked the length
          * because if you fill the password field up with spaces
          * it looks like a lot more characters than 4, so it looks
          * kind of stupid to report "password too short".
          */
      }

      /* Email error checking */
      $field = "email";  //Use field name for email
      if(!$subemail || strlen($subemail = trim($subemail)) == 0){
         $form->setError($field, "* Email not entered");
      }
      else{
         /* Check if valid email address */
         if(!filter_var($subemail, FILTER_VALIDATE_EMAIL)){
            $form->setError($field, "* Email invalid");
         }
         $subemail = stripslashes($subemail);
      }
      /* First Name error checking */
      $field = "clan";      //Use field name for clan name
      if(!$subclan || strlen($subclan = trim($subclan)) == 0){
          $form->setError($field, "* Clan not entered");
      }

      /* Last Name error checking */
      $field = "namers";        //Use field name for Runescape name
      if(!$subnamers|| strlen($subnamers = trim($subnamers)) == 0){
          $form->setError($field, "* Runescape name not entered");
      }  
      /* Errors exist, have user correct them */
      if($form->num_errors > 0){
         return 1;  //Errors with form
      }
      /* No errors, add the new account to the database*/
      else{
         if($database->addNewUser($subuser, md5($subpass), $subemail, $subclan, $subnamers)){
            if(EMAIL_WELCOME){
               $mailer->sendWelcome($subuser, $subemail, $subpass, $subclan, $subnamers);
            }
            return 0;  //New user added succesfully
         }else{
            return 2;  //Registration attempt failed
         }
      }


    }

database.php (插入内容)

       function addNewUser($username, $password, $email, $clan, $namers){
      $time = time();
      /* If admin sign up, give admin user level */
      if(strcasecmp($username, ADMIN_NAME) == 0){
         $ulevel = ADMIN_LEVEL;
      }else{
         $ulevel = USER_LEVEL;
      }
      $q = "INSERT INTO ".TBL_USERS." VALUES ($username, $password, '0', $ulevel, $email, $time, $clan, $namers)";
      return mysql_query($q, $this->connection);
   }

收到以下错误

We're sorry, but an error has occurred and your registration for the username (name), could not be completed.
Please try again at a later time.

以下代码用于该错误

    <?php
/**
 * The user is already logged in, not allowed to register.
 */
if($session->logged_in){
   echo "<h1>Registered</h1>";
   echo "<p>We're sorry <b>$session->username</b>, but you've already registered. "
       ."<a href=\"main.php\">Main</a>.</p>";
}
/**
 * The user has submitted the registration form and the
 * results have been processed.
 */
else if(isset($_SESSION['regsuccess'])){
   /* Registration was successful */
   if($_SESSION['regsuccess']){
      echo "<h1>Registered!</h1>";
      echo "<p>Thank you <b>".$_SESSION['reguname']."</b>, your information has been added to the database, "
          ."you may now <a href=\"main.php\">log in</a>.</p>";
   }
   /* Registration failed */
   else{
      echo "<h1>Registration Failed</h1>";
      echo "<p>We're sorry, but an error has occurred and your registration for the username <b>".$_SESSION['reguname']."</b>, "
          ."could not be completed.<br>Please try again at a later time.</p>";
          print_r($_SESSION);


   }
   unset($_SESSION['regsuccess']);
   unset($_SESSION['reguname']);
}

问题是他没有在数据库中插入任何东西

它可能有很多代码,但我希望它足够的信息

1 个答案:

答案 0 :(得分:7)

INSERT Query需要VALUES中varchar的引号。

$q = "INSERT INTO ".TBL_USERS." VALUES ('$username', '$password', '0', '$ulevel', '$email', '$time', '$clan', '$namers')";