Jump to content

Recommended Posts

Hello all.

I'm a basic PHP coder, and I've been working on a simple site the past couple of days. I am having two issues that I would like to get figured out.

First, I have need to fix a mail() code.

if ($fromname=="" || $fromemail=="") {
echo "You must include your name and email address. <a href='javascript:history.go(-1)'Go back and try again.</a>";
}
else {
mail($to, $subject, $message, $headers);
echo "Your email has been sent! <a href='view.php?id=".$id."'>Return to the listing.</a>";
}

I would like to make it so if the mail() function doesn't work (eg they put in something for the email on the HTML form that gets submitted, but not an email address), then it wont return

Warning: mail() [function.mail]: SMTP server response: 550 5.1.0 <jon> domain name required in C:\xampp\htdocs\dtc\new\email2.php on line 21
Your email has been sent! Return to the listing.

saying the email sent when it actually didnt. I'm not sure of the best way to do this.

 

I also have an issue with my insert_query for submitting a listing.

	$connect=mysql_connect($host,$username,$password);
mysql_select_db("dtc",$connect) or die (mysql_errno().":<b> ".mysql_error()."</b>");
$insert_query = 'insert into listings (firstname,initial,location,password,email,have,lookingfor,dateposted,body)values
(
"' . $name . '",
"' . $initial . '",
"' . $location . '",
"' . $pass1 . '",
"' . $email . '",
"' . $have . '",
"' . $want . '",
"CURENT_DATE()",
"' . $body . '"
)';

That's my PHP code, and the dateposted is defined as date on the table structure, but when the row is added, the dateposted is always 0000-00-00. Any idea what is causing this?

Link to comment
https://forums.phpfreaks.com/topic/208740-date-help-and-try-help/
Share on other sites

You're inserting a string "CURENT_DATE()" instead of calling a function CURDATE()

 

Try like this:

 

$insert_query = "INSERT INTO
  listings (firstname,initial,location,password,email,have,lookingfor,dateposted,body)
VALUES (
  '$name','$initial','$location','$pass1','$email','$have','$want',CURDATE(),'$body'
)";

 

Ain't that looking better? ;)

 

 

That looks so much better :) Thank you!

Now, just need to fix the mail issue.

Also one more quick question. On the same page as the insert query, I had it just echoing "Your posting has been submitted." I wanted it to also include a link to the posting, and the only way I knew to do it was through another query that selected the row where the email, password, and body matched the email, password, and body submitted from the HTML form. While this shouldn't be a problem, there is a very small chance that there could be more than one matching row. Is there any other way to get the ID from the newly submitted row so I can guarantee it will return the right ID?

mysql_insert_id() after the iNSERT query will return the auto-incremented index of the table (you do have one, right?) for the last inserted record. You can pass that as a $_GET var in the URL to form a link to the post.

I hate using @, but this is as good of a case as any.

 

else {
if(!@mail($to, $subject, $message, $headers)) {
echo 'There was an error sending the email, please try again.';
}
else {
echo "Your email has been sent! <a href='view.php?id=".$id."'>Return to the listing.</a>";
}
}

 

You may not even have to use the @ symbol for this to suppress the error.

If you need to suppress errors but don't want to suppress all the error on the whole site / page you can place an "at" sign before the function.

 

Examples:

 

@mail($to,$sub,$msg,$headers);

 

$mail = @mail($to,$sub,$msg,$headers);
if($mail){echo "Email sent";}else{echo "email error";}

 

$url = "http://www.google.com/";
$content = @file_get_contents($url);
echo $content;

 

Good luck!

If you need to suppress errors but don't want to suppress all the error on the whole site / page you can place an "at" sign before the function.

 

Examples:

 

@mail($to,$sub,$msg,$headers);

 

$mail = @mail($to,$sub,$msg,$headers);
if($mail){echo "Email sent";}else{echo "email error";}

 

$url = "[url=http://www.google.com/]http://www.google.com/[/url]";
$content = @file_get_contents($url);
echo $content;

 

Good luck!

 

Yes, rather than fix the cause of any errors, just suppress the messages.  ::)

Well....yeah! I mean, if that's what the need of the script calls. For instance, I run xampp on my laptop for development, etc. I can't send emails so I suppress them cause they are ugly.

 

...Not to mention that I was specifically answering one of the original questions:

 

I would like to make it so if the mail() function doesn't work (eg they put in something for the email on the HTML form that gets submitted, but not an email address), then it wont return

Code: [select]

Warning: mail() [function.mail]: SMTP server response: 550 5.1.0 <jon> domain name required in C:\xampp\htdocs\dtc\new\email2.php on line 21

Your email has been sent! Return to the listing.saying the email sent when it actually didnt. I'm not sure of the best way to do this.

 

But for most circumstances, yes, you would want to fix the problem and not just cover it up ...otherwise stuff won't work, duh!

 

I actually just noticed that like 17 minutes before I actually posted someone else went in there and mentioned using @ to suppress errors. Damn, I was too late. Oh well.

A nicer way to do this is to convert all errors to exceptions as described here. This way we can catch the exception and handle it, silence it, or rethrow higher up.

 

I really like this, I never realized that you could handle errors like that. Worth checking out if you haven't already.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.