Lambneck Posted October 31, 2008 Share Posted October 31, 2008 I hate to post a problem without any details but unfortunately I cant find anything wrong with the code and don't know why its not working: <?php $connect = mysql_connect($db_host, $db_user, $db_pwd); mysql_select_db($database); $country = check_input($_POST['country'], "Please choose a country."); $compname = check_input($_POST['companyname'], "Please enter your company's name."); $email = htmlspecialchars($_POST['email']); if (! preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email) ) { show_error('E-mail address not valid.'); } $subject = check_input($_POST['subject'], "Please enter a subject."); $message = check_input($_POST['message'], "Please enter your advertisement."); $ip = $_SERVER['REMOTE_ADDR']; $timeout = 20; // SPECIFICALLY THIS FOLLOWING CODE IS WHERE THE TROUBLE MUST BE: $result = mysql_query("SELECT submission_date FROM $table WHERE ip_address = '$ip' ORDER BY submission_date DESC LIMIT 1") or die('Error'); if(mysql_num_rows($result) == 1){ $info = mysql_fetch_array($result); if(time() - $info['submission_date'] > $timeout * 60) $insert = mysql_query("INSERT INTO $table (col_1, col_2, col_3, col_4, col_5, submission_date, ip_address) VALUES ('$country', '$compname', '$email', '$subject', '$message', ".time().", '$ip')"); else die("Please submit no more than one job ad per day."); } } header('Location: ../advertThanks.php'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/130843-sometimes-i-hate-php/ Share on other sites More sharing options...
PFMaBiSmAd Posted October 31, 2008 Share Posted October 31, 2008 At least tell us what it is or is not doing vs what it is supposed to be doing. What do you see in front of you when you try it? Quote Link to comment https://forums.phpfreaks.com/topic/130843-sometimes-i-hate-php/#findComment-679106 Share on other sites More sharing options...
JasonLewis Posted October 31, 2008 Share Posted October 31, 2008 Is $info['submission_date'] a Unix timestamp? If it is a date such as 0000-00-00 00:00:00 then subtracting it from time() would not be working out like you expect it to. Quote Link to comment https://forums.phpfreaks.com/topic/130843-sometimes-i-hate-php/#findComment-679111 Share on other sites More sharing options...
Lambneck Posted October 31, 2008 Author Share Posted October 31, 2008 what im attempting is to set a limit on how many form submissions a user can make during a certain period of time. I used the following code for one form and it works fine: <?php $connect = mysql_connect($db_host, $db_user, $db_pwd); mysql_select_db($database); $name = check_input($_POST['name'], "Please enter your name."); $email = htmlspecialchars($_POST['email']); if (! preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email) ) { show_error('E-mail address not valid.'); } $subject = check_input($_POST['subject'], "Please enter a subject."); $message = check_input($_POST['message'], "Please enter your resume."); $ip = $_SERVER['REMOTE_ADDR']; $timeout = 20; $result = mysql_query("SELECT submission_date FROM $table WHERE ip_address = '$ip' ORDER BY submission_date DESC LIMIT 1") or die('Error'); if(mysql_num_rows($result) == 1){ $info = mysql_fetch_array($result); if(time() - $info['submission_date'] > $timeout * 60) $insert = mysql_query("INSERT INTO $table (col_2, col_3, col_4, col_5, submission_date, ip_address) VALUES ('$name', '$email', '$subject', '$message', ".time().", '$ip')"); else die("Please submit only one resume per month."); } } header('Location: ../resThanks.php'); ?> But when I use it again for a slightly different form it doesn't work: <?php $connect = mysql_connect($db_host, $db_user, $db_pwd); mysql_select_db($database); $country = check_input($_POST['country'], "Please choose a country."); $compname = check_input($_POST['companyname'], "Please enter your company's name."); $email = htmlspecialchars($_POST['email']); if (! preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email) ) { show_error('E-mail address not valid.'); } $subject = check_input($_POST['subject'], "Please enter a subject."); $message = check_input($_POST['message'], "Please enter your advertisement."); $ip = $_SERVER['REMOTE_ADDR']; $timeout = 20; $result = mysql_query("SELECT submission_date FROM $table WHERE ip_address = '$ip' ORDER BY submission_date DESC LIMIT 1") or die('Error'); if(mysql_num_rows($result) == 1){ $info = mysql_fetch_array($result); if(time() - $info['submission_date'] > $timeout * 60) $insert = mysql_query("INSERT INTO $table (col_1, col_2, col_3, col_4, col_5, submission_date, ip_address) VALUES ('$country', '$compname', '$email', '$subject', '$message', ".time().", '$ip')"); else die("Please submit no more than one job ad per day."); } } header('Location: ../advertThanks.php'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/130843-sometimes-i-hate-php/#findComment-679117 Share on other sites More sharing options...
Lambneck Posted October 31, 2008 Author Share Posted October 31, 2008 At least tell us what it is or is not doing vs what it is supposed to be doing. What do you see in front of you when you try it? What it is doing is not sending form data to the database. What it is supposed to do is only allow a user to submit the form once every 20 minutes. When I test the code nothing shows up in the database after a form submission. Quote Link to comment https://forums.phpfreaks.com/topic/130843-sometimes-i-hate-php/#findComment-679126 Share on other sites More sharing options...
sloth456 Posted October 31, 2008 Share Posted October 31, 2008 Could it be a syntax problem in your insert statement shouldn't ,".time().", be ,'".time()."', Quote Link to comment https://forums.phpfreaks.com/topic/130843-sometimes-i-hate-php/#findComment-679189 Share on other sites More sharing options...
samshel Posted October 31, 2008 Share Posted October 31, 2008 user or die(mysql_error()); instead of or die('Error'); Also add it for insert statement. if the problem is with query, it will show this... Quote Link to comment https://forums.phpfreaks.com/topic/130843-sometimes-i-hate-php/#findComment-679201 Share on other sites More sharing options...
Lambneck Posted October 31, 2008 Author Share Posted October 31, 2008 user Code: [select] or die(mysql_error()); instead of Code: [select] or die('Error'); Also add it for insert statement. if the problem is with query, it will show this... Actually, when the form is processed it takes you to the "Thank you" page: <?php header('Location: ../advertThanks.php'); ?> However, nothing is sent to the database... How is that possible? Quote Link to comment https://forums.phpfreaks.com/topic/130843-sometimes-i-hate-php/#findComment-679273 Share on other sites More sharing options...
waynew Posted October 31, 2008 Share Posted October 31, 2008 Please try and tell us if you see an error. <?php $connect = mysql_connect($db_host, $db_user, $db_pwd); mysql_select_db($database); $name = check_input($_POST['name'], "Please enter your name."); $email = htmlspecialchars($_POST['email']); if (! preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email) ) { show_error('E-mail address not valid.'); } $subject = check_input($_POST['subject'], "Please enter a subject."); $message = check_input($_POST['message'], "Please enter your resume."); $ip = $_SERVER['REMOTE_ADDR']; $timeout = 20; $result = mysql_query("SELECT submission_date FROM $table WHERE ip_address = '$ip' ORDER BY submission_date DESC LIMIT 1") or die('Error'); if(mysql_num_rows($result) == 1){ $info = mysql_fetch_array($result); if(time() - $info['submission_date'] > $timeout * 60) $insert = mysql_query("INSERT INTO $table (col_2, col_3, col_4, col_5, submission_date, ip_address) VALUES ('$name', '$email', '$subject', '$message', ".time().", '$ip')") or die(mysql_error()); else die("Please submit only one resume per month."); } } header('Location: ../resThanks.php'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/130843-sometimes-i-hate-php/#findComment-679276 Share on other sites More sharing options...
Lambneck Posted October 31, 2008 Author Share Posted October 31, 2008 I am using the following code, and when it processes the form it takes me to the Thank You page as it is supposed to with no errors. But the problem is that the information is not sent to the database. <?php $connect = mysql_connect($db_host, $db_user, $db_pwd); mysql_select_db($database); $country = check_input($_POST['country'], "Please choose a country."); $compname = check_input($_POST['companyname'], "Please enter your company's name."); $email = htmlspecialchars($_POST['email']); if (! preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email) ) { show_error('E-mail address not valid.'); } $subject = check_input($_POST['subject'], "Please enter a subject."); $message = check_input($_POST['message'], "Please enter your advertisement."); $ip = $_SERVER['REMOTE_ADDR']; $timeout = 20; $result = mysql_query("SELECT submission_date FROM $table WHERE ip_address = '$ip' ORDER BY submission_date DESC LIMIT 1") or die(mysql_error()); if(mysql_num_rows($result) == 1){ $info = mysql_fetch_array($result); if(time() - $info['submission_date'] > $timeout * 60) $insert = mysql_query("INSERT INTO $table (col_1, col_2, col_3, col_4, col_5, submission_date, ip_address) VALUES ('$country', '$compname', '$email', '$subject', '$message', ".time().", '$ip')") or die(mysql_error()); else die("Please submit no more than one job ad per day."); } } header('Location: ../advertThanks.php'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/130843-sometimes-i-hate-php/#findComment-679285 Share on other sites More sharing options...
JasonLewis Posted October 31, 2008 Share Posted October 31, 2008 The if() statement where it checks time is most probably failing. Remove the header() and do some error checking on the variables. Perhaps you have your logic wrong. Quote Link to comment https://forums.phpfreaks.com/topic/130843-sometimes-i-hate-php/#findComment-679290 Share on other sites More sharing options...
Lambneck Posted October 31, 2008 Author Share Posted October 31, 2008 There are no errors when I remove header() ...just a blank page. As I said earlier I use almost the exact same code on another form and it works perfectly... I don't understand why it's not working with this one. ??? Quote Link to comment https://forums.phpfreaks.com/topic/130843-sometimes-i-hate-php/#findComment-679330 Share on other sites More sharing options...
Andy82 Posted October 31, 2008 Share Posted October 31, 2008 I hate to post a problem without any details but unfortunately I cant find anything wrong with the code and don't know why its not working: <?php $connect = mysql_connect($db_host, $db_user, $db_pwd); mysql_select_db($database); $country = check_input($_POST['country'], "Please choose a country."); $compname = check_input($_POST['companyname'], "Please enter your company's name."); $email = htmlspecialchars($_POST['email']); if (! preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email) ) { show_error('E-mail address not valid.'); } $subject = check_input($_POST['subject'], "Please enter a subject."); $message = check_input($_POST['message'], "Please enter your advertisement."); $ip = $_SERVER['REMOTE_ADDR']; $timeout = 20; // SPECIFICALLY THIS FOLLOWING CODE IS WHERE THE TROUBLE MUST BE: $result = mysql_query("SELECT submission_date FROM $table WHERE ip_address = '$ip' ORDER BY submission_date DESC LIMIT 1") or die('Error'); if(mysql_num_rows($result) == 1){ $info = mysql_fetch_array($result); if(time() - $info['submission_date'] > $timeout * 60) $insert = mysql_query("INSERT INTO $table (col_1, col_2, col_3, col_4, col_5, submission_date, ip_address) VALUES ('$country', '$compname', '$email', '$subject', '$message', ".time().", '$ip')"); else die("Please submit no more than one job ad per day."); } } header('Location: ../advertThanks.php'); ?> It looks like you have a mismatch in {} of the IF ELSE statement. Quote Link to comment https://forums.phpfreaks.com/topic/130843-sometimes-i-hate-php/#findComment-679455 Share on other sites More sharing options...
Lambneck Posted November 1, 2008 Author Share Posted November 1, 2008 Andy82 thanks, you are right there is an extra curly bracket, however that was opened in a portion of the script that I didnt include in the initial post: <?php if ( isset($_POST['submit']) ) { ?> Still stumped :-\ Quote Link to comment https://forums.phpfreaks.com/topic/130843-sometimes-i-hate-php/#findComment-679793 Share on other sites More sharing options...
sKunKbad Posted November 1, 2008 Share Posted November 1, 2008 "sometimes i hate php" ... That made me laugh. I imagined someone walking in to a bar full of KKK members and saying, "I hate white people!" Quote Link to comment https://forums.phpfreaks.com/topic/130843-sometimes-i-hate-php/#findComment-679855 Share on other sites More sharing options...
Lambneck Posted November 1, 2008 Author Share Posted November 1, 2008 That's a helpful analogy. So in your eyes us phpfreaks are comparable to hateful bigots like those of the kkk? Anyway I appreciate you injecting your personal feelings about this post's title, however I would prefer to stay on topic if possible. Please save the personal reflection for your blog. Quote Link to comment https://forums.phpfreaks.com/topic/130843-sometimes-i-hate-php/#findComment-679926 Share on other sites More sharing options...
Lambneck Posted November 6, 2008 Author Share Posted November 6, 2008 BUMP Quote Link to comment https://forums.phpfreaks.com/topic/130843-sometimes-i-hate-php/#findComment-683786 Share on other sites More sharing options...
sloth456 Posted December 18, 2008 Share Posted December 18, 2008 I know this is kinda late, but I noticed your database selection statement looks a little odd. you've put: mysql_select_db($database); should it not be mysql_select_db($database,$connect); To be able to select a database you must pass it the connection too. Hope this solves the problem. Quote Link to comment https://forums.phpfreaks.com/topic/130843-sometimes-i-hate-php/#findComment-719266 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.