nezbo Posted September 26, 2007 Share Posted September 26, 2007 Hi I have the following code and it was working fine untill i added the dection where it check that database of exists and says you can't add twice. $getDayOfTheWeek = mysql_query("SELECT * FROM _daysoftheweek ORDER BY Priorty ASC"); $bankholidays = mysql_query("SELECT * FROM _bankholidays ORDER BY bankHolidayDate ASC"); echo "<table border=1 align=center><tr><td colspan=7><h2>Bank Holidays</h2></td></tr>"; while ($bh = mysql_fetch_array($bankholidays)) { echo "</td><td>" . $bh['bankHolidayDate'] . "</td><td>" . $bh['bankHolidayStartTime'] . "</td><td>" . $bh['bankHolidayEndTime'] . "</td><td>" . $bh['bankHolidayDisc'] . "</td><td>" . $bh['bankHolidayDay'] . "</td><td>" . $bh['bankHolidayMult'] . "</td></tr>"; } echo "</table>"; if (!$_POST['submit']) { echo "<form name=form1 method=POST action=" . $_SERVER['PHP_SELF'] . ">"; echo "<table align=center border=0><tr><td colspan=2><h2>Add bankholiday</h2></td></tr>"; echo "<tr><td align=left>Enter Bankholiday Date :</td><td align=left><input type=text maxlength=2 size=2 name=bhDateDay /> / <input type=text maxlength=2 size=2 name=bhDateMonth /> / <input type=text maxlength=4 size=4 name=bhDateYear /></td></tr>"; echo "<tr><td align=left>Enter Bankholiday Start Time :</td><td align=left><input type=text maxlength=4 size=4 name=bhStime /></td></tr>"; echo "<tr><td align=left>Enter Bankholiday End Time :</td><td align=left><input type=text maxlength=4 size=4 name=bhEtime /></td></tr>"; echo "<tr><td align=left>Enter Bankholiday Day :</td><td align=left><select name=bhDay><option selected=selected value=''>"; while($getDay = mysql_fetch_array($getDayOfTheWeek)) { echo "<option value=\"" . $getDay['dayOfTheWeek'] . "\">" . $getDay['dayOfTheWeek'] . "</option>"; } echo "</td></tr>"; echo "<tr><td align=left>Enter Bankholiday Discription :</td><td align=left><input type=text name=bhDisc /></td></tr>"; echo "<tr><td align=left>Enter Bankholiday Pay Multiplier :</td><td align=left><input type=text name=bhMult /></td></tr>"; echo "<tr><td colspan=2><h2><input type=submit name=submit value=Add bankholiday /></h2></td></tr>"; echo "</table></form>"; } else { if ($_POST['bhDateYear'] < '10'){ $bhYear = '0' . $_POST['bhDateYear'] + '2000'; } if ($_POST['bhDateYear'] < '10'){ $bhMonth = '0' . $_POST['bhDateMonth']; } if ($_POST['bhDateYear'] < '99'){ $bhDay = '0' . $_POST['bhDateDay']; } $bhDate = $bhYear . $bhMonth . $bhDay; $bhDay = $_POST['bhDay']; $bhbhDisc = $_POST['bhDisc']; $bhMult = $_POST['bhMult']; $bhStime = $_POST['bhStime'] . '00'; $bhEtime = $_POST['bhEtime'] . '00'; $one = mysql_num_rows(mysql_query("SELECT * FROM _bankholidays WHERE bankHolidayStartTime='$bhStime' AND bankHolidayEndTime='$bhEtime' AND bankHolidayValid='1' AND bankHolidayDate='$bhDate' AND bankHolidayDisc='$bhDisc' AND bankHolidayDay='$bhDay' AND bankHolidayMult='$bhMult' ")) or die(mysql_error()); if($one == 0){ $addBankHolidays = "INSERT INTO _bankholidays (bankHolidayStartTime, bankHolidayEndTime, bankHolidayID, bankHolidayValid, bankHolidayDate, bankHolidayDisc, bankHolidayDay, bankHolidayMult) VALUES ('$bhStime', '$bhEtime', '', '1', '$bhDate', '$bhDisc', '$bhDay', '$bhMult')"; if (!mysql_query($addBankHolidays,$con)) { die('eeek Error : ' . mysql_error()); } echo "<div align=center><h3>The record has been added.</h3></div>"; } else{ echo "<div align=center><h3>Sorry you can add a row twice.</h3></div>"; } } This is the part of the code that i added :: if($one == 0){ $addBankHolidays = "INSERT INTO _bankholidays (bankHolidayStartTime, bankHolidayEndTime, bankHolidayID, bankHolidayValid, bankHolidayDate, bankHolidayDisc, bankHolidayDay, bankHolidayMult) VALUES ('$bhStime', '$bhEtime', '', '1', '$bhDate', '$bhDisc', '$bhDay', '$bhMult')"; if (!mysql_query($addBankHolidays,$con)) { die('eeek Error : ' . mysql_error()); } echo "<div align=center><h3>The record has been added.</h3></div>"; } else{ echo "<div align=center><h3>Sorry you can add a row twice.</h3></div>"; } The original working code looked like this :: $addBankHolidays = "INSERT INTO _bankholidays (bankHolidayStartTime, bankHolidayEndTime, bankHolidayID, bankHolidayValid, bankHolidayDate, bankHolidayDisc, bankHolidayDay, bankHolidayMult) VALUES ('$bhStime', '$bhEtime', '', '1', '$bhDate', '$bhDisc', '$bhDay', '$bhMult')"; if (!mysql_query($addBankHolidays,$con)) { die('eeek Error : ' . mysql_error()); } echo "<div align=center><h3>The record has been added.</h3></div>"; the problem that i am having is that it looks like it just dosent do the insert into part... hope this makes sense. Neil Quote Link to comment https://forums.phpfreaks.com/topic/70729-solved-insert-into-with-check-problem/ Share on other sites More sharing options...
hemlata Posted September 26, 2007 Share Posted September 26, 2007 Hello, Have you check what value you are getting for $one variable? Regards, Quote Link to comment https://forums.phpfreaks.com/topic/70729-solved-insert-into-with-check-problem/#findComment-355551 Share on other sites More sharing options...
beboo002 Posted September 26, 2007 Share Posted September 26, 2007 yes plz check the value of $one. Quote Link to comment https://forums.phpfreaks.com/topic/70729-solved-insert-into-with-check-problem/#findComment-355553 Share on other sites More sharing options...
nezbo Posted September 26, 2007 Author Share Posted September 26, 2007 ok, i have check the $one value and it is the correct value. but it dosent even display the "Sorry you can add a row twice." whitch i would expect if the value is anything but 0. I have comented out some code and it adds to the database... /* $one = mysql_num_rows(mysql_query("SELECT * FROM _bankholidays WHERE bankHolidayStartTime='$bhStime' AND bankHolidayEndTime='$bhEtime' AND bankHolidayValid='1' AND bankHolidayDate='$bhDate' AND bankHolidayDisc='$bhDisc' AND bankHolidayDay='$bhDay' AND bankHolidayMult='$bhMult' ")) or die(mysql_error()); if($one == 0) { */ $addBankHolidays = "INSERT INTO _bankholidays (bankHolidayStartTime, bankHolidayEndTime, bankHolidayID, bankHolidayValid, bankHolidayDate, bankHolidayDisc, bankHolidayDay, bankHolidayMult) VALUES ('$bhStime', '$bhEtime', '', '1', '$bhDate', '$bhDisc', '$bhDay', '$bhMult')"; if (!mysql_query($addBankHolidays,$con)) { die('eeek Error : ' . mysql_error()); } echo "<div align=center><h3>The record has been added.</h3></div>"; /* } else { echo "<div align=center><h3>Sorry you can add a row twice.</h3></div>"; } */ i have also tryed to add the same thing twice and it work as expected, (when the code is not comented out) but dosent add to the database. cheers Quote Link to comment https://forums.phpfreaks.com/topic/70729-solved-insert-into-with-check-problem/#findComment-355562 Share on other sites More sharing options...
nezbo Posted September 26, 2007 Author Share Posted September 26, 2007 i have just dubble checked the value of $one and it has no value that confuses me now ??? Quote Link to comment https://forums.phpfreaks.com/topic/70729-solved-insert-into-with-check-problem/#findComment-355564 Share on other sites More sharing options...
nezbo Posted September 26, 2007 Author Share Posted September 26, 2007 it is still like it is not adding the insert queary ? Quote Link to comment https://forums.phpfreaks.com/topic/70729-solved-insert-into-with-check-problem/#findComment-355565 Share on other sites More sharing options...
beboo002 Posted September 26, 2007 Share Posted September 26, 2007 echo the value of $ one what it is. Quote Link to comment https://forums.phpfreaks.com/topic/70729-solved-insert-into-with-check-problem/#findComment-355567 Share on other sites More sharing options...
nezbo Posted September 26, 2007 Author Share Posted September 26, 2007 i echo $one when i am trying to do multiple enetryes and i get the result of 1 but when i enter values that are unque that should add a value the value of $one = nul Quote Link to comment https://forums.phpfreaks.com/topic/70729-solved-insert-into-with-check-problem/#findComment-355569 Share on other sites More sharing options...
hemlata Posted September 26, 2007 Share Posted September 26, 2007 Hello, One possible solution to your problem could be.. $query_result = mysql_query("SELECT * FROM _bankholidays WHERE bankHolidayStartTime='$bhStime' AND bankHolidayEndTime='$bhEtime' AND bankHolidayValid='1' AND bankHolidayDate='$bhDate' AND bankHolidayDisc='$bhDisc' AND bankHolidayDay='$bhDay' AND bankHolidayMult='$bhMult'") or die(mysql_error()); $one = mysql_num_rows($query_result); if(!$one) { $addBankHolidays = "INSERT INTO _bankholidays (bankHolidayStartTime, bankHolidayEndTime, bankHolidayID, bankHolidayValid, bankHolidayDate, bankHolidayDisc, bankHolidayDay, bankHolidayMult) VALUES ('$bhStime', '$bhEtime', '', '1', '$bhDate', '$bhDisc', '$bhDay', '$bhMult')"; if (!mysql_query($addBankHolidays,$con)) { die('eeek Error : ' . mysql_error()); } echo "<div align=center><h3>The record has been added.</h3></div>"; } else { echo "<div align=center><h3>Sorry you can add a row twice.</h3></div>"; } Hope this will solve your problem. Regards, Quote Link to comment https://forums.phpfreaks.com/topic/70729-solved-insert-into-with-check-problem/#findComment-355586 Share on other sites More sharing options...
beboo002 Posted September 26, 2007 Share Posted September 26, 2007 try this code if($one > 1) { echo "<div align=center><h3>Sorry you can add a row twice.</h3></div>"; } else { */ $addBankHolidays = "INSERT INTO _bankholidays (bankHolidayStartTime, bankHolidayEndTime, bankHolidayID, bankHolidayValid, bankHolidayDate, bankHolidayDisc, bankHolidayDay, bankHolidayMult) VALUES ('$bhStime', '$bhEtime', '', '1', '$bhDate', '$bhDisc', '$bhDay', '$bhMult')"; if (!mysql_query($addBankHolidays,$con)) { die('eeek Error : ' . mysql_error()); } echo "<div align=center><h3>The record has been added.</h3></div>"; /* } Quote Link to comment https://forums.phpfreaks.com/topic/70729-solved-insert-into-with-check-problem/#findComment-355587 Share on other sites More sharing options...
nezbo Posted September 26, 2007 Author Share Posted September 26, 2007 Cheers that worked a treat you are a star... Neil Hello, One possible solution to your problem could be.. $query_result = mysql_query("SELECT * FROM _bankholidays WHERE bankHolidayStartTime='$bhStime' AND bankHolidayEndTime='$bhEtime' AND bankHolidayValid='1' AND bankHolidayDate='$bhDate' AND bankHolidayDisc='$bhDisc' AND bankHolidayDay='$bhDay' AND bankHolidayMult='$bhMult'") or die(mysql_error()); $one = mysql_num_rows($query_result); if(!$one) { $addBankHolidays = "INSERT INTO _bankholidays (bankHolidayStartTime, bankHolidayEndTime, bankHolidayID, bankHolidayValid, bankHolidayDate, bankHolidayDisc, bankHolidayDay, bankHolidayMult) VALUES ('$bhStime', '$bhEtime', '', '1', '$bhDate', '$bhDisc', '$bhDay', '$bhMult')"; if (!mysql_query($addBankHolidays,$con)) { die('eeek Error : ' . mysql_error()); } echo "<div align=center><h3>The record has been added.</h3></div>"; } else { echo "<div align=center><h3>Sorry you can add a row twice.</h3></div>"; } Hope this will solve your problem. Regards, Quote Link to comment https://forums.phpfreaks.com/topic/70729-solved-insert-into-with-check-problem/#findComment-355590 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.