simcoweb Posted January 31, 2007 Share Posted January 31, 2007 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 https://forums.phpfreaks.com/topic/36512-query-validation-question-why-its-not-working/ Share on other sites More sharing options...
Jessica Posted January 31, 2007 Share Posted January 31, 2007 Do you get an error? You know the mysql DATE format is YYYY-MM-DD, so this won't work unless your date field is a varchar formatted that way. Link to comment https://forums.phpfreaks.com/topic/36512-query-validation-question-why-its-not-working/#findComment-173842 Share on other sites More sharing options...
simcoweb Posted January 31, 2007 Author Share Posted January 31, 2007 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 https://forums.phpfreaks.com/topic/36512-query-validation-question-why-its-not-working/#findComment-173844 Share on other sites More sharing options...
Jessica Posted January 31, 2007 Share Posted January 31, 2007 Try if ($num_rows >= 1) { Link to comment https://forums.phpfreaks.com/topic/36512-query-validation-question-why-its-not-working/#findComment-173845 Share on other sites More sharing options...
simcoweb Posted January 31, 2007 Author Share Posted January 31, 2007 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 https://forums.phpfreaks.com/topic/36512-query-validation-question-why-its-not-working/#findComment-173857 Share on other sites More sharing options...
simcoweb Posted January 31, 2007 Author Share Posted January 31, 2007 Never mind on that! I found it like 10 seconds after I posted. I had (!results) when it should be (!$results). Link to comment https://forums.phpfreaks.com/topic/36512-query-validation-question-why-its-not-working/#findComment-173860 Share on other sites More sharing options...
simcoweb Posted January 31, 2007 Author Share Posted January 31, 2007 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 https://forums.phpfreaks.com/topic/36512-query-validation-question-why-its-not-working/#findComment-173890 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.