Pb with php code returning blank page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ptityop
Forum Newbie
Posts: 13
Joined: Thu May 07, 2009 1:48 pm

Pb with php code returning blank page

Post by ptityop »

Hi,
I have a short script that allows to enter newsletter registrations in the database , but it does not want to work .... not sure what could be wrong . The script does work on my local machine but not when put on the server .. any help would be appreciated.
Many thanks

This is my script :

Code: Select all

<?php
include "config.php"; 
$nome=$_POST['nome']; 
$email=$_POST['email'];
$telefone=$_POST['telefone'];
if(!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $str)) {
    $msg = 'email is not valid';
}
else {
$valid = true;
}
$vr=mysql_query("select email from newsletter where email='$email'") or die(mysql_error());
if(mysql_num_rows($vr)>0){
echo"<script language='javascript'>
window.alert('E-mail já Cadastrado !');
window.history.go(-1);
</script>";
break;}
$cad=mysql_query("insert into newsletter (id, nome, email, telefone, id_grupo, id_hostel) values('', '$nome', '$email', '$telefone', '1', '') ") or die(mysql_error());
mysql_close();
echo"<script language='javascript'>
window.alert('Cadastro realizado com sucesso!');
window.location=('index.html');
</script>";
?>
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Pb with php code returning blank page

Post by twinedev »

Try debugging the code, use something like the following which is your code, the way I would have done it. Note the two MAIL functions to e-mail you the SQL that is actually being run. I purposely do NOT indent debug lines like this, so when I'm done working on code, I can easily find them and remove them.

Code: Select all

<script type="text/javascript"><?php
	require_once ('config.php');

	$nome = $_POST['nome'];
	$email = $_POST['email'];
	$telefone = $_POST['telefone'];

	$msg = FALSE;
	if(!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $email)) {
		$msg = 'email is not valid';
	}
	else {
		$SQL = 'SELECT `email` FROM `newsletter` WHERE `email`="'.mysql_real_escape_string($email).'" LIMIT 1';
mail ('your@email.com','Debug @ Line'.__LINE__,$SQL);
		$vr = mysql_query($SQL) or die (mysql_error());
		if ($vr && mysql_num_rows($vr)>0) {
			$msg = 'E-mail já Cadastrado !';
			mysql_free_result($vr);
		}
	}
	if ($msg) { // an error with e-mail, so let them know
		echo 'window.alert("'.$msg.'");',"\n";
		echo "window.history.go(-1);\n";
	}
	else {
		// data ok, try to insert, assuming ID is an autoincrement PK
		$SQL  = 'INSERT INTO `newsletter` (`id`, `nome`, `email`, `telefone`, `id_grupo`, `id_hostel`) ';
		$SQL .= sprintf('VALUES (NULL, "%s", "%s", "%s", "1", "")',
			mysql_real_escape_string($name),
			mysql_real_escape_string($email),
			mysql_real_escape_string($telefone));
mail ('your@email.com','Debug @ Line'.__LINE__,$SQL);
		$cad = mysql_query($SQL) or die(mysql_error());
		echo 'window.alert("Cadastro realizado com sucesso!");',"\n";
		echo 'window.location=("index.html");',"\n";
	}
?></script>
ptityop
Forum Newbie
Posts: 13
Joined: Thu May 07, 2009 1:48 pm

Re: Pb with php code returning blank page

Post by ptityop »

Hi thanks for the reply , I'm not sure i really understand what I should be doing with the code now ... not that expert :-) could you tell me a procedure I should be following if it is not too much to ask.. Many thanks
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Pb with php code returning blank page

Post by twinedev »

You would put that in place of the code you put here, replacing 'your@email.com' with your e-mail address, call the page, and then check your e-mail to get a copy of what SQL statement it was trying to run.

Then verify that those statements will run on the database by hand.

Also, you say it returns a "blank page", is that just visual, or when you do VIEW->SOURCE is that blank as well?

-Greg
ptityop
Forum Newbie
Posts: 13
Joined: Thu May 07, 2009 1:48 pm

Re: Pb with php code returning blank page

Post by ptityop »

Oh ok, so I did the right thing then , was not too sure ... but it had no effect still geting the same blank page and did not receive any email (even in spam folder), and the source code is empty as well ...
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Pb with php code returning blank page

Post by twinedev »

Well if you are using the code I gave, and the source is still empty, there is something besides that code that is wrong, as there should be output before it even processes actual PHP code.

Look though your error logs, see if it is reporting an error.

-Greg
ptityop
Forum Newbie
Posts: 13
Joined: Thu May 07, 2009 1:48 pm

Re: Pb with php code returning blank page

Post by ptityop »

Ok, thanks for your help, I guess the pb has to do with some hosting settings ... cannot get the error logs ..... thanks for your time though !!
Post Reply