我一直在关注一个教程,其中包括将数据插入数据库,然后发送电子邮件以确认该帐户确实由电子邮件的同一所有者拥有。这是我正在关注的视频。
https://www.youtube.com/watch?v=LXQfEFEfFcM&t=62s
html:
数据库:
代码:
<?php
$error = NULL;
if (isset($_POST['submit'])) {
/* Get Form data */
$e = $_POST['e'];
$u = $_POST['u'];
$p = $_POST['p'];
$p2 = $_POST['p2'];
$fn = $_POST['fn'];
$mn = $_POST['mn'];
$ln = $_POST['ln'];
$dob = $_POST['dob'];
if(strlen($u) < 5) {
/* Username too short */
$error = '<p style="color: red;">Username must be at least 5 characters</p>';
} else if($p2 != $p) {
/* Passwords dont match */
$error = '<p style="color: red;">Passwords do not match</p>';
} else {
/* Form is valid -> Connect to the database */
$mysqli = NEW MySQLi('localhost', 'root', '', 'test');
/* Sanitize form data (prevent any sql injection) */
$e = $mysqli->real_escape_string($e);
$u = $mysqli->real_escape_string($u);
$p = $mysqli->real_escape_string($p);
$p2 = $mysqli->real_escape_string($p2);
$fn = $mysqli->real_escape_string($fn);
$mn = $mysqli->real_escape_string($mn);
$ln = $mysqli->real_escape_string($ln);
$dob = $mysqli->real_escape_string($dob);
/* Generate Verification Key + hash password */
$vkey = md5(time().$u);
$hashedp = password_hash($p, PASSWORD_DEFAULT);
/* Insert into database */
$insert = $mysqli->query("INSERT INTO accounts(email, username, password2, first_name, middle_name, last_name, DOB, vkey) VALUES('$e', '$u', '$hashedp', '$fn', '$mn', '$ln', $dob, '$vkey')");
$error = 'Sql statement executed';
$mysqli->error;
/* Check if data was inserted */
if($insert) {
$error = 'Data Inserted';
$mysqli->error;
/* Send email */
$to = $e;
$subject = "Email Verification";
$message = "<a href='http://yieldsync.com/verify.php?vkey=$vKey'>Register Account</a>";
$headers = "From: The Sender Name <[email protected]>\r\n";
$headers .= "MIME-Version: 1.0"."\r\n";
$headers .= "Reply-To: [email protected]\r\n";
$headers .= "Content-type: text/html\r\n";
mail($to, $subject, $message, $headers);
header('location: thankyou.php');
} else {
$error = 'Data not inserted';
$mysqli->error;
}
}
}
?>
<!------------------------------------------------------------------------------------------------------------------->
<?php include 'global-items/header.php'; ?>
<div id="sign-up" class="loginTerminalContainer">
<p class="loginTitle">Sign Up</p>
<?php echo $error; ?>
<Form action="" method="post" style="width: 100%;">
<input name="fn" class="loginTextBox" type="text" placeholder="First Name">
<input name="mn" class="loginTextBox" type="text" placeholder="Middle (if none -> blank)">
<input name="ln" class="loginTextBox" type="text" placeholder="Last Name">
<input name="dob" class="loginTextBox" type="date" placeholder="Date of Birth">
<input name="e" class="loginTextBox" type="email" placeholder="E-mail" autocomplete="off">
<input name="u" class="loginTextBox" type="text" placeholder="Username" autocomplete="off">
<input name="p" class="loginTextBox" type="password" placeholder="Password">
<input name="p2" class="loginTextBox" type="password" placeholder="Repeat Password">
<button name="submit" value="Register" type="submit" class="btn btn-primary loginButton">Register</button>
</form>
</div>
<?php include 'global-items/footer.php'; ?>
我得到的错误是“数据未插入”mysqli 错误:“第 1 行‘password2’列的数据太长”
**Error:** "Data too long for column 'password2' at row 1"
改变表长度!
ALTER TABLE
accounts
CHANGEpassword2
password2
VARCHAR(80);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句