mahenda Posted November 14, 2019 Share Posted November 14, 2019 (edited) <?php global $con; $name = $_GET['name']; $email = $_GET['email']; if((!empty($name)) && (!empty($email))){ $check = $con->prepare('SELECT email FROM subs WHERE email = ?'); $check->execute([$email]); $row = $check->fetch(); if($email == $row['email']){ echo '<span>'.$email.' already in database, try another email!.</span>'; }else{ $subscribe = $con->prepare('INSERT INTO subs (name, email) VALUES (?, ?)'); $subscribe->execute([$name, $email]); if($subscribe){ echo '<span>Hi '.$name.'</b>,subscription is now active.</span>'; }else{ echo '<span>Try again</span>'; } } }else{ echo '<span>please enter something</span>'; } ?> the problem found at if($subscribe){ //the echo not showing the span element when data is inserted } any help please? Edited November 14, 2019 by cyberRobot added code indents Quote Link to comment https://forums.phpfreaks.com/topic/309526-what-wrong-with-this-code/ Share on other sites More sharing options...
ginerjm Posted November 14, 2019 Share Posted November 14, 2019 Is the echo for "not being inserted" showing up? PS - your check to see if a row is returned matching your $email could also be done by simply checking the number of rows returned. Plus - using the input value directly could compare a bad input value against the most-likely 'good' value saved in your db if your user is providing 'bad' input. Quote Link to comment https://forums.phpfreaks.com/topic/309526-what-wrong-with-this-code/#findComment-1571566 Share on other sites More sharing options...
mahenda Posted November 14, 2019 Author Share Posted November 14, 2019 38 minutes ago, ginerjm said: Is the echo for "not being inserted" showing up? PS - your check to see if a row is returned matching your $email could also be done by simply checking the number of rows returned. Plus - using the input value directly could compare a bad input value against the most-likely 'good' value saved in your db if your user is providing 'bad' input. The first echo after 54 minutes ago, mahenda said: if($email == $row['email']){ The echo after the above code is show and it is okey but the problem is there on the echo after insertion of data the echo doesn't show the given success message on the page Quote Link to comment https://forums.phpfreaks.com/topic/309526-what-wrong-with-this-code/#findComment-1571567 Share on other sites More sharing options...
cyberRobot Posted November 14, 2019 Share Posted November 14, 2019 I'm guessing that you are using PDO for your database connection? Have you checked for errors? Perhaps the following will help:https://www.php.net/manual/en/pdostatement.errorinfo.php Quote Link to comment https://forums.phpfreaks.com/topic/309526-what-wrong-with-this-code/#findComment-1571568 Share on other sites More sharing options...
ajoo Posted November 14, 2019 Share Posted November 14, 2019 Hi, This is a probably a wrong way of inserting new email into the DB and can result in race conditions. You should be inserting the new email directly into the DB and your column for ermail ids should be unique so that it throws an exception for duplicate entries. 2 Quote Link to comment https://forums.phpfreaks.com/topic/309526-what-wrong-with-this-code/#findComment-1571569 Share on other sites More sharing options...
mahenda Posted November 15, 2019 Author Share Posted November 15, 2019 All queries are inserted successfully but why this statement is not showing on page to tell user about subscription is sent successfully This one echo '<span>Hi '.$name.'</b>,subscription is now active.</span>'; Quote Link to comment https://forums.phpfreaks.com/topic/309526-what-wrong-with-this-code/#findComment-1571587 Share on other sites More sharing options...
Barand Posted November 15, 2019 Share Posted November 15, 2019 What happens if you actually test if the execute() worked? Instead of $subscribe->execute([$name, $email]); if($subscribe){ echo .... try if ($subscribe->execute([$name, $email])) { echo .... Quote Link to comment https://forums.phpfreaks.com/topic/309526-what-wrong-with-this-code/#findComment-1571588 Share on other sites More sharing options...
mac_gyver Posted November 15, 2019 Share Posted November 15, 2019 what output do you get and if it's a blank page, what does the 'view source' of the page in your browser show? either the output is hidden in the 'view source' or the code isn't being executed. since you apparently are getting the data inserted, it's possible you are 'seeing' the result of a double page request, especially since you are using a get request to do this rather than a post method form. Quote Link to comment https://forums.phpfreaks.com/topic/309526-what-wrong-with-this-code/#findComment-1571589 Share on other sites More sharing options...
mahenda Posted November 15, 2019 Author Share Posted November 15, 2019 41 minutes ago, Barand said: What happens if you actually test if the execute() worked? Instead of $subscribe->execute([$name, $email]); if($subscribe){ echo .... try if ($subscribe->execute([$name, $email])) { echo .... Here the data is sent in a database but the echo '<span>Hi '.$name.'</b>,subscription is now active.</span>'; doesn't work. In else statement Quote Link to comment https://forums.phpfreaks.com/topic/309526-what-wrong-with-this-code/#findComment-1571590 Share on other sites More sharing options...
mahenda Posted November 15, 2019 Author Share Posted November 15, 2019 36 minutes ago, mac_gyver said: what output do you get and if it's a blank page, what does the 'view source' of the page in your browser show? either the output is hidden in the 'view source' or the code isn't being executed. since you apparently are getting the data inserted, it's possible you are 'seeing' the result of a double page request, especially since you are using a get request to do this rather than a post method form Thanks you guys I solved the problem. It was the view part not a PHP code Quote Link to comment https://forums.phpfreaks.com/topic/309526-what-wrong-with-this-code/#findComment-1571592 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.