Jump to content

[SOLVED] insert into with check problem.


Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/70729-solved-insert-into-with-check-problem/
Share on other sites

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

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,

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>";

/*

 

}

 

 

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,

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.