Php 为什么不将数据插入MySQL数据库?
我正在设计一个简单的聊天应用程序,它只是插入和检索数据。我正在使用PHP和MySQL。我在YouTube上看到了这个教程,并试图做同样的事情。我在下面发布代码,问题是新用户没有被插入数据库 以下是数据库代码:Php 为什么不将数据插入MySQL数据库?,php,mysql,sql,database,pdo,Php,Mysql,Sql,Database,Pdo,我正在设计一个简单的聊天应用程序,它只是插入和检索数据。我正在使用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 -- Serve
-- 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>
现在,当我注册为新用户时,它会通知用户已插入,但当我在phpMyAdmin中检查数据库用户时,表仍然为空。数据库用户和密码正确。并且没有抛出异常和遇到错误。我不明白为什么它不起作用 需要在执行中使用“: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()
));
}
答:这并不是必须的。亚历克斯,这不是我的反对票,是的,关于冒号,手册上并没有指定它是可选的。一些程序员不仅能读,还能写。你可以编写一个程序,看看他们的想法是对是错。@YourCommonSense:我希望stackoverflow上的一切都不是老派社区,而是讽刺和不友好的
<?php
try{
$bdd=new PDO("mysql:host=localhost;dbname=chat","root","demo123");
}
catch(Exception $e)
{
die("ERROR:".$e->getMesssge());
}
<?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");
}
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()
));
}