duangang4001 2013-07-25 15:00
浏览 49
已采纳

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

After my recent topic i found out that my register page is not inserting into mysql first things first i know i'm still using mysql but i change it to pdo when evrything is working.

So here's the thing

The following parts are being used to insert it into the database

form

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

The following code is the

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(check for errors in form)

    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(inserting the things)

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

the following error is being recieved

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.

the following code is for that error

    <?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']);
}

the problem is that he does not insert anything into the database

it may be alot of code but i hope its enough info for this

  • 写回答

1条回答 默认 最新

  • dousi2553 2013-07-25 15:08
    关注

    INSERT Query needs quotation marks for varchar in VALUES.

    $q = "INSERT INTO ".TBL_USERS." VALUES ('$username', '$password', '0', '$ulevel', '$email', '$time', '$clan', '$namers')";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学中在线排序的时间在线排序的在线LPT算法
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