Jump to content

what wrong with this code?


mahenda

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
Link to comment
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.

Link to comment
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 

Link to comment
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
Link to comment
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.

Link to comment
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

Link to comment
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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.