1
我正在使用PHP和mongodb進行註冊表單。這種形式的工作,但問題是它沒有執行驗證。即使我將所有字段留空,它也會使用空字段更新數據庫。它彷彿整個error = array();
是不可見的。php驗證和mongodb
我需要的是它執行檢查並不更新數據庫,直到滿足所有要求。
<?php
session_start();
if($_POST['submit']){
$ScName=strip_tags($_POST['ScName']);
$fname=strip_tags($_POST['fname']);
$lname=strip_tags($_POST['lname']);
$email=strip_tags($_POST['email']);
$password=strip_tags($_POST['password']);
$password2=strip_tags($_POST['password2']);
$error = array();
if(empty($email) or !filter_var($email,FILTER_SANITIZE_EMAIL)){
$error[] = "Email id is empty or invalid";
}
if(empty($password)){
$error[] = "Please enter password";
}
if(empty($password2)){
$error[] = "Please Confirm password";
}
if($password != $password2){
$error[] = "Password and Confirm password are not matching";
}
if(empty($fname)){
$error[] = "Enter first name";
}
if(empty($lname)){
$error[] = "Enter last name";
}
if(count($error == 0)){
//database configuration
$host = 'localhost';
$database_name = 'mongo1';
$database_user_name = '';
$database_password = '';
$connection=new Mongo('localhost');
if($connection){
//connecting to database
$database=$connection->user;
//connect to specific collection
$collection=$database->user;
$query=array('email'=>$email);
//check for existing username
//$query=array('ScName'=>$ScName);
//checking for existing user
$count=$collection->findOne($query);
if(!count($count)){
//Save the New user
$user=array('fname'=>$fname,'lname'=>$lname,'ScName'=>$ScName,'email'=>$email,'password'=>md5($password));
$collection->save($user);
echo "You are successfully registered.";
}else{
echo "Email already exists.Please register with another Email";
}
}else{
die("Database is not connected");
}
}else{
//Displaying the error
foreach($error as $err){
echo $err.'</br>';
}
}
}
?>
非常感謝您,現在可以工作。我在這裏呆了好幾個小時,不知道該怎麼辦 – ORYON100