gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 17:44:05 24 4
gpt4 key购买 nike

我是一个新手,我正在尝试使用 php 和 bootstrap 制作注册表单。我选择了用户的“emailAddress”作为名为“user_accounts”的 mysql 数据库表中的主键。我想要的是,如果用户的电子邮件是唯一的,它应该将他们的记录添加到数据库中(这工作得非常棒),但是如果用户输入数据库中已经存在的电子邮件,它不应该将他们的记录添加到数据库中,将他们带回同一页面,并将“form-group”更改为“form-group has-danger”元素(如 Bootstrap 中所示),向他们提供更改电子邮件并再次注册的消息。它不会在数据库中添加重复电子邮件的记录,因为电子邮件字段是主键,但提交时不会显示“表单组有危险”,因此不会给出错误消息。这是我的代码 -

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

非常感谢任何帮助!谢谢!

最佳答案

请注意,您应该使用 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>

关于php - 无法使用单页 PHP 编码更改表单元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49883336/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com