0

I'm making a PHP Newsletter script, I'm not very experienced in code, but I try my best to improve, I just need a few ideas in order to make this work.

function validate(){
    if(isset($_POST['email'])){
        $email = $_POST["email"];
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        echo "<br>Va rugam introduceti o adresa valida de email";
        }else{
            return 1;
        }
      }
    }



function checkmail(){
        if(validate()==1){
            if(isset($_POST['email'])){
                $email = $_POST['email'];
                $sql = "SELECT * FROM subscribe WHERE email LIKE '$email'";
                $connect =  new mysqli("localhost", "root", "", "alexandru");
                $result = mysqli_query($connect,$sql);
                echo print_r($result);
            }        
        }

    }

I don't know how I can check the result of the query, I need some ideas, thanks

Alexandru Ianas
  • 95
  • 1
  • 11
  • 1
    Pass the cleaned `$_POST['email']` as a parameter to the function instead. Also, don't make a db connection each time, include a file which already has it. That way your code is more split up and easier to debug in the future. – Script47 Oct 16 '15 at 10:39
  • Thanks for the tip, I will do that, I apreciate it. – Alexandru Ianas Oct 16 '15 at 10:43
  • So the idea is that you check if the email exists in the `subscribe` table before you insert it? – Mjh Oct 16 '15 at 11:01
  • If you want to check for a record **before inserting**, please don't refer to the marked duplicate. – Mjh Oct 16 '15 at 11:15

2 Answers2

1

I have made this simple function which you can use.

function field_exists($field_name, $field_value, $table)    
    {
        global $conn;
        try
        {
            $s = $conn->prepare("SELECT * from $table where $field_name = :f_value");
            $s->bindParam(':f_value', $field_value);
            $s->execute();
            if($s->rowCount() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }
    }//function

Using this function, you can check any table for any value of the specified column.

So in your case, the $field_name would be email, $field_value would be $email and table would be subscribers.

Usage

if(field_exists("email", $email, "subscribers"))
{
//email exists
}
else
{
//email doesn't exist
}

The function would return true if this email in the table exists, and false if the email doesn't.

Akshay
  • 2,244
  • 3
  • 15
  • 34
  • 2
    using globals. OOOh. That hurts. – B001ᛦ Oct 16 '15 at 10:40
  • OP uses mysqli, concerning PDOException. – syck Oct 16 '15 at 10:41
  • @syck He can easily convert it to mysqli. I just copy pasted the function I've made myself for these kind of checks ;) – Akshay Oct 16 '15 at 10:42
  • @Akshay you generally provide an answer based on the OP question. – Script47 Oct 16 '15 at 10:42
  • @Script47 Well I just thought posting a better function would help OP in the long run. He might definitely want to do more of these type of checks in future. – Akshay Oct 16 '15 at 10:43
  • considering the OP has accepted they do not have a strong knowledge of code in general, it would be much more helpful to provide a MySQLi based function, in time they can research PDO of their own volition. – Martin Oct 16 '15 at 10:52
-1

This code i use whithout oops concept in my practicle ithink it'll help

  extract($_POST);
    $qwe = "SELECT * FROM user_info where email= '$email'";
    $result = mysqli_query($con, $qwe);
    if (mysqli_fetch_row($result) >= 1) {
      header("Location: userreg.php?err=msg");
    }
    else {
        // query for what u want after check mail doesn't exist
    }
Chirag Senjaliya
  • 460
  • 3
  • 16