注意:未定义的索引:( isset不起作用)

时间:2014-11-20 04:03:17

标签: php mysqli isset

我尝试进行创建自动密码的注册并通过电子邮件发送,但每当执行代码时,我都会在所有字段中收到此错误(注意:未定义的索引:)。请帮忙

isset

 //whether the name is blank
    if (isset($_POST['name']) and $_POST['name'] == '') 
 {
  $_SESSION['error']['name'] = "First Name is required.";
 }

配置

<?php



session_start();
include('/includes/db2.inc.php');
if(isset($_POST['submit']))
{

     //whether the name is blank
    if($_POST['name'] == '')
 {
  $_SESSION['error']['name'] = "First Name is required.";
 }
        //whether the last name is blank
    if($_POST['lastname'] == '')
 {
  $_SESSION['error']['lastname'] = "Last Name is required.";
 }


 //whether the email is blank
 if($_POST['email'] == '')
 {
  $_SESSION['error']['email'] = "E-mail is required.";
 }
 else
 {
  //whether the email format is correct
  if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9._-]+)+$/", $_POST['email']))
  {
   //if it has the correct format whether the email has already exist
   $email= $_POST['email'];
   $sql1 = "SELECT * FROM users WHERE email = '$email'";
   $result1 = mysqli_query($mysqli,$sql1) or die(mysqli_error());
   if (mysqli_num_rows($result1) > 0)
            {
    $_SESSION['error']['email'] = "This Email is already used.";
   }
  }
  else
  {
   //this error will set if the email format is not correct
   $_SESSION['error']['email'] = "Your email is not valid.";
  }
 }
    //whether the username is blank
 if($_POST['username'] == '')
 {
  $_SESSION['error']['username'] = "User Name is required.";
 }
    //whether the pharmacy is blank
 if($_POST['pharmacy'] == '')
 {
  $_SESSION['error']['pharmacy'] = "Pharmacy Name is required.";
 }
    //whether the The RX30 Lincence is blank
 if($_POST['rx30lincence'] == '')
 {
  $_SESSION['error']['rx30lincence'] = "RX30 Lincence Name is required.";
 }


 function randomPassword() {
    $alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
    $pass = array(); //remember to declare $pass as an array
    $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
    for ($i = 0; $i < 8; $i++) {
        $n = rand(0, $alphaLength);
        $pass[] = $alphabet[$n];
    }
    return implode($pass); //turn the array into a string
}
. 
$rampass = randomPassword();

 //if the error exist, we will go to registration form
 if(isset($_SESSION['error']))
 {
  header("Location: regis.html.php");
  exit;
 }

 else

 {
$name = $_POST['name'];
     $lastname = $_POST['lastname'];
  $email = $_POST['email'];
   $username = $_POST['username'];

  $password = md5('$rampass');
  $licence = $_POST['rx30licence'];
  $city = $_POST['city'];
  $phone = $_POST['phone'];
  $fax = $_POST['fax'];
  $com_code = md5(uniqid(rand()));

  $sql2 = "INSERT INTO users (name, lastname, email, username, password, licence, city, phone, fax, com_code) VALUES ('name', '$lastname', '$email', '$username', '$password', '$licence', '$city', '$phone', '$fax', '$com_code')";
  $result2 = mysqli_query($mysqli,$sql2) or die(mysqli_error());

  if($result2)
  {
   $to = $email;
   $subject = "Confirmation from Ontime Soft inc to $username";
   $header = "Ontime Soft inc: Confirmation from Ontime Soft inc";
   $message = "your password is $rampass. rn";
   $message = "Please click the link below to verify and activate your account. rn";
   $message .= "http://www.rx30pr.com/confirm.php?passkey=$com_code";

   $sentmail = mail($to,$subject,$message,$header);

   if($sentmail)
            {
   echo "Your Confirmation link and password Has Been Sent To Your Email Address.";
   }
   else
         {

    echo "Cannot send Confirmation link to your e-mail address";
   }
  }
 }
}

?>

HTML

