Learjet Posted July 24, 2010 Share Posted July 24, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/208740-date-help-and-try-help/ Share on other sites More sharing options...
Mchl Posted July 24, 2010 Share Posted July 24, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/208740-date-help-and-try-help/#findComment-1090555 Share on other sites More sharing options...
Learjet Posted July 24, 2010 Author Share Posted July 24, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/208740-date-help-and-try-help/#findComment-1090648 Share on other sites More sharing options...
Pikachu2000 Posted July 24, 2010 Share Posted July 24, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/208740-date-help-and-try-help/#findComment-1090657 Share on other sites More sharing options...
Learjet Posted July 24, 2010 Author Share Posted July 24, 2010 Yes, I do And that is exactly what I needed, thanks! Still the issue of the mail() function. Quote Link to comment https://forums.phpfreaks.com/topic/208740-date-help-and-try-help/#findComment-1090662 Share on other sites More sharing options...
jcbones Posted July 24, 2010 Share Posted July 24, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/208740-date-help-and-try-help/#findComment-1090681 Share on other sites More sharing options...
Learjet Posted July 24, 2010 Author Share Posted July 24, 2010 That worked perfectly! All my issues are solved. Thanks everyone! Quote Link to comment https://forums.phpfreaks.com/topic/208740-date-help-and-try-help/#findComment-1090687 Share on other sites More sharing options...
cjohnweb Posted July 24, 2010 Share Posted July 24, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/208740-date-help-and-try-help/#findComment-1090693 Share on other sites More sharing options...
Pikachu2000 Posted July 24, 2010 Share Posted July 24, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/208740-date-help-and-try-help/#findComment-1090713 Share on other sites More sharing options...
cjohnweb Posted July 24, 2010 Share Posted July 24, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/208740-date-help-and-try-help/#findComment-1090716 Share on other sites More sharing options...
Mchl Posted July 24, 2010 Share Posted July 24, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/208740-date-help-and-try-help/#findComment-1090728 Share on other sites More sharing options...
cjohnweb Posted July 24, 2010 Share Posted July 24, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/208740-date-help-and-try-help/#findComment-1090751 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.