2016-12-20 36 views
0

我正在使用PHP一個項目,我做了這個文件:PHP查詢不返回真實情況

<?php 
$err = false; 
if (isset($_POST['submit'])){ 
    $projectname = $_POST['project_name']; 
    if (strlen($projectname)>10){ 
     $err = true; 
     echo " 
     <script type='text/javascript'> 
      alert('Your project name contains too many words'); 
     </script> 
     "; 
    } 
    $emailone = $_POST['mail_one']; 
    $emailtwo = $_POST['mail_two']; 
    $numstars = $_POST['num_stars']; 
    $numchars = $numstars + 2; 
} 
?> 
<!DOCTYPE html> 
<html> 
    <head> 
     <title>Welcome To Email Guesser</title> 
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css"> 
     <link rel="stylesheet" href="style.css"> 
    </head> 
    <body> 
     <div class="container"> 
      <br /> 
      <h1 style="text-align:center;color: #F90B6D; font-family: 'Open Sans', sans-serif; font-size: 34px; font-weight: 300; line-height: 40px; margin: 0 0 16px;">Email Guesser <small>v0.2</small></h1> 
      <h1 style="text-align:center;color: #F90B6D; font-family: 'Open Sans', sans-serif; font-size: 24px; font-weight: 300; line-height: 32px; margin: 0 0 14px;"><a href="http://www.emailguesser.pouyavagefi.com">www.emailguesser.pouyavagefi.com</a></h1> 
      <br /> 
      <div class="inner contact"> 
       <!-- Form Area --> 
       <div class="contact-form"> 
        <!-- Form --> 
        <form id="contact-us" method="POST" action="action.php"> 
         <!-- Left Inputs --> 
         <div class="col-xs-6 wow animated slideInLeft" data-wow-delay=".5s"> 
          <!-- Name --> 
          <input type="text" name="project_name" id="name" required="required" class="form" placeholder="Name Of Project" /> 
          <!-- Another Email Address --> 
          <input type="email" name="mail_one" id="mail" class="form" placeholder="Other Email Address You May Know #1" /> 
          <!-- Another Email Address Two --> 
          <input type="email" name="mail_two" id="mail" class="form" placeholder="Other Email Address You May Know #2" /> 
          <!-- Subject --> 
          <input type="number" name="num_stars" id="subject" required="required" class="form" placeholder="Number Of Stars" /> 
         </div><!-- End Left Inputs --> 

         <!-- Right Inputs --> 
         <div class="col-xs-6 wow animated slideInRight" data-wow-delay=".5s"> 
          <!-- Message --> 
          <textarea name="message" id="message" class="form textarea" placeholder="Message"></textarea> 
         </div><!-- End Right Inputs --> 
         <!-- Bottom Submit --> 
          <div class="relative fullwidth col-xs-12"> 
          <button type="submit" id="submit" name="submit" class="form-btn semibold">Submit</button> 
         </div><!-- End Bottom Submit --> 
         <!-- Clear --> 
         <div class="clear"></div> 
        </form><p><i>make your project online on social medias with this hashtag: <strong>#hcktck</strong></i></p> 
       </div><!-- End Contact Form Area --></br> 
      </div> 
      <hr class="style-one">     
      <!-- Showing Outputs -->      
      <div class="container outptut"> 
       <h3>Returning Results:</h3> 
       <?php 
       if($err=false){ 
        echo " 
          <p class='bg-primary'> &nbsp; Victim's email address is $numchars characters long</p> 
          <p class='bg-success'> &nbsp; This text indicates success.</p> 
          <p class='bg-info'> &nbsp; This text represents some information.</p> 
          <p class='bg-warning'> &nbsp; This text represents a warning.</p> 
          <p class='bg-danger'> &nbsp; This text represents danger.</p> 
        "; 
       }else{ 
        echo "<h4 style='color:red;'>an error occured, try again or contact with our developers.</h4>"; 
       } 
       ?> 
      </div> 
      <center> Visit Developer's Website <a href="http://pouyavagefi.com" target="blank">Pouya Vagefi </a> </center> 
     </div> 
    </body> 
</html> 

此文件被稱爲action.php,每當我用正確的信息運行它,它表明我an error occured, try again or contact with our developers.這是我之前設定的。基本上你可以看到我調用了一個布爾變量($ err),如果出現任何類型的錯誤(例如,項目名稱中包含太多字符),則該變量爲true。

但問題是這個變量設置爲true,即使我提交表單的正確信息。不過,我已經在頁面頂部的$ err = false處調用。

那麼爲什麼這件事發生?請注意,根本沒有出現php錯誤。

+0

'$ err = false'應該是'$ err == false'。在if語句中。用一個等號表示總是設置爲false。 – michaPau

回答

1

變化if($err=false){if($err === false){來自:

<?php if($err=false){ echo " 
          <p class='bg-primary'> &nbsp; Victim's email address is $numchars characters long</p> 
          <p class='bg-success'> &nbsp; This text indicates success.</p> 
          <p class='bg-info'> &nbsp; This text represents some information.</p> 
          <p class='bg-warning'> &nbsp; This text represents a warning.</p> 
          <p class='bg-danger'> &nbsp; This text represents danger.</p> 
        "; }else{ echo "<h4 style='color:red;'>an error occured, try again or contact with our developers.</h4>"; } ?> 

你使用它的方式,你只是asigning值。

您需要實際驗證這兩個值。如果您使用===,那麼您還要檢查變量類型,在這種情況下爲boolean

+1

這就是爲什麼開發者應該習慣YODA條件 – Robert

0

您需要將支票設置爲if($err===false)。你現在的方式是將它設置爲false,而不是檢查它是否爲false。