我正在设计一个简单的聊天应用程序,它只插入和检索数据。我正在使用PHP和MySQL。我在YouTube上看到了这个教程并试图做同样的事情。我发布下面的代码,问题是新用户没有插入数据库。
以下是数据库代码:
-- phpMyAdmin SQL Dump
-- version 4.1.12
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Apr 20, 2014 at 01:24 AM
-- Server version: 5.6.16
-- PHP Version: 5.3.28
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `chat`
--
-- --------------------------------------------------------
--
-- Table structure for table `chats`
--
CREATE TABLE IF NOT EXISTS `chats` (
`ChatId` int(11) NOT NULL,
`ChatUserId` int(11) NOT NULL,
`ChatText` text NOT NULL,
PRIMARY KEY (`ChatId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
CREATE TABLE IF NOT EXISTS `users` (
`UserId` int(11) NOT NULL AUTO_INCREMENT,
`UserName` varchar(50) NOT NULL,
`UserMail` varchar(50) NOT NULL,
`UserPassword` text NOT NULL,
PRIMARY KEY (`UserId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
现在index.php文件用于注册和登录:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script src="../JqueryLibrary/jquery-1.8.3.js" type="text/javascript"></script>
<script src="../JqueryLibrary/jquery-1.8.3.min.js" type="text/javascript"></script>
<title>Welcome to Chat App</title>
</head>
<body>
<h2>LOGIN FORM</h2>
<div id="LoginDiv">
<form action="pages/UserLogin.php" method="post">
<table>
<tr>
<td>Email:</td>
<td><input name="UserMailLogin" type="email"></td>
</tr>
<tr>
<td>Password</td>
<td><input name="UserPasswordLogin" type="password"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="LOG IN"></td>
</tr>
<?php
if(isset($_GET['error'])){
?>
<tr>
<td></td>
<td><span style="color:red">ERROR LOGIN</span></td>
</tr>
<?php
}
?>
</table>
</form>
</div><br>
<br>
<br>
<h2>SIGN UP FORM</h2>
<div id="SignUpDiv">
<form action="pages/InsertUser.php" method="post">
<table>
<tr>
<td>Your Name</td>
<td><input name="UserName" type="text"></td>
</tr>
<tr>
<td>Your Email:</td>
<td><input name="UserMail" type="email"></td>
</tr>
<tr>
<td>Password:</td>
<td><input name="UserPassword" type="password"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Sign Up"></td>
</tr>
<?php
if(isset($_GET['success'])){
?>
<tr>
<td></td>
<td><span style="color:green">User Inserted</span></td>
</tr>
<?php
}
?>
</table>
</form>
</div>
</body>
</html>
包含两个用户和聊天类的class.php。
<?php
class user{
private $UserId, $UserName, $UserMail, $UserPassword;
public
function getUserId(){
return $this->UserId;
}
public
function setUserId($UserId){
$this->UserId = $UserId;
}
public
function getUserName(){
return $this->UserName;
}
public
function setUserName($UserName){
$this->Username = $UserName;
}
public
function getUserMail(){
return $this->UserMail;
}
public
function setUserMail($UserMail){
$this->UserMail = $UserMail;
}
public
function getUserPassword(){
return $this->UserPassword;
}
public
function setUserPassword($UserPassword){
$this->UserPassword = $UserPassword;
}
public
function InsertUser(){
include "conn.php";
$req = $bdd->prepare("INSERT INTO users(Username,UserMail,UserPassword) VALUES (:UserName,:UserMail,:UserPassword");
$req->execute(array('UserName' => $this->getUserName() ,'UserMail' => $this->getUserMail() ,'UserPassword' => $this->getUserPassword()));
}
public
function UserLogin(){
include "conn.php";
$req = $bdd->prepare("SELECT * FROM users WHERE UserMail=:UserMail AND UserPassword=:UserPassword");
$req->execute(array('UserMail' => $this->getUserMail() ,'UserPassword' => $this->getUserPassword()));
if ($req->rowcount() == 0) {
header("Location: ../index.php?error=1");
} else {
while ($data = $req->fetch()) {
$this->setUserId($data['UserId']);
$this->setUserName($data['UserName']);
$this->setUserPassword($data['UserPassword']);
$this->setUserMail($data['UserMail']);
header("Location: Home.php");
return true;
}
}
}
}
class chat{
private $ChatId, $ChatUserId, $ChatText;
public
function getchatId(){
return $this->ChatId;
}
public
function setChatId($ChatId){
$this->ChatId = $ChatId;
}
public
function getChatUserId(){
return $this->ChatUserId;
}
public
function setChatUserId($ChatUserId){
$this->ChatUserId = $ChatUserId;
}
public
function getChatText(){
return $this->ChatText;
}
public
function setChatText($ChatText){
$this->ChatText = $ChatText;
}
public
function InsertChatMessage(){
include "conn.php";
$req = $bdd->prepare("INSERT INTO chats(ChatUserId,ChatText)VALUES(:ChatUserId,:ChatText)");
$req->execute(array('ChatUserId' => $this->getChatUserId() ,'ChatText' => $this->getChatText() ,));
}
public
function DisplayMessage(){
include "conn.php";
$ChatReq = $bdd->prepare("SELECT * from chats ORDER BY ChatId DESC");
$ChatReq->execute();
while ($DataChat = $ChatReq->fetch()) {
$UserReq = $bdd->prepare("SELECT * FROM users WHERE UserId=:UserId");
$UserReq->execute(array('UserId' => $DataChat['ChatUserId']));
$DataUser = $UserReq->fetch();
?>
<span class="UserNameS"><?php
echo $DataUser['UserName']; ?></span>says :<br/>
<span class="ChatMessagesS"><?php
echo $DataChat['ChatText']; ?></span><br/>
<?php
}
}
}
这个脚本用于使用PDO进行数据库连接:
<?php
try{
$bdd=new PDO("mysql:host=localhost;dbname=chat","root","demo123");
}
catch(Exception $e)
{
die("ERROR:".$e->getMesssge());
}
insertUser.php
脚本如下:
<?php
include "classes.php";
$user=new user();
if(isset($_POST['UserName']) && isset($_POST['UserMail']) && isset($_POST['UserPassword'])){
$user->setUserName($_POST['UserName']);
$user->setUserMail($_POST['UserMail']);
$user->setUserPassword(sha1($_POST['UserPassword']));
$user->InsertUser();
header ("Location: ../index.php?success=1");
}
现在,当我注册为新用户时,它会通知用户已插入,但当我在phpMyAdmin中检查数据库用户时,该表仍为空白。数据库用户和密码是正确的。并且没有抛出异常并遇到错误。我不明白为什么它不起作用。
答案 0 :(得分:-1)
在执行中需要使用':variable'而不是'variable'。
public function InsertUser(){
include "conn.php";
$req = $bdd->prepare("INSERT INTO users(Username,UserMail,UserPassword) VALUES (:UserName,:UserMail,:UserPassword");
$req->execute(array(
':UserName' => $this->getUserName(),
':UserMail' => $this->getUserMail(),
':UserPassword' => $this->getUserPassword()
));
}