Check this PHP code for error.

Status
Not open for further replies.

toofan

Technomancer
<?php
if($_POST['submit'] == '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>

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

God of Mistakes...
I'm not sure about interpolation in heredoc :| Means ...
Code:
 <FORM METHOD="post" ACTION="[b]$thisfile[/b]">
Not sure that the value will be put here...
Check that !!
 

amitava82

MMO Addict
PHP:
<?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 :p
 
OP
toofan

toofan

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

amitava82

MMO Addict
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).

An example use of addslashes() is when you're entering data into a database. For example, to insert the name O'reilly into a database, you will need to escape it. Most databases do this with a \ which would mean O\'reilly. This would only be to get the data into the database, the extra \ will not be inserted. Having the PHP directive magic_quotes_sybase set to on will mean ' is instead escaped with another '.

The PHP directive magic_quotes_gpc is on by default, and it essentially runs addslashes() on all GET, POST, and COOKIE data. Do not use addslashes() on strings that have already been escaped with magic_quotes_gpc as you'll then do double escaping. The function get_magic_quotes_gpc() may come in handy for checking this.

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

victor_rambo

हॉर्न ओके प्लीज़
@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:
Code:
Parse error: Unexpected T_VAR in /home/nai/scripts/contact.php on line 38
That helps us to hit the target more easily!
 

amitava82

MMO Addict
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.
 
OP
toofan

toofan

Technomancer
@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.
 
Status
Not open for further replies.
Top Bottom