trouble with select

I am using PHP Version 5.3.1 and MYSQL 5.1.41 and I have my select statement here however i was not sure with the php that im using to filter the results if I should have or could have posted it here or in the PHP section, as part of my question pertains to it as well…if i am posting in the wrong place please let me know and i will move it.

ok this is killin me its not working and when it works it works backwards…whats happening is when I use $DArea anywhere in the Query it doesnt work, If I choose a different Area than one in the database It executes the Else Statement(not supposed to do that) and it post data from the same Month, Year and Area to the database…once again what im trying to do is not allow any information from an Area “$DArea” in the database unless the Month or the Year is different.

In other words If I fill out information from area “Gym” on November(“DMonth”) of 2010(“DYear”) the next time i will be allowed to enter data on it is next month. And if you were wondering Dmonth and DYear are variables tied to the time() function abd it does pass the correct month and time to the database.
[php]
$DArea = $areaReported;

$query = mysql_query(“SELECT * FROM $table_name WHERE Month =’$DMonth’ AND Year= ‘$DYear’ AND Area = ‘$DArea’”) or die(mysql_error());
$numrows = mysql_num_rows($query);
if ($numrows!=0)
{
while ($row = mysql_fetch_assoc($query))
{
$dbMonth = $row[‘Month’];
$dbYear = $row[‘Year’];
$dbArea = $row[‘Area’];

	}
		
		if ($DMonth==$dbMonth&&$DYear==$dbYear&&$DArea==$dbArea)
		{
			mysql_query("INSERT INTO $table_name (user_id, Date, Month, Year, Area, percent1, stat1)
								values ('{$user_id}',
										'{$realdate}',
										'{$DMonth}',
										'{$DYear}',
										'{$DArea}',
										'{$percent1}',
										'{$stat1}')");

// defining the output
include(“output.inc”);
if ($total == “1”){
$output = $output1;
}
else if ($total == “2”){
$output = $output2;
}
else if ($total == “3”){
$output = $output3;
}
else if ($total == “4”){
$output = $output4;
}
else if ($total == “5”){
$output = $output5;
}
else if ($total == “6”){
$output = $output6;
}
else if ($total == “7”){
$output = $output7;
}
else if ($total == “8”){
$output = $output8;
}
else if ($total == “9”){
$output = $output9;
}
else if ($total == “10”){
$output = $output10;
}
}
else
echo “<p style=“color:red;”>A Report for this $areaReported has already been submitted for this Month

”;
}
[/php]

I have also tried the statement this way.
[php]
(“SELECT ‘Month’, ‘Year’, ‘Area’ FROM ‘data’ WHERE ‘Month’ =’$DMonth’ AND ‘Year’=’$DYear’ AND ‘Area’ = ‘$DArea’”)
[/php]

If it stops any info from hitting the database it stops it all and when the error message I want to appear when someone tries to enter a duplicate record from a specific area for the same month, it does not show unless its a month that does not exist in the database and to get that to even show up I have to take the Area Clause out of the Select statement. I am out of ideas on this one. Can anyone tell me if im even going in the right direction here? Please help

[php]

<?php $DArea = $areaReported; $query = mysql_query("SELECT * FROM $table_name WHERE Month ='$DMonth' AND Year= '$DYear' AND Area = '$DArea'") or die(mysql_error()); $numrows = mysql_num_rows($query); if ($numrows!=0) { //this while loop is not needed, your SELECT is only getting records for DMonth in DYear in DArea, therefore the $row['month'] is always going to be DMonth, $row['year'] always DYear and $row['area'] always DArea... while ($row = mysql_fetch_assoc($query)) { $dbMonth = $row['Month']; $dbYear = $row['Year']; $dbArea = $row['Area']; } //now here you are saying if a record exists for this month, year and area...then add this month, this year and this area? the reverse of what you want to do if ($DMonth==$dbMonth&&$DYear==$dbYear&&$DArea==$dbArea) { mysql_query("INSERT INTO $table_name (user_id, Date, Month, Year, Area, percent1, stat1) values ('{$user_id}', '{$realdate}', '{$DMonth}', '{$DYear}', '{$DArea}', '{$percent1}', '{$stat1}')"); // defining the output include("output.inc"); if ($total == "1"){ $output = $output1; } else if ($total == "2"){ $output = $output2; } else if ($total == "3"){ $output = $output3; } else if ($total == "4"){ $output = $output4; } else if ($total == "5"){ $output = $output5; } else if ($total == "6"){ $output = $output6; } else if ($total == "7"){ $output = $output7; } else if ($total == "8"){ $output = $output8; } else if ($total == "9"){ $output = $output9; } else if ($total == "10"){ $output = $output10; } } else echo "

A Report for this $areaReported has already been submitted for this Month

"; } ?>

[/php]

try
[php]

<?php $DArea = $areaReported; $query = mysql_query("SELECT * FROM $table_name WHERE Month ='$DMonth' AND Year= '$DYear' AND Area = '$DArea'") or die(mysql_error()); $numrows = mysql_num_rows($query); //if there is a record for this month, year, area... if ($numrows!=0) { echo "

A Report for this $areaReported has already been submitted for this Month

"; //if no record for this month, year, area yet... } else { mysql_query("INSERT INTO $table_name (user_id, Date, Month, Year, Area, percent1, stat1) values ('{$user_id}','{$realdate}','{$DMonth}','{$DYear}','{$DArea}','{$percent1}','{$stat1}')"); // defining the output include("output.inc"); if ($total == "1"){ $output = $output1; } else if ($total == "2"){ $output = $output2; } else if ($total == "3"){ $output = $output3; } else if ($total == "4"){ $output = $output4; } else if ($total == "5"){ $output = $output5; } else if ($total == "6"){ $output = $output6; } else if ($total == "7"){ $output = $output7; } else if ($total == "8"){ $output = $output8; } else if ($total == "9"){ $output = $output9; } else if ($total == "10"){ $output = $output10; } } ?>

[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service