这里我有一个基本的注册表格。
<form name="signUp" id="signUp" method="POST" action="insertUser.php">
<table>
<tr>
<td>Name</td>
<td><input type="text" name="signUpName" placeholder="Name"/></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="signUpEmail" placeholder="Email"/></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="signUpPassword" placeholder="password"/></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Go"/></td>
</tr>
</table>
</form>
将字段数据发送到文件insertUser.php
。
<?php
include 'User.php';
if(isset($_POST['submit'])){
$user = new User();
$user->setUserName($_POST['signUpName']);
$user->setUserEmail($_POST['signUpEmail']);
$user->setUserPassword($_POST['signUpPassword']);
$user->userInsert();
}
?>
该文件创建名为User
的类的对象,并将从表单接收的数据传递给该类的变量。 userInsert
类的User
函数最终将数据插入数据库。
<?php
class User{
public $userName, $userEmail, $userPassword;
public function getUserName(){
return $this->userName;
}
public function setUserName($userName){
$this->userName = $userName;
}
public function getUserEmail(){
return $this->userEmail;
}
public function setUserEmail($userEmail){
$this->userEmail = $userEmail;
}
public function getUserPassword(){
return $this->userPassword;
}
public function setUserPassword($userPassword){
$this->userPassword = $userPassword;
}
public function userInsert(){
global $userName, $userEmail, $userPassword;
include 'db_connection.php';
$r = $db->prepare("INSERT INTO user(Username, Useremail, Userpassword) VALUES(:userName, :userEmail, :userPassword);");
$r->bindParam(':userName',$userName);
$r->bindParam(':userEmail',$userEmail);
$r->bindParam(':userPassword',$userPassword);
$r->execute();
header('Location: index.php?successful=1');
}
}
数据库连接
<?php
try{
$db = new PDO("mysql:host=localhost;dbname=basicchatapp","root","");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(Exception $e){
die("Error: ");
}
?>
现在的问题是,每次插入NULL
输入时。
搜索了许多其他答案,但没有一个解决方案适合我。获得提示将有很大帮助。谢谢。
答案 0 :(得分:0)
可能是你的userInsert()函数找不到变量值所以在函数中传递那些参数然后它应该看起来像下面的代码
class User{
public $userName, $userEmail, $userPassword;
public function getUserName(){
return $this->userName;
}
public function setUserName($userName){
$this->userName = $userName;
}
public function getUserEmail(){
return $this->userEmail;
}
public function setUserEmail($userEmail){
$this->userEmail = $userEmail;
}
public function getUserPassword(){
return $this->userPassword;
}
public function setUserPassword($userPassword){
$this->userPassword = $userPassword;
}
public function userInsert($userName=null,$userEmail=null,$userPassword=null){
include 'db_connection.php';
$sql = "INSERT INTO user(Username, Useremail, Userpassword) VALUES($userName,$userEmail,$userPassword);";
if(mysqli_query($db,$sql) == TRUE){
header('Location: index.php?successful=1');
}
else{
header('Location: index.php?successful=0');
}
}
}
答案 1 :(得分:0)
虽然您应该考虑在PHP中使用绑定参数和预处理语句的@tadman注释,但您可能会发现以下行应该有$ this-&gt;在变量前面设置范围。
$sql = "INSERT INTO user(Username, Useremail, Userpassword) VALUES('$userName','$userEmail','$userPassword');";
$sql = "INSERT INTO user(Username, Useremail, Userpassword) VALUES('$this->userName','$this->userEmail','$this->userPassword');";
答案 2 :(得分:0)
请用此
替换您的查询$ sql =“INSERT INTO user(Username,Useremail,Userpassword)VALUES('$ this-&gt; userName','$ this-&gt; userEmail','$ this-&gt; userPassword');”; < / p>