检查数据库中是否已存在电子邮件

时间:2018-02-22 13:40:21

标签: php

我希望人们留下电子邮件发送简报,我认为它有效,而且它看起来正确,但必须做错事,因为即使数据库中没有电子邮件,它也会继续执行其他声明! / p>

这是代码:

 public function newsStore(Request $request)
{

    $this->validate($request, [

            'email' => 'required|max:155', 

              ]);


      $email = $request['email'];

      $result = DB::table('newsletters')->where( 'email', '=', $email)->count();   

      if($result = 0){

              $news = DB::table('newsletters')->insert(
                  [
                     'users_id' => 0,
                     'email' => request('email'),

                  ]);

                 session()->flash('message', 'Thank you for your email!');  

                  return back();

       }else{

                session()->flash('message', 'you have already given your email!');   
                return back();
      }
   }

以下是索引页面上的代码:

 @guest
     <form method="POST" action="/sub2">
       {{ csrf_field() }}

   <div class="input-group">
       <input type="email" name="email" class="form-control" size="155" placeholder="For our News Letter Email Address" required>

        <div class="input-group-btn">
        <button type="submit" class="btn btn-danger">Subscribe</button>
     </div>      
   </div> 
</form>

  @else
  <form method="POST" action="/sub">
    {{ csrf_field() }}

       <div class="input-group">
           <input type="email" name="email" class="form-control" size="155" placeholder="For our News Letter Email Address" required>

            <div class="input-group-btn">
              <button type="submit" class="btn btn-danger" >Subscribe</button>          
            </div>      
       </div> 
   </form>

 @endguest

1 个答案:

答案 0 :(得分:0)

`if($result = 0)`{

这里为$ result变量指定0。所以总是假的。 使用

if($result == 0){