嘿,我新的php和我试图使注册和登录表单,但他显示这个代码当我点击提交按钮提交我的用户信息到数据库:
sign-up.php页:
<?php
require('functions/database.php');
session_start();
if(isset($_SESSION['user_id'])) {
header("Location:dashboard.html");
}
session_destroy();
session_start();
ob_start();
?>
<html>
[...]
<head>
[...]
<body>
<form role="form text-left" action="sign_up.php" method="POST">
<div class="mb-3">
<input type="text" class="form-control" placeholder="First Name" name="userfirstname" id="userfirstname">
</div>
<div class="mb-3">
<input type="text" class="form-control" placeholder="Last Name" name="userlastname" id="userlastname">
</div>
<div class="mb-3">
<input type="text" class="form-control" placeholder="Nickname" name="usernickname" id="usernickname">
</div>
<div class="mb-3">
<input type="password" class="form-control" placeholder="Password" name="userpass" id="userpass">
</div>
<div class="mb-3">
<input type="password" class="form-control" placeholder="Confirm Password" name="userpassconfirm" id="userpassconfirm">
</div>
<div class="mb-3">
<input type="email" class="form-control" placeholder="Email" name="useremail" id="useremail">
</div>
<div class="mb-3">
<input type="text" class="form-control" placeholder="Hometown" name="userhometown" id="userhometown">
</div>
<div class="text-center">
<input type="submit" class="btn bg-gradient-dark w-100 my-4 mb-2" name="register"></input>
</div>
<p class="text-sm mt-3 mb-0">Already have an account? <a href="javascript:;" class="text-dark font-weight-bolder">Sign in</a></p>
</form>
</body>
</head>
</html>
sign_up.php页:
<?
session_start();
require_once('functions/database.php');
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['register'])) {
// Retrieve Data
$userfirstname = $_POST['userfirstname'];
$userlastname = $_POST['userlastname'];
$usernickname = $_POST['usernickname'];
$userpassword = md5($_POST['userpass']);
$useremail = $_POST['useremail'];
$userhometown = $_POST['userhometown'];
$query = "SELECT user_email, FROM users WHERE user_email=:email";
$check = $pdo->prepare($query);
$check->bindParam(':email', $useremail, PDO::PARAM_STR);
$check->execute();
$checkquery = $check->fetchAll(PDO::FETCH_ASSOC);
if ($count($checkquery) > 0) {
$msg = "User alredy registered";
} else {
// Insert Data
$query = "INSERT INTO users(user_firstname, user_lastname, user_nickname, user_password, user_email, user_hometown) VALUES (:firstname, :lastname, :nickname, :password, :email, :hometown)";
$check = $pdo->prepare($query);
$check->bindParam(':firstname', $userfirstname, PDO::PARAM_STR);
$check->bindParam(':lastname', $userlastname, PDO::PARAM_STR);
$check->bindParam(':nickname', $usernickname, PDO::PARAM_STR);
$check->bindParam(':password', $userpassword, PDO::PARAM_STR);
$check->bindParam(':email', $useremail, PDO::PARAM_STR);
$check->bindParam(':hometown', $userhometown, PDO::PARAM_STR);
$check->execute();
if ($check->rowCount() > 0) {
$msg = "success!";
} else {
$msg ="error, try again";
}
}
}
}
echo $msg;
?>
提交注册表单时出现错误:
prepare($query); $check->bindParam(':email', $useremail, PDO::PARAM_STR); $check >execute(); $checkquery = $check->fetchAll(PDO::FETCH_ASSOC); if ($count($checkquery) > 0) { $msg = "Utente gia registrato"; } else { // Insert Data $query = "INSERT INTO users(user_firstname, user_lastname, user_nickname, user_password, user_email, user_hometown) VALUES (:firstname, :lastname, :nickname, :password, :email, :hometown)"; $check = $pdo->prepare($query); $check->bindParam(':firstname', $userfirstname, PDO::PARAM_STR); $check->bindParam(':lastname', $userlastname, PDO::PARAM_STR); $check->bindParam(':nickname', $usernickname, PDO::PARAM_STR); $check->bindParam(':password', $userpassword, PDO::PARAM_STR); $check->bindParam(':email', $useremail, PDO::PARAM_STR); $check->bindParam(':hometown', $userhometown, PDO::PARAM_STR); $check->execute(); if ($check->rowCount() > 0) { $msg = "utente registrato"; } else { $msg ="utente non registrato"; } } } } echo $msg; ?>
如果您对语法、注释有任何建议,欢迎提出
请删除","在代码中的user_email之后。
$query = "SELECT user_email FROM users WHERE user_email=:email";
$check = $pdo->prepare($query);
$check->execute(array('email' => $useremail));