验证码不起作用

时间:2017-03-31 10:55:14

标签: php mysqli

代码:

<?php
    session_start();
    if(isset($_POST['insert']) && !empty($_POST['insert']))
      {
        extract($_POST);
      $query = "select * from enquires2 where email = '$email'";
      $result = mysqli_query($link,$query);
      $row = mysqli_fetch_array($result);
      if($row > 0 )
      {
        $msg .="<h5 style='text-align:center;color:red;'>EmailId already exists please login with different emailid</h5>";
      }
      else
      {
        if(!empty($_POST['captcha_code']))
        {
          $captchaCode = $_SESSION['captchaCode'];
          $enteredcaptchaCode = $_POST['captcha_code'];
          $sql = "insert into enquires2(name,email,phone,message)values('$name','$email','$phone','$message')";
          $result=mysqli_query($link,$sql);
          if($result == true)
          {
          $msg .="<h4 style='text-align:center;color:green;'>Your Data Has Been Submitted.</h4>";

          }
          else 
          {
            $errMsg = 'Captcha code not matched, please try again.';
          }
        }
          else
          {
            $msg .="<h4 style='text-align:center;color:red;'>Error</h4>";
          }
      }
      }
    ?>

html代码:

<?php echo $msg; ?>
            <?php if(!empty($errMsg)) echo '<p style="color:#EA4335;">'.$errMsg.'</p>';?>
            <?php if(!empty($succMsg)) echo '<p style="color:#34A853;">'.$succMsg.'</p>';?>
            <form method="post">
              <input type="text" name="name" id="name" placeholder="Enter Your Name">
              <input type="text" name="email" id="email" placeholder="Enter Your Email">
              <input type="text" name="phone" id="phone" placeholder="Enter Your Phone">
              <input type="text" name="message" id="message" placeholder="Enter Your Message" >
              <input name="captcha_code" type="text" value="" placeholder="Enter the code" >
              <img src="captcha.php" id="capImage"/>
              <br/>Can't read the image? click here to  <a href="javascript:void(0);" onclick="javascript:$('#capImage').attr('src','captcha.php');">refresh</a>.
              <input type="submit" name="insert" value="Submit" placeholder="Enter Your Message" >
          </form>

当我点击提交按钮时,它显示数据已经成功提交,而验证码是正确的,或者它将表格值插入数据库。那么,我该如何解决这个问题?

谢谢

1 个答案:

答案 0 :(得分:0)

Please use this code:
<?php
session_start();
if(isset($_POST['insert']) && !empty($_POST['insert']))
  {
    extract($_POST);
  $query = "select * from enquires2 where email = '$email'";
  $result = mysqli_query($link,$query);
  $row = mysqli_fetch_array($result);
  if($row > 0 )
  {
    $msg .="<h5 style='text-align:center;color:red;'>EmailId already exists please login with different emailid</h5>";
  }
  else
  {
    if(!empty($_POST['captcha_code']))
    {
      $captchaCode = $_SESSION['captchaCode'];
      $enteredcaptchaCode = $_POST['captcha_code'];
      if($captchaCode == $enteredcaptchaCode)
      {
        $sql = "insert into enquires2(name,email,phone,message)values('$name','$email','$phone','$message')";
        $result=mysqli_query($link,$sql);
        if($result == true)
        {
          $msg .="<h4 style='text-align:center;color:green;'>Your Data Has Been Submitted.</h4>";
        }
        else 
        {
          $msg .= "<h4 style='text-align:center;color:green;'>Your Data Has Not Been Submitted.</h4>";
        }
      }
      else
      {
        $errMsg = 'Captcha code not matched, please try again.';
      }
    }
      else
      {
        $msg .="<h4 style='text-align:center;color:red;'>Error</h4>";
      }
    }
  }
?>