Jump to content

sometimes i hate php


Lambneck

Recommended Posts

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');
?>

Link to comment
Share on other sites

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');
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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');
?>

Link to comment
Share on other sites

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');
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 1 month later...

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.

Link to comment
Share on other sites

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.