Assuming that all of your sql column names are correct, something like this should work:
if(isset($_POST['submit'])){
global $wpdb;
$tablename= $wpdb . 'form_subscribe';
$myrows = $wpdb->get_var( $wpdb->prepare("SELECT email FROM $tablename WHERE email=%s LIMIT 1", $_POST['email']));
if(empty($myrows)){
$data=array(
'name' => $_POST['fullname'],
'age' => $_POST['age'],
'email' => $_POST['email']
);
$wpdb->insert( $tablename, $data);
}
else {
$status="User already subscribed";
}
$status="";
}
I changed $wpdb->email
to $_POST['email']
, I think that is what you meant there. Additionally, I added the $wpdb->prepare()
function for sanitation and $wpdb->prefix
for best practices.
Since you are doing a lookup to check for something existing (i.e. 1 existing record means the same thing as 1000 existing records for our purposes) I changed $wpdb->get_results()
to $wpdb->get_var()
and added a limit clause. This will make the query more efficient.