字段是空的mysqli

时间:2016-07-10 17:42:45

标签: php mysqli

我的代码有问题,如果输入为空则需要make将不会执行数据库中的任何内容,并发送一条消息,该消息应填写所有字段并且已整天调查但我不能给出解决方案希望某人可以帮助我。

如果字段为空,我怎么能这样做,发送一条说错误的消息并且不在数据库中执行任何操作。

<?php
session_start();
if(isset($_SESSION['userSession'])!="")
{
 header("Location: me.php");
}
include_once 'sistema/config.web.php';

if(isset($_POST['btn-login']))
{
 $email = $MySQLi_CON->real_escape_string(trim($_POST['user_email']));
 $upass = $MySQLi_CON->real_escape_string(trim($_POST['password']));

 $query = $MySQLi_CON->query("SELECT * FROM users WHERE mail='$email'");
 $row=$query->fetch_array();

 if(password_verify($upass, $row['password']))
 {
  $_SESSION['userSession'] = $row['id'];
  header("Location: me.php");
 }
 else
 {
  $msg = "Email o contraseña no encontrados";
 }

 $MySQLi_CON->close();

}

if(isset($_POST['btn-signup']))
{
$uname = $MySQLi_CON->real_escape_string(trim($_POST['user_name']));
$email = $MySQLi_CON->real_escape_string(trim($_POST['user_email']));
$upass = $MySQLi_CON->real_escape_string(trim($_POST['password']));
$new_password = password_hash($upass, PASSWORD_DEFAULT);
$check_email = $MySQLi_CON->query("SELECT mail FROM users WHERE mail='$email'");
$count=$check_email->num_rows;
if($count==0){
$query = "INSERT INTO users(username,mail,password) VALUES('$uname','$email','$new_password')";
if($MySQLi_CON->query($query))
{
$msge = "Registrado correctamente";
}
else
{
$msge = "Eror al registrarte";
}
}
else{
$msge = "Email en uso";
}
$MySQLi_CON->close();
}
?>

1 个答案:

答案 0 :(得分:1)

你可以试试这个:

if(isset($_POST['btn-signup'])) {

     $uname = $MySQLi_CON->real_escape_string(trim($_POST['user_name']));
     $email = ......
     $upass = ......

     /* Modified code starts */

     $valid = true;

     if(empty($uname)) {
         $msge[] = "Username is required.";
         $valid = false;
     } if(empty($email)) {
         $msge[] = "email is required.";
         $valid = false;
     } if(empty($upass)) {
         $msge[] = "Password is required.";
         $valid = false;
     }

     /* Modified code ends */

     $new_password = ......
     $check_email =....
     $count = ....

     /* Modified code starts again */

     if ($count > 0) {
       $msge[] = "Email en uso";
       $valid = false;
     }

     if ($valid) {

          $query = "INSERT INTO users(username,mail,password) VALUES('$uname','$email','$new_password')";
          if ($MySQLi_CON->query($query)) {
             $msge = "Registrado correctamente";
          } else {
               $msge = "Eror al registrarte";
          }
      }

     $MySQLi_CON->close();
  }

最后,打印警告信息:

echo implode('<br/>', $msge);