我正在使用PDO做一些基本的登錄註冊表單,但無法在數據庫中插入數據時我var_dump()$查詢,我看到沒有值將不會執行在$ query->執行語句。看下面什麼時發現我var_dump($查詢);PDO準備聲明不能用數據替換佔位符
**
對象(PDOStatement對象)#4(1){[ 「的queryString」] =>串(92)「INSERT INTO 用戶(用戶名,密碼,名稱,電子郵件,網站)VALUES (?,?,?,?,?)」}這裏登錄
**
註冊successfull.Click我遇到過這個問題,並嘗試幾乎所有的東西,我知道。請參閱代碼幫助我找到問題。
數據庫格式像 數據庫名稱:oopreg 表名:用戶
id int(11) primary auto_increment
username varchar(30)
password varchar(30)
name varchar(50)
email varchar(100)
website varchar(100)
的config.php
<?php
class databaseConnection{
public function __construct(){
GLOBAL $pdo;
try{
$pdo = new PDO('mysql:host:localhost;dbname=oopreg','root','');
echo "Connected";
}catch(PDOException $e){
echo "DataBase connection Error";
}
}
}
的functions.php
<?php
require 'config.php';
class loginRegistration{
function __construct(){
$database = new databaseConnection();
}
public function registerUser($username,$password,$name,$email,$website){
global $pdo;
$query= $pdo->prepare("SELECT id FROM users WHERE username=? AND email=?");
$query->execute(array($username,$email));
$num= $query->rowCount();
if($num==0){
$query= $pdo->prepare("INSERT INTO users(username,password,name,email,website) VALUES
(?,?,?,?,?)");
$query->execute(array($username,$password,$name,$email,$website));
var_dump($query);
return true;
}else{
print "<span style='color:red'>Error...Username/Email alreay exists</span>";
}
}
}
?>
register.php
<?php
require_once "functions.php";
$user= new loginRegistration();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Registration Page</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="wrapper">
<div class="header">
<h3>PHP OOP Login Register System</h3>
</div><!--End header Section-->
<div class="mainmenu">
<ul>
<li><a href="index.php">Home</a></li>
<li><a href="profile.php">Show Profile</a></li>
<li><a href="changePassword.php">Change Password</a></li>
<li><a href="logout.php">Logout</a></li>
<li><a href="login.php">Login</a></li>
<li><a href="register.php">Register</a></li>
</ul>
</div><!--End Manin menu section-->
<div class="content">
<h2>Register</h2>
<p class="msg">
<!--PHP area start-->
<?php
if ($_SERVER['REQUEST_METHOD']=='POST') {
$username=$_POST['username'];
$password=$_POST['password'];
$name=$_POST['name'];
$email=$_POST['email'];
$website=$_POST['website'];
if(empty($username) or empty($password) or empty($name) or empty($email) or empty($website)) {
echo "<span style='color:red;'> Field must not be empty</span>";
}else{
$password=md5($password);
$register=$user->registerUser($username,$password,$name,$email,$website);
if($register){
echo "<span style='color:green'>Registration successfull.<a href='login.php'>Click here</a> for login</span>";
}else{
echo "<span style='color:red'>UserName or email already exists</span>";
}
}
}
?>
</p>
<div class="login_reg">
<form action="" method="post">
<table>
<tr>
<td>Username:</td>
<td><input type="text" name="username" placeholder="Enter your username..."/></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password" placeholder="Enter your password..."/></td>
</tr>
<tr>
<td>Name:</td>
<td><input type="text" name="name" placeholder="Enter your name..."/></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="email" name="email" placeholder="Enter your email..."/></td>
</tr>
<tr>
<td>Website:</td>
<td><input type="text" name="website" placeholder="Enter your website..."/></td>
</tr>
<tr>
<td colspan="2">
<input type="reset" name="" value="Reset"/>
<input type="submit" name="register" value="Register"/>
</td>
</tr>
</table>
</form>
<?php //var_dump($_POST); ?>
</div><!--End Login_reg page-->
<div class="back">
<a href="">Back</a>
</div>
</div><!--End Content section-->
<div class="footer">
<h3>Training with live project</h3>
</div>
</div><!--End Wrapper section-->
</body>
</html>
要調試PDO語句,然後有一些有用的建議:[如何調試PDO數據庫查詢?](http://stackoverflow.com/questions/2411182/how-to-debug-pdo-database-queries)。對於mysql,這很有用:[如何以及何時啓用MySQL日誌](http://www.pontikis.net/blog/how-and-when-to-enable-mysql-logs)。我使用'在運行時啓用日誌'選項。 –