<?php 

 session_start();
 if(isset($_SESSION['error']))
 {
  echo '<div class="alert alert-danger" role="alert">'.$_SESSION['error']['name'].'</div>';
  echo '<div class="alert alert-danger" role="alert">'.$_SESSION['error']['lastname'].'</div>';
  echo '<div class="alert alert-danger" role="alert">'.$_SESSION['error']['email'].'</div>';
  echo '<div class="alert alert-danger" role="alert">'.$_SESSION['error']['username'].'</div>';
  echo '<div class="alert alert-danger" role="alert">'.$_SESSION['error']['pharmacy'].'</div>';
  echo '<div class="alert alert-danger" role="alert">'.$_SESSION['error']['rx30licence'].'</div>';
  echo '<div class="alert alert-danger" role="alert">'.$_SESSION['error']['city'].'</div>'; 
  unset($_SESSION['error']);
 }
?>
       <!--- comienzo del formulario de registro---->
        <section class="register">
            <div class="register-panel">
            <form role="form" action="regiter_conf.php" method="post">
                <div class="row">
                    <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 reg">
                        <h3 class="text-muted">Register</h3>
                        <div class="form-group">

                              <input name="name" type="firstname"class="form-control" id="name" placeholder="First Name">
                          </div>
                        <div class="form-group">
                              <input  name="lastname" type="lastname" class="form-control" id="lastname" placeholder="Last Name">
                          </div>
                     <div class="form-group">
                              <input name="email" type="email" class="form-control" id="email" placeholder="Email">
                          </div>

                            <div class="form-group">
                              <input name="username" type="username" class="form-control" id="username" placeholder="Username">
                          </div>

                          <div class="form-group">

                              <div class="input-group">
                              <h5 class="form-control" >Password <small>will be send to you via email</small></h5>

                               </div>
                          </div>
                 <div class="form-group">
                              <input type="pharmacy" class="form-control" id="pharmacy" placeholder="Pharmacy Name">
                          </div>
                <div class="form-group">
                              <input type="rx30licence" class="form-control" id="rx30licence" placeholder="RX30 Licence">
                          </div>
                                <div class="form-group">
                              <input type="city" class="form-control" id="city" placeholder="City">
                          </div>
                <div class="form-group">
                              <input type="phone" class="form-control" id="phone" placeholder="Phone#">
                          </div>
                <div class="form-group">
                              <input type="fax" class="form-control" id="fax" placeholder="Fax#">
                          </div>
                   <span class="input-group-btn">
                                    <input name="submit" type="submit" value="Submit"/>
                                  </span>

                    </div>
                    <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
                        <h4 class="text-muted text-center">Politica</h4>
                        <div class="row">
                            <p class="text-muted padded">Lorem im pust</p>
                        </div>
                    </div>
                </div>
                </form>

2 个答案:

答案 0 :(得分:0)

如果未设置$_POST['name'],则会发出通知,因此您可以使用:

if (isset($_POST['name']))
{
if ($_POST['name'] == '')
{
$_SESSION['error']['name'] = "First Name is required.";
}
}

答案 1 :(得分:0)

您错过了输入name并使用错误的type使用type ="text"

缺少input name将无法获取您的数据,而且没有type="pharmacy"等等......

 <div class="form-group">
              <input type="text" name="pharmacy" class="form-control" id="pharmacy" placeholder="Pharmacy Name">
          </div>
<div class="form-group">
              <input  type="text" name="rx30licence" class="form-control" id="rx30licence" placeholder="RX30 Licence">
          </div>
                <div class="form-group">
              <input  type="text" name="city" class="form-control" id="city" placeholder="City">
          </div>
<div class="form-group">
              <input  type="text" name="phone" class="form-control" id="phone" placeholder="Phone#">
          </div>
<div class="form-group">
              <input  type="text" name="fax" class="form-control" id="fax" placeholder="Fax#">
          </div>
   <span class="input-group-btn">
                    <input name="submit" type="submit" value="Submit"/>
                  </span>

    </div>

这些错误的第一个解决方法是使用@Mark M所说的print_r($_POST) 也可以在条件中使用echo来检查特定条件是否正在执行