Jump to content

query validation question why it's not working


simcoweb

Recommended Posts

Ok, i'm trying to prevent certain information being inserted more than once into a mysql database. I'm using this code to validate against the 'date' field. If there's no entry then it runs the $sql INSERT:

 

<?
if ($err == "") {
			   $dupe = "SELECT * FROM picks WHERE date='$date'";
			   $dupe_results = mysql_query($dupe) or die(mysql_error());
			   $num_rows = mysql_num_rows($dupe_results);
			   if ($num_rows == 1) {
			     echo "There are already picks entered for this date: $date . If you wish to edit them then select Display Picks and choose the date you wish to edit.<br>\n";
				} else {
			   $sql = "INSERT INTO picks (step1, step2, step3, step4, date) VALUES ( '$step1', '$step2', '$step3', '$step4', '$date')";
			   $results = mysql_query($sql) or die(mysql_error());
			   
				}
			 }
?>

 

Problem is it's not working. I have the $date variable set like this:

 

$date  = date("m/d/Y");

 

Which returns todays date. Maybe i'm missing something but it appears that it either won't validate against that date format ( 01/31/2007 ) or my code is wrong.

Link to comment
Share on other sites

I don't get any errors and i'm using a varchar format for the field instead of the mysql 'date' format.

 

That's what I can't understand is why it's bypassing the validation without an error and simply running the 2nd query. There may be something else I have to validate against but there's nothing 'unique' except an id that's auto-incremented for each entry. But, that's also based upon the date as well as each day should be one entry. Therefore one id per day.

Link to comment
Share on other sites

That did the trick! Just for my edification, why wouldn't the == work as opposed to the >= ?

 

And, this may be related to my lack of expertise as well, but this isn't functioning as it logically should.

 

<?
		if(!results ) {
			     echo "Could not enter new picks into database. Please re-enter.<br>\n";
				} else {
				 echo "<font color='#0000FF'>New picks successfully entered for $date.<br>\n";

				   echo "<ul><li>Step 1: $step1</li>\n
				             <li>Step 2: $step2</li>\n
							 <li>Step 3: $step3</li>\n
							 <li>Step 4: $step4</li>\n
							 </ul><br>\n";
				}
			?>

 

It's displaying the <ul> stuff whether or not the query for inserting it was successful.

Link to comment
Share on other sites

Grrr... ok, this code controls the form validation and mysql insertion/validation/check. What's happening is when I click on the menu item 'Add Picks' which is this page/form it's automatically submitting the form instead of waiting for it to be completed. Therefore, it's inserting blank fields into the database with the exception of the date. I can't spot where i'm missing the right sequence to stop this from happening:

 

<?
				$v_sbm=$_REQUEST['submit'];  
				$connection = mysql_connect($dbhost, $dbusername, $dbpass);
				$SelectedDB = mysql_select_db($dbname);
			// set our POST variables
			$step1 = mysql_real_escape_string($_POST['step1']);
			$step2 = mysql_real_escape_string($_POST['step2']);
			$step3 = mysql_real_escape_string($_POST['step3']);
			$step4 = mysql_real_escape_string($_POST['step4']);
			$date  = date("m/d/Y");
			$err   = "";
			$err_dupe = "";

				// validate data
			if (isset($v_sbm)) {
			  if(trim($step1)== "") {
			  $err .="Step One cannot be blank.<br>";
			  }    
			  if(trim($step2)== "") {
			  $err .="Step Two cannot be blank.<br>";
			  }
			  if(trim($step3) == "") {
			  $err .="Step Three cannot be blank.<br>";
			  }
			  if(trim($step4) == "") {
			  $err .="Step Four cannot be blank.<br>";
			} 
		} 
			 if (empty($err)){
			   $dupe = "SELECT * FROM picks WHERE date='$date'";
			   $dupe_results = mysql_query($dupe) or die(mysql_error());
			   $num_rows = mysql_num_rows($dupe_results);
			   if ($num_rows >= 1) {
			    $err_dupe .="There are already picks entered for this date: $date . If you wish to edit them then select Display Picks and choose the date you wish to edit.<br>\n";
			    
				} else {
				  if (empty($err_dupe)){
			   $sql = "INSERT INTO picks (step1, step2, step3, step4, date) VALUES ( '$step1', '$step2', '$step3', '$step4', '$date')";
			   $results = mysql_query($sql) or die(mysql_error());
			   }
				}
			 }
?>

 

To recap, in my 'admin' area there's several links and the one pointing to this one is Add Picks (points to picks_add.php which this code is part of). Immediately upon displaying the page it also displays the echoed results (parsed from this code inserted into the body of the form)

 

<?
if(!$results) {
     echo "Could not enter new picks into database. Please re-enter.<br>\n";
} else {
     echo "<font color='#0000FF'>New picks successfully entered for $date.<br>\n";

 echo "<ul><li>Step 1: $step1</li>\n
          <li>Step 2: $step2</li>\n
	 <li>Step 3: $step3</li>\n
	 <li>Step 4: $step4</li>\n
	 </ul><br>\n";
	}
?>

 

None of this should happen until the Submit button is pressed :(

 

 

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.