 |
02-10-2008, 07:33 PM
|
#1 (permalink)
|
|
Human Spambot
Join Date: May 2008
Location: Haldwani(Nainital)
Posts: 2,124
|
Check this PHP code for error.
Quote:
<?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"> </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.
|
|
|
|
Advertisements. Register and be a member of the community to get rid of them.
|
|
Advertisement
|
|
02-10-2008, 07:58 PM
|
#2 (permalink)
|
|
God of Mistakes...
Join Date: Dec 2005
Location: Pune, Maharashtra
Posts: 1,923
|
Re: Check this PHP code for error.
I'm not sure about interpolation in heredoc  Means ...
Code:
<FORM METHOD="post" ACTION="$thisfile">
Not sure that the value will be put here...
Check that !!
|
|
|
02-10-2008, 08:06 PM
|
#3 (permalink)
|
|
MMO Addict
Join Date: Jul 2004
Location: Bangalore
Posts: 1,474
|
Re: Check this PHP code for error.
PHP Code:
<?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"> </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
|
|
|
02-10-2008, 08:45 PM
|
#4 (permalink)
|
|
Human Spambot
Join Date: May 2008
Location: Haldwani(Nainital)
Posts: 2,124
|
Re: Check this PHP code for error.
@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.
|
|
|
02-10-2008, 08:52 PM
|
#5 (permalink)
|
|
God of Mistakes...
Join Date: Dec 2005
Location: Pune, Maharashtra
Posts: 1,923
|
Re: Check this PHP code for error.
lol... thats good...
|
|
|
02-10-2008, 09:23 PM
|
#6 (permalink)
|
|
MMO Addict
Join Date: Jul 2004
Location: Bangalore
Posts: 1,474
|
Re: Check this PHP code for error.
Quote:
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..
|
|
|
02-10-2008, 10:36 PM
|
#7 (permalink)
|
|
हॉर्न ओके प्लीज़
Join Date: Sep 2007
Posts: 1,493
|
Re: Check this PHP code for error.
@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!
__________________
विक्टर रॅंबो - चाणकया प्रभावित व्यक्ति
गीक होना माँगता
|
|
|
02-10-2008, 11:01 PM
|
#8 (permalink)
|
|
MMO Addict
Join Date: Jul 2004
Location: Bangalore
Posts: 1,474
|
Re: Check this PHP code for error.
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.
|
|
|
03-10-2008, 06:53 AM
|
#9 (permalink)
|
|
Human Spambot
Join Date: May 2008
Location: Haldwani(Nainital)
Posts: 2,124
|
Re: Check this PHP code for error.
@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.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|