无法使用单页php编码更改表单元素

soat7uwm  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(351)

我是一个新手,我正在尝试使用php和bootstrap制作一个注册表单。我选择了用户的emailaddress作为mysql数据库表user\u accounts中的主键。我想要的是,如果用户的电子邮件是唯一的,它应该将他们的记录添加到数据库中(这工作非常出色),但如果用户输入的电子邮件已经存在于数据库中,它不应该将他们的记录添加到数据库中,把他们带回到同一个页面上,把“formgroup”改成“formgroup has danger”元素(就像在bootstrap中一样),给他们一条消息,让他们更改电子邮件并再次注册。由于email字段是主键,所以它没有在数据库中添加重复电子邮件的记录,但在提交时没有显示“表单组有危险”,因此没有给出错误消息。这是我的密码-

<body>
  <div class="container">     
    <center>
      <form class="signUpForm" method="post">
        <h1 class="signUpH1"><strong>Sign Up!</strong></h1>
        <hr>
        <fieldset>
          <div class="form-group">
            <label for="fullName">Full Name</label>
            <input type="text" class="form-control" name="fullName"  placeholder="Enter name">
          </div>
          <?php
            $_REQUEST[$status];
            if ($status == 'changeEmail') {
              # code...
              echo "<div class='form-group has-danger'>
                      <label for='emailAddress'>Email address</label>
                      <input type='email' class='form-control is-invalid' name='emailAddress' placeholder='Enter email'>
                      <div class='invalid-feedback'>Sorry, an account with that Email already exists! Try another.</div>
                    </div>";
             }
             else {
               # code...
               echo "<div class='form-group'>
                       <label for='emailAddress'>Email address</label>
                       <input type='email' class='form-control' name='emailAddress' aria-describedby='emailHelp' placeholder='Enter email'>
                       <small id='emailHelp' class='form-text text-muted'>We'll never share your email with anyone else.</small>
                      </div>";
              }
            ?>
            <div class="form-group">
              <label for="password">Password</label>
              <input type="password" class="form-control" name="password" placeholder="Enter Password">
            </div>
            <button type="submit" class="btn btn-primary" name="signUp">Submit</button>
        </fieldset>
      </form>
    </center>
  </div>

  <?php 
    if (isset($_REQUEST['signUp'])) {
      $fullName = $_REQUEST['fullName'];
      $emailAddress = $_REQUEST['emailAddress'];
      $password = $_REQUEST['password'];

      $link = mysql_connect("localhost","root","");
      mysql_select_db("practiceDatabase",$link);
      mysql_query("insert into user_accounts values ('".$fullName."','".$emailAddress."','".$password."')");
      $n = mysql_affected_rows();
      if ($n == 0) {
        # code...
        $status = 'changeEmail';
        return $status;
      }

      mysql_close($link);
    }
   ?>
 </body>

非常感谢您的帮助!谢谢!

eulz3vhy

eulz3vhy1#

请注意,您应该采取上述意见,并实现他们,使用pdo。下面是利用pdo重新编写的代码。

<body>
  <div class="container">     
    .....
  </div>

  <?php 
    if (isset($_REQUEST['signUp'])) {
      $fullName = $_REQUEST['fullName'];
      $emailAddress = $_REQUEST['emailAddress'];
      $password = $_REQUEST['password'];

      $link = mysql_connect("localhost","root","");
      mysql_select_db("practiceDatabase",$link);

      // First you should check and see if the email already exists
      $sql = "SELECT * FROM user_accounts WHERE email_address = :email_address";
      $params = array(":email_address"=>$emailAddress);

      $db = $conn->prepare($sql);
      $db->execute($params);

      if($db->rowCount() > 0) {
           // This means that there is already a row with this email, and is an error.  
      } else {
          $sql = "INSERT INTO user_accounts VALUES (....)";
          $params = .....;
          $insertDb = $conn->prepare($sql);
          $insertDb->execute($params);
      }
?>
 </body>

相关问题