Jump to content
#StayAtHome ×
mahenda

what wrong with this code?

Recommended Posts

<?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 by cyberRobot
added code indents

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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 

Share this post


Link to post
Share on other sites

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.

 

  • Like 2

Share this post


Link to post
Share on other sites

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>';

 

Share this post


Link to post
Share on other sites

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 ....

 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.