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

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

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

  1. <body>
  2. <div class="container">
  3. <center>
  4. <form class="signUpForm" method="post">
  5. <h1 class="signUpH1"><strong>Sign Up!</strong></h1>
  6. <hr>
  7. <fieldset>
  8. <div class="form-group">
  9. <label for="fullName">Full Name</label>
  10. <input type="text" class="form-control" name="fullName" placeholder="Enter name">
  11. </div>
  12. <?php
  13. $_REQUEST[$status];
  14. if ($status == 'changeEmail') {
  15. # code...
  16. echo "<div class='form-group has-danger'>
  17. <label for='emailAddress'>Email address</label>
  18. <input type='email' class='form-control is-invalid' name='emailAddress' placeholder='Enter email'>
  19. <div class='invalid-feedback'>Sorry, an account with that Email already exists! Try another.</div>
  20. </div>";
  21. }
  22. else {
  23. # code...
  24. echo "<div class='form-group'>
  25. <label for='emailAddress'>Email address</label>
  26. <input type='email' class='form-control' name='emailAddress' aria-describedby='emailHelp' placeholder='Enter email'>
  27. <small id='emailHelp' class='form-text text-muted'>We'll never share your email with anyone else.</small>
  28. </div>";
  29. }
  30. ?>
  31. <div class="form-group">
  32. <label for="password">Password</label>
  33. <input type="password" class="form-control" name="password" placeholder="Enter Password">
  34. </div>
  35. <button type="submit" class="btn btn-primary" name="signUp">Submit</button>
  36. </fieldset>
  37. </form>
  38. </center>
  39. </div>
  40. <?php
  41. if (isset($_REQUEST['signUp'])) {
  42. $fullName = $_REQUEST['fullName'];
  43. $emailAddress = $_REQUEST['emailAddress'];
  44. $password = $_REQUEST['password'];
  45. $link = mysql_connect("localhost","root","");
  46. mysql_select_db("practiceDatabase",$link);
  47. mysql_query("insert into user_accounts values ('".$fullName."','".$emailAddress."','".$password."')");
  48. $n = mysql_affected_rows();
  49. if ($n == 0) {
  50. # code...
  51. $status = 'changeEmail';
  52. return $status;
  53. }
  54. mysql_close($link);
  55. }
  56. ?>
  57. </body>

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

eulz3vhy

eulz3vhy1#

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

  1. <body>
  2. <div class="container">
  3. .....
  4. </div>
  5. <?php
  6. if (isset($_REQUEST['signUp'])) {
  7. $fullName = $_REQUEST['fullName'];
  8. $emailAddress = $_REQUEST['emailAddress'];
  9. $password = $_REQUEST['password'];
  10. $link = mysql_connect("localhost","root","");
  11. mysql_select_db("practiceDatabase",$link);
  12. // First you should check and see if the email already exists
  13. $sql = "SELECT * FROM user_accounts WHERE email_address = :email_address";
  14. $params = array(":email_address"=>$emailAddress);
  15. $db = $conn->prepare($sql);
  16. $db->execute($params);
  17. if($db->rowCount() > 0) {
  18. // This means that there is already a row with this email, and is an error.
  19. } else {
  20. $sql = "INSERT INTO user_accounts VALUES (....)";
  21. $params = .....;
  22. $insertDb = $conn->prepare($sql);
  23. $insertDb->execute($params);
  24. }
  25. ?>
  26. </body>
展开查看全部

相关问题