每当我提交表单时,只会在php类和处理提交的脚本之间发送第一个字符。 $ _post和变量的其他字符都将丢失。
例如,如果我在国家/地区下拉菜单中提交尼日利亚作为国家/地区,则只会在尼日利亚选择N.而Class方法只返回它应该返回的第一个字符。
这是处理表单提交的脚本的代码
<?php
class Signup extends Connection
{
private $fulname, $email, $phone, $address, $city, $state, $country, $bizType, $bizName, $mailerUse, $biz_website, $username, $password, $con;
private $err = array();
public function validateSignUp(
$fulname,
$username,
$email,
$phone,
$address,
$city,
$state,
$country,
$bizType,
$bizName,
$mailerUse,
$biz_website,
$password
) {
$this->con = Connection::connect();
$this->fulname = mysqli_real_escape_string($this->con, trim(stripslashes(strip_tags(ucfirst($fulname)))));
$this->email = mysqli_real_escape_string($this->con, trim(stripslashes(strip_tags($email))));
$this->username = mysqli_real_escape_string($this->con, trim(stripslashes(strip_tags($username))));
$this->phone = mysqli_real_escape_string($this->con, trim(stripslashes(strip_tags($phone))));
$this->address = mysqli_real_escape_string($this->con, trim(stripslashes(strip_tags(ucfirst($address)))));
$this->city = mysqli_real_escape_string($this->con, trim(stripslashes(strip_tags(ucfirst($city)))));
$this->state = mysqli_real_escape_string($this->con, trim(stripslashes(strip_tags(ucfirst($state)))));
$this->country = mysqli_real_escape_string($this->con, trim(stripslashes(strip_tags(ucfirst($country)))));
$this->bizType = mysqli_real_escape_string($this->con, trim(stripslashes(strip_tags(ucfirst($bizType)))));
$this->bizName = mysqli_real_escape_string($this->con, trim(stripslashes(strip_tags(ucfirst($bizName)))));
$this->mailerUse = mysqli_real_escape_string($this->con, trim(stripslashes(strip_tags($mailerUse))));
$this->biz_website = mysqli_real_escape_string($this->con,
trim(stripslashes(strip_tags(strtolower($biz_website)))));
$this->password = hash("SHA512", $password);
$this->err['count'] = 0;
if (!preg_match("/^[A-Za-z\s.\-]+$/", $this->fulname)) {
$this->err['fulname'] = '<p class="small alert alert-danger">Name contains invalid characters, please check entry again!</p>';
$this->err['count']++;
}
if (!filter_var($this->email, FILTER_VALIDATE_EMAIL)) {
$this->err['email'] = '<p class="alert alert-danger small">Invalid Email address please, verify!</p>';
$this->err['count']++;
}
if (!ctype_digit($this->phone)) {
$this->err['phone'] = '<p class="alert alert-danger small">Invalid phone number supplied! Only digits are accepted</p>';
$this->err['count']++;
}
if (strlen($this->phone) != 11 && $this->country == 'Nigeria') {
$this->err['phone'] = '<p class="alert alert-danger small">Phone number for Nigerian subscriber must be 11 digits!</p>';
$this->err['count']++;
}
if (!filter_var($this->biz_website, FILTER_VALIDATE_URL) && $this->biz_website != "") {
$this->err['website'] = '<p class="alert alert-danger small">Invalid website address submitted! Please, enter a valid website address example http://website.com</p>';
$this->err['count']++;
}
if (!preg_match("/^[A-Za-z]||[A-Za-z][0-9]$/", $this->username)) {
$this->err['username'] = '<p class="alert alert-danger small">Username can only have Uppercase, lowercase, digits or a mixture!</p>';
$this->err['count']++;
}
if (strlen($this->username) < 6 || strlen($this->username) > 16) {
$this->err['username'] = '<p class="alert alert-danger small">Username should have between 6 to 16 characters</p>';
$this->err['count']++;
}
if ($this->err['count'] == 0) {
$this->err['regReport'] = $this->regUser();
return $this->err['regReport'];
} else {
$this->err['regReport'] = '<div class="alert alert-danger">One or more field submitted contains invalid data. Correct and re-submit to continue</div>';
return $this->err;
}
}
private function regUser()
{
$dateReg = date("Y-m-d H:i:s");
$regUser = mysqli_query($this->con,
"INSERT INTO `mailer-account`(`email`,`username`,`password`,`phone`,`address`,`city`,`state`,`country`,`business_name`,`business_type`,`mailer-use`,`business_website`,`date_reg`) VALUES('$this->email','$this->username','$this->password','$this->phone','$this->address','$this->city','$this->state','$this->country','$this->bizName','$this->bizType','$this->mailerUse','$this->biz_website','$dateReg')");
if (mysqli_affected_rows($this->con) == 1) {
header("Location:../confirmation?suc=" . base64_encode("Registration Successful! A confirmation email has been sent to $this->email. Check email to activate your account!"));
} else {
return '<div class="alert alert-danger">Unable to complete registration at the moment! Err: SGN79</div>';
}
}
}
?>
下面是另一个文件中的PHP类脚本
{{1}}
答案 0 :(得分:0)
尝试通过在线论坛和文章找到的其他解决方案。后来我找到了问题的解决方案。
if(isset($_POST['sign_up'])==1){
$signUpHandler = new Signup($_POST);
$sub=1;
$signed = $signUpHandler->validateSignUp();
}
班级改为:
<?php
class Signup extends Connection{
private $_data = array(), $_con;
private $_err = array();
//Class constructor for Signing up
public function __construct($data = array()){
$this->_con = Connection::connect();
$this->_data['fulname'] = mysqli_real_escape_string($this->_con, trim(stripslashes(strip_tags(ucfirst($data['fulname'])))));
$this->_data['email'] = mysqli_real_escape_string($this->_con, trim(stripslashes(strip_tags($data['email']))));
$this->_data['username'] = mysqli_real_escape_string($this->_con, trim(stripslashes(strip_tags($data['username']))));
$this->_data['phone'] = mysqli_real_escape_string($this->_con, trim(stripslashes(strip_tags($data['phone']))));
$this->_data['address'] = mysqli_real_escape_string($this->_con, trim(stripslashes(strip_tags(ucfirst($data['address'])))));
$this->_data['city'] = mysqli_real_escape_string($this->_con, trim(stripslashes(strip_tags(ucfirst($data['city'])))));
$this->_data['state'] = mysqli_real_escape_string($this->_con, trim(stripslashes(strip_tags(ucfirst($data['state'])))));
$this->_data['country'] = mysqli_real_escape_string($this->_con, trim(stripslashes(strip_tags(ucfirst($data['country'])))));
$this->_data['bizType'] = mysqli_real_escape_string($this->_con, trim(stripslashes(strip_tags(ucfirst($data['biz_type'])))));
$this->_data['bizName'] = mysqli_real_escape_string($this->_con, trim(stripslashes(strip_tags(ucfirst($data['biz_name'])))));
$this->_data['mailerUse'] = mysqli_real_escape_string($this->_con, trim(stripslashes(strip_tags($data['mailer_use']))));
$this->_data['biz_website'] = mysqli_real_escape_string($this->_con, trim(stripslashes(strip_tags(strtolower($data['website'])))));
$this->_data['password'] = hash("SHA512",$data['password']);
}
//Validate User details
public function validateSignUp(){
$this->_err['count']=0;
if(!preg_match("/^[A-Za-z\s.\-]+$/",$this->_data['fulname'])){
$this->_err['fulname'] = '<p class="small alert alert-danger">Name contains invalid characters, please check entry again!</p>';
$this->_err['count']++;
}
if(!filter_var($this->_data['email'], FILTER_VALIDATE_EMAIL)){
$this->_err['email']='<p class="alert alert-danger small">Invalid Email address please, verify!</p>';
$this->_err['count']++;
}
if(!ctype_digit($this->_data['phone'])){
$this->_err['phone']='<p class="alert alert-danger small">Invalid phone number supplied! Only digits are accepted</p>';
$this->_err['count']++;
}
if(strlen($this->_data['phone'])!=11 && $this->_data['country']=='Nigeria'){
$this->_err['phone']='<p class="alert alert-danger small">Phone number for Nigerian subscriber must be 11 digits!</p>';
$this->_err['count']++;
}
if(!filter_var($this->_data['biz_website'],FILTER_VALIDATE_URL) && $this->_data['biz_website']!=""){
$this->_err['website']='<p class="alert alert-danger small">Invalid website address submitted! Please, enter a valid website address example http://website.com</p>';
$this->_err['count']++;
}
if(!preg_match("/^[A-Za-z]||[A-Za-z][0-9]$/",$this->_data['username'])){
$this->_err['username']='<p class="alert alert-danger small">Username can only have Uppercase, lowercase, digits or a mixture!</p>';
$this->_err['count']++;
}
if(strlen($this->_data['username'])<6 || strlen($this->_data['username'])>16){
$this->_err['username']='<p class="alert alert-danger small">Username should have between 6 to 16 characters</p>';
$this->_err['count']++;
}
if($this->_err['count']==0){
$this->_err['regReport'] = $this->regUser();
return $this->_err;
}else{
$this->_err['regReport'] = '<div class="alert alert-danger">One or more field submitted contains invalid data. Correct and re-submit to continue</div>';
return $this->_err;
}
}
private function regUser(){
$dateReg = date("Y-m-d H:i:s");
$regUser = mysqli_query($this->_con, "INSERT INTO `mailer-account`(`email`,`username`,`password`,`phone`,`address`,`city`,`state`,`country`,`business_name`,`business_type`,`mailer_use`,`business_website`,`date_reg`) VALUES('{$this->_data['email']}','{$this->_data['username']}','{$this->_data['password']}','{$this->_data['phone']}','{$this->_data['address']}','{$this->_data['city']}','{$this->_data['state']}','{$this->_data['country']}','{$this->_data['bizName']}','{$this->_data['bizType']}','{$this->_data['mailerUse']}','{$this->_data['biz_website']}','$dateReg')");
if(mysqli_affected_rows($this->_con)==1){
header("Location:../confirmation?suc=".base64_encode("Registration Successful! A confirmation email has been sent to ".$this->_data['email'].". Check email to activate your account!"));
}else{
return '<div class="alert alert-danger">Unable to complete registration at the moment! Err: SGN84</div>';
}
}
}
?>