PHP not updating MySQL DB

Tested this with $referrer NOT empty.
The MySQL default value of ‘clicks’ and ‘signup’ is 0 and both set as integers.

Everything is working, but these lines:

	if( ! empty($referrer))
	{
		$mysqli->query("UPDATE coming_soon_emails SET signup = signup + 1 WHERE code='" . $referrer ."'");
	}

Where does it need to be placed in my code for it to function correctly? Or have I errored somewhere?

Thanks.

if ( ! empty($referrer))
{
$mysqli->query("UPDATE coming_soon_emails SET clicks = clicks + 1 WHERE code='" . $referrer ."'");
}
//that works and updates correctly


if($_POST['email']){

    // Requested with AJAX:
    $ajax = ($_SERVER['HTTP_X_REQUESTED_WITH']  == 'XMLHttpRequest');

    try{
        if(!filter_input(INPUT_POST,'email',FILTER_VALIDATE_EMAIL)){
            throw new Exception('Invalid Email!');
        }

        $mysqli->query("INSERT INTO coming_soon_emails
                        SET email='".$mysqli->real_escape_string($_POST['email'])."'");
		
        if($mysqli->affected_rows != 1){
            throw new Exception('This email already exists in the database.');
        } else {   
          $email_code = add_code($mysqli->insert_id); 
        } 
		
		if( ! empty($referrer))
		{
			$mysqli->query("UPDATE coming_soon_emails SET signup = signup + 1 WHERE code='" . $referrer ."'");
		}
		//that doesn't work and does not update the signup column
		
		$msg = "http://www.exampleurl.com/" . $email_code;
		
		if($ajax){
			die(json_encode(array('msg' => $msg)));
		}



    }
    catch (Exception $e){

        if($ajax){
            die(json_encode(array('error'=>$e->getMessage())));
        }

        $msg = $e->getMessage();        
    }
}

Maybe an exception is being thrown. You have no way of knowing because in your catch block you don’t do anything with the exception if $ajax is false.

interesting! thanks aamonkey! how can I test that and check the possible cause?

In this block:

}
    catch (Exception $e){

        if($ajax){
            die(json_encode(array('error'=>$e->getMessage())));
        }

        $msg = $e->getMessage();        
    }

you are catching any thrown exceptions, and dieing with an error if the $ajax variable is false. If it’s not, you are assigning the exception message to the $msg variable, but that’s it. The script will continue.

…or maybe that’s not the entire script and you are echoing out the message later - don’t know…

hey aamonkey thanks again - $msg is echo’d out after form submit and it does not display any error :frowning:

found the issue :wink: now how do i fix this…

$referrer is blank during

if($_POST['email']){
  • though it’s not blank outside of that. why is this the case and how can i fix it?

thanks!

If anyone can sort this problematic issue it would be much appreciated!

Update is this…

In JS if I comment out the ajax call of

,'json')

and in index.php comment out the line


$ajax = ($_SERVER['HTTP_X_REQUESTED_WITH']  == 'XMLHttpRequest');

it then updates the DB correctly and all works well! :slight_smile:

However, now ajax no longer hides and shows the divs… so knowing that the issue is some AJAX interference, what can I do to sort it?

I’d love for the div fades to work and the DB update I was initially having issues with, to both work.

Thank you!


if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    ... it's an AJAX request so do something ...
}
else {
    ... it's not an AJAX request so do something else ...
}


I assume you had to to echo $_SERVER[‘HTTP_X_REQUESTED_WITH’] to determine the value?

Thanks tangoforce.
I don’t have to echo it as the only value being passed is ‘email’ (via $_POST). I’ll give your fix a shot now…

Thank you.

I would highly recommend echoing it purely for debug purposes just to that you can actually be sure of its value and that it is set.

Debugging is the very core of solving php problems - I frequently see peoples code this forum and the other one and will quickly test it on a site like writecodeonline.com and quickly run through it etc. Debugging is a wonderful skill to have :wink: It’s basically putting echo in strategic places in your script :smiley:

just so I know what I’m doing… (clearly a beginner here)


$ajax = ($_SERVER['HTTP_X_REQUESTED_WITH']  == 'XMLHttpRequest'); 

is replaced with

if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
echo $_SERVER['HTTP_X_REQUESTED_WITH'];
//other code below the $ajax line with an ending "}"

Or have I not followed your instructions correctly?

Thats pretty much it but I would suggest you echo it above the if conditional so that even if it doesn’t work for whatever reason you can find out what the value of it actually is. Again you only need to do this for debugging once its fixed you can scrap it.

Thanks for the explanation…

I have performed the following;

if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
echo $_SERVER['HTTP_X_REQUESTED_WITH'];
} else {
echo "not working";
}

if ($_POST['email']){
...

it echo’d “not working”. It then wouldn’t allow me to submit the email address in the form - it does nothing. So basically I can’y even tell once it submits what the value is of

$_SERVER['HTTP_X_REQUESTED_WITH'];

If I replace the echo “not working” in the else statement, with:

echo "server: " . $_SERVER['HTTP_X_REQUESTED_WITH'];

It just echo’s "server: " meaning it’s blank.

Same goes for if I echo it above the if (isset($_SERVER… statement…

What did I just say? :wink:


//I would suggest you echo it above the if conditional so that
//even if it doesn't work for whatever reason you can find out what the value of it actually is.

echo 'server: ' .$_SERVER['HTTP_X_REQUESTED_WITH'];

if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
//Your code here
}

If its coming up blank then it means either the browser you’re using isn’t using an xmlrequest (IE uses ActiveX and I’m not sure how that affects things) or you simply can’t use the HTTP_X_REQUESTED_WITH header (I understood X was used for proxies but I may be wrong).

:wink:

Just tested again and it outputs '"server: ", so it’s blank.
I’m using FF :slight_smile:

What can I do now? Or is this the end of the road?

Hmm…

I suspect you’re going to need to get creative on this one my friend :wink:

You could always use an ajax parameter in your urls - like mode=ajax and then test for that in your script instead.

damn.
as it stands, the url must be example.com/[REFERRAL CODE HERE], adding an additional variable in the url is something I’m trying to avoid…

PM me tomorrow evening (GMT) and download Teamviewer. We’ll have a desktop session and try to get it working.