我收到的注册表单当前未发送数据或功能未按预期工作。
首页获取了所有必须使用 POST 方法发送到 sign-up.php 的表单,所有用户输入都将被 strip 化,如果用户输入了所有请求表单数据,将被发送到方法 在
。
该类处理 mysql 提交。USER
类中注册
数据库连接按预期工作。
HTML 表单
<form id="signupform" class="form-horizontal" role="form" action="sign-up.php" method="POST">
<div id="signupalert" style="display:none" class="alert alert-danger">
<p>Errore:</p>
</div>
<div class="form-group">
<label for="email" class="col-md-3 control-label">Email</label>
<div class="col-md-9">
<input type="text" class="form-control" name="email" placeholder="Indirizzo e-Mail">
</div>
</div>
<div class="form-group">
<label for="username" class="col-md-3 control-label">Username</label>
<div class="col-md-9">
<input type="text" class="form-control" name="username" placeholder="Username">
</div>
</div>
[ ... Striped Code ...]
<div class="col-md-offset-3 col-md-9">
<input type="submit" name="submit" id="submit" type="button" class="btn btn-info" value="Registrati"/>
<button id="btn-fbsignup" type="button" class="btn btn-primary"><i class="glyphicon glyphicon-facebook"></i>Registrati con Facebook</button>
</div>
</form>
sign-up.php
<?php
session_start();
require_once('core/class.user.php');
$user = new USER();
var_dump($_POST);
if($user->is_loggedin()!="")
{
$user->redirect('home.php');
}
if(isset($_POST['submit']))
{
$uname = strip_tags($_POST['username']);
$umail = strip_tags($_POST['email']);
$upass = strip_tags($_POST['password']);
$firstname = strip_tags($_POST['firstname']);
$lastname = strip_tags($_POST['lastname']);
$address = strip_tags($_POST['address']);
$cap = strip_tags($_POST['cap']);
$city = strip_tags($_POST['city']);
$prov = strip_tags($_POST['prov']);
$tel = strip_tags($_POST['tel']);
if($uname=="") {
$error[] = "provide username !";
}
else if($umail=="") {
$error[] = "provide email id !";
}
else if(!filter_var($umail, FILTER_VALIDATE_EMAIL)) {
$error[] = 'Please enter a valid email address !';
}
else if($upass=="") {
$error[] = "provide password !";
}
else if(strlen($upass) < 6){
$error[] = "Password must be atleast 6 characters";
}
else
{
try
{
$stmt = $user->runQuery("SELECT user_name, user_email FROM users WHERE user_name=:uname OR user_email=:umail");
$stmt->execute(array(':uname'=>$uname, ':umail'=>$umail));
$row=$stmt->fetch(PDO::FETCH_ASSOC);
if($row['user_name']==$uname) {
$error[] = "sorry username already taken !";
}
else if($row['user_email']==$umail) {
$error[] = "sorry email id already taken !";
}
else
{
if($user->register($uname,$umail,$upass,$firstname,$lastname,$address,$cap,$city,$prov,$tel)){
$user->redirect('sign-up.php?joined');
}
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
}
if ($_GET['joined']) {
print 'Registrazione completata';
}
?>
class.user.php
class USER
{
private $conn;
public function __construct()
{
$database = new Database();
$db = $database->dbConnection();
$this->conn = $db;
}
public function runQuery($sql)
{
$stmt = $this->conn->prepare($sql);
return $stmt;
}
public function register($uname, $umail, $upass, $firstname, $lastname, $address, $cap, $city, $prov, $tel)
{
try {
$new_password = password_hash($upass, PASSWORD_DEFAULT);
$stmt = $this->conn->prepare("INSERT INTO users(user_name,user_email,user_pass,disk_quota, details_nome, details_cognome, details_indirizzo, details_cap, details_citta, details_provincia, details_telefono)
VALUES(:uname, :umail, :upass, '209715200', :firstname, :lastname, :address, :cap, :city, :prov, :tel)");
$stmt->bindparam(":uname", $uname);
$stmt->bindparam(":umail", $umail);
$stmt->bindparam(":upass", $new_password);
$stmt->bindparam(":firstname", $firstname);
$stmt->bindparam(":lastname", $lastname);
$stmt->bindparam(":address", $address);
$stmt->bindparam(":cap", $cap);
$stmt->bindparam(":city", $city);
$stmt->bindparam(":prov", $prov);
$stmt->bindparam(":tel", $tel);
$stmt->execute();
return $stmt;
} catch (PDOException $e) {
echo $e->getMessage();
}
}
[... striped rest of class code ...]
public function is_loggedin()
{
if(isset($_SESSION['user_session']))
{
return true;
}
}
}
[... striped rest of class code ...]
dbconfig.php
<?php
class Database
{
private $host = "localhost";
private $db_name = "c9";
private $username = "andreaem_dev";
private $password = "";
public $conn;
public function dbConnection()
{
$this->conn = null;
try
{
$this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name . ";charset=utf8", $this->username, $this->password);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $exception)
{
echo "Connection error: " . $exception->getMessage();
}
return $this->conn;
}
}
MySQL日志
MySQL日志仅返回与数据库的初始连接
170314 4:09:26 163 Connect andreaem_dev@localhost on c9
163 Quit
POST数据的var_dump()
/home/ubuntu/workspace/auth/sign-up.php:5: array(12) { 'email' => string(19) "johndoe@example.com" 'username' => string(7) "johndoe" 'firstname' => string(4) "John" 'lastname' => string(3) "Doe" 'address' => string(15) "Fantasy Road,14" 'cap' => string(6) "010101" 'city' => string(4) "Roma" 'prov' => string(4) "Roma" 'tel' => string(13) "+395551234567" 'passwd' => string(8) "jdoe1234" 'repasswd' => string(8) "jdoe1234" 'submit' => string(10) "Registrati" }
行为
当表单提交到 sign_up.php
时,除了 var_dump()
打印输出之外,我得到了一个空白页。数据库中没有插入任何内容,也没有显示错误
我希望我已经了解了问题所在。
最佳答案
感谢 CodeGodie 的帮助,我发现问题出在位于 sign_up.php
页面的 IF block 中,删除此代码后代码将按预期工作并创建用户。
我将使用 jQuery 来管理它,它也更加用户友好。 谢谢
关于PHP:注册表不发送到 mySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42777821/