ace2721 Posted December 16, 2008 Share Posted December 16, 2008 I need help guys, sorry I am new to php so forgive me if I don't explain this very well. I have created the contact script below so it submits to my mysql database and also sends an email to me. its working fine apart from one small problem, when You submit you enquiry online it duplicates the enquiry and sends two blank enquiry's as well. any help would be greatly appreciated Kraig <?php // Connect to database $hostname = "ip address"; $username = "username"; $password = "password"; $dbname = "database"; $usertable = "pitman"; $f_name = $_REQUEST['f_name']; $postcode = $_REQUEST['postcode']; $tel = $_REQUEST['tel']; $mobile = $_REQUEST['mobile']; $email = $_REQUEST['email']; $course = $_REQUEST['course']; mysql_connect($hostname, $username, $password) or DIE("Unable to connect to MySQL server $hostname"); $selected = mysql_select_db($dbname) or DIE("Could not select requested db $dbname"); $query = "INSERT INTO database (id, f_name, postcode, tel, mobile, email, course, pdate) VALUES ('', '$f_name', '$postcode', '$tel', '$mobile', '$email' , '$course', NOW())"; $results = mysql_query($query) or DIE ("could not execute query : $query." . mysql_error()); $result = mysql_query($query) or DIE("Could not Execute Query on table $usertable"); mysql_close; $errmsg = ''; // error message $f_name = ''; // sender's name $postcode = ''; // sender's name $tel = ''; // message tel $mobile = ''; // message tel $email = ''; // sender's email addres $course = ''; // the message itself if(isset($_POST['send'])) { $f_name = $_POST['f_name']; $postcode = $_POST['postcode']; $tel = $_POST['tel']; $mobile = $_POST['mobile']; $email = $_POST['email']; $course = $_POST['course']; if(trim($f_name) == '') { $errmsg = 'Please enter your name'; } else if(trim($postcode) == '') { $errmsg = 'Please enter your postcode'; } else if(trim($tel) == '') { $errmsg = 'Please enter Your Telephone Number:'; } else if(trim($mobile) == '') { $errmsg = 'Please enter your mobile'; } else if(trim($email) == '') { $errmsg = 'Please enter your email address'; } else if(!isEmail($email)) { $errmsg = 'Your email address is not valid'; } else if(trim($course) == '') { $errmsg = 'Please enter the course your interested in'; } if($errmsg == '') { if(get_magic_quotes_gpc()) { $tel = stripslashes($tel); $message = stripslashes($message); } // the email will be sent here $to = "my@domain"; $user_message .= "Name: $f_name \r\n"; $user_message .= "postcode: $postcode \r\n"; $user_message .= "tel: $tel \r\n"; $user_message .= "mobile: $mobile \r\n"; $user_message .= "email: $email \r\n"; $user_message .= "course: $course \r\n"; $headers = "- web Enquiry \r\n"; $headers .= "Reply-To: $email"; mail('my@domain.com','website Enquiry',$user_message,$headers); ?> <div align="center" class="boldenquire">Your message has been sent </div> <?php } } if(!isset($_POST['send']) || $errmsg != '') { ?> <div align="center" class="ermsg"><?=$errmsg;?></div> <form method="post" name="msgform" id="msgform"> <table border="0" align="center" cellpadding="2" cellspacing="1" class="maincell"> <tr> <td align="center" colspan="2"><span class="boldenquire"> Please fill in your details <br /> for more information.</span></td> </tr> <tr> <td>Your Name</td> <td><input name="f_name" type="text" class="eqform" value="<?=$f_name;?>" style="width:100px" /></td> </tr> <tr> <td>Nearest centre</td> <td><label> <select name="postcode" size="1" class="eqform" style="width:100px" value=" <?=$postcode;?>"> <option value="Ashford">Ashford</option> <option value="Bromley">Bromley</option> <option value="Chatham">Chatham</option> <option value="Dartford">Dartford</option> <option value="Gravesend">Gravesend</option> <option value="Maidstone">Maidstone</option> <option value="Tunbridge Wells">Tunbridge Wells</option> </select> </label></td> </tr> <tr> <td>Tel </td> <td><input name="tel" type="text" class="eqform" value="<?=$tel;?>" style="width:100px" /></td> </tr> <tr> <td>Mobile</td> <td><input name="mobile" type="text" class="eqform" value="<?=$mobile;?>" style="width:100px" /></td> </tr> <tr> <td>Email </td> <td><input name="email" type="text" class="eqform" value="<?=$email;?>" style="width:100px" /></td> </tr> <tr> <td>Course of interest </td> <td><input name="course" type="text" class="eqform" value="<?=$course;?>" style="width:100px" /> </td> </tr> <tr align="center"> <td colspan="2"><input name="send" type="submit" value="Send Message" onclick="return checkForm();" /></td> </tr> <tr align="left"> <td colspan="2"><br /> <script language="JavaScript"> var addr = ''; var host = ''; var email = ''; document.write(email); </script></td> </tr> </table> </form> <?php } function isEmail($email) { return(preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i" ,$email)); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/137180-solved-problem-with-php-contact-script/ Share on other sites More sharing options...
Adam Posted December 16, 2008 Share Posted December 16, 2008 You're querying the database twice: $results = mysql_query($query) or DIE ("could not execute query : $query." . mysql_error()); $result = mysql_query($query) or DIE("Could not Execute Query on table $usertable"); Use one or the other.. A Quote Link to comment https://forums.phpfreaks.com/topic/137180-solved-problem-with-php-contact-script/#findComment-716574 Share on other sites More sharing options...
gevans Posted December 16, 2008 Share Posted December 16, 2008 Also... mysql_close; is a function, it should be mysql_close(); Though you don't need to close mysql when not using a persistent connection Quote Link to comment https://forums.phpfreaks.com/topic/137180-solved-problem-with-php-contact-script/#findComment-716576 Share on other sites More sharing options...
ace2721 Posted December 16, 2008 Author Share Posted December 16, 2008 Guys you are legends thanks so much for your help I really need to start reading my books more, have a great day and merry christmas Quote Link to comment https://forums.phpfreaks.com/topic/137180-solved-problem-with-php-contact-script/#findComment-716589 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.