# Check this PHP code for error.



## toofan (Oct 2, 2008)

> <?php
> if($_POST['submit'] == 'submit')
> {
> if(!$_POST['email'] || $_POST['email'] == "" || strlen ($_POST['email']) >30)
> ...



Friends Help me to debug this code. I don't know whats wrong with this code. Acc to me their is some problem with the code marked with blue.

Thanks friends.


----------



## Garbage (Oct 2, 2008)

I'm not sure about *interpolation in heredoc * Means ...

```
<FORM METHOD="post" ACTION="[b]$thisfile[/b]">
```
Not sure that the value will be put here... 
Check that !!


----------



## amitava82 (Oct 2, 2008)

```
<?php
if($_POST['submit'])
{
if(!$_POST['email'] || $_POST['email'] == "" || strlen ($_POST['email']) >30)
{
$message = "<p> Problem. Did you enter an email address?</p>"; 
}
else 
{
//open connection to database
mysql_connect("localhost", "root", "abc123") or die ("Failur to communication to database");

mysql_select_db("test") or die ("problem connecting to db. " .mysql_error());
//Insert email address
$as_email = addslashes($_POST['email']);
$tr_email = trim($as_email);
$query = "insert into mailinglist (ID, Email, Source)
values (null, '$tr_email', 'www.example.com/newletter_signup.html')";
$result = mysql_query($query);
if(mysql_affected_rows() == 1)
{
echo "<p> Your information has been recorded.</p>";
$noform_var = 1;
}
else 
{
error_log(mysql_error());
$message = "<p>Something went wrong with your signup attempt.</p>";
}
}
}

//show the form in every case except successful submission
//if (!$noform_var) {
$thisfile = $_SERVER['PHP_SELF'];
$message .= <<< EOMSG
<P>Enter your email address and we will send you our weekly newsletter.</P>
<FORM METHOD="post" ACTION="$thisfile">
<INPUT TYPE="text" SIZE=25 NAME="email">
<BR><BR>
<INPUT TYPE="submit" NAME="submit" VALUE="submit">
</FORM>
EOMSG;
//}

?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>email newsletter</title>
<style type="text/css">
<!--
body, p {
color:#000099; 
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:12pt
}
H1 {
color:#990000;
font-family:Arial, Helvetica, sans-serif;
font-size:14pt
}
-->
</style>
</head>
<body>

<table width="100%" border="0" cellspacing="0" cellpadding="10">
<tr>
<td width="17%" bgcolor="#f0f8ff">&nbsp;</td>
<td width="83%" align="center" valign="top" bgcolor="#fffff0"><h1>News Letter SignUp Form</h1>
<?php echo $message; ?>
</td>
</tr>
</table>


</body>
</html>
```
I think this is the correct code. Wrong placement of "}". Didn't check other stuffs though. Im in office now


----------



## toofan (Oct 2, 2008)

@amitava82 
Boss you are genius. Solved my problem in minutes. that's why i like this forum for having such an intelligent and helping peoples.
Now after the correction there is no need to change the following lines. 

if ($_POST['submit'] == 'Submit') to 
if($_POST['submit']) 

no need to hide this line 
 if (!$noform_var) {

its working well after that editing "}" sign and adding one at the last if statement.

thanks dear. Now can you tell me one thing 
what is the effect of these lines. 
$as_email = addslashes($_POST['email']);
$tr_email = trim($as_email);
why we used addslashes and trime.


----------



## Garbage (Oct 2, 2008)

lol... thats good...


----------



## amitava82 (Oct 2, 2008)

> string addslashes ( string $str )
> 
> Returns a string with backslashes before characters that need to be quoted in database queries etc. These characters are single quote ('), double quote ("), backslash (\) and NUL (the NULL byte).
> 
> ...



So if you have get_magic_quotes_gpc()  _on_ then you don't need to do addslashes(). Check the database entry if you have slash in email ID. If yes then remove addslashes(). Too lazy to type..


----------



## victor_rambo (Oct 2, 2008)

@Toofani,

Instead of posting the whole code, post the error. It hints us the likely location of error and we don't have to waste time reading the whole piece of code.

You will get error that look like:

```
Parse error: Unexpected T_VAR in /home/nai/scripts/contact.php on line 38
```
That helps us to hit the target more easily!


----------



## amitava82 (Oct 2, 2008)

Unfortunately there are no errors in his code. Its just an issue with his _if _condition. Try running his code, you won't get any error.


----------



## toofan (Oct 3, 2008)

@rohan_shenoy
If there will be any error next time and I can't solve it ,I will surely be posting it with code to check.

@amitava82
Thanks for your help dear.
Can you suggest me a good advance programming book in PHP. 
And after PHP what should I learn to make myself something.
I already know HTML, JAVASCRIPT,MACROMEDIA FLASH,MACROMEDIA FIREWORKS, CSS,MySql,  a bit of AJAX . tried learning XML but found it of no use and its very very confusing.
Can you guide me.


----------

