hope you can help me out with this.
I use Dreamweaver to help me code as I'm still learning PHP and I'm stuck on trying to create a loop.
I have a Course Provider's Table and an Invoices Table.
What I'm trying to do:
Check the invoices table for a course providers ID - if exists - check for last months date - if exists - don't insert anything.
If there are no records with the course provider's ID and last months date, insert a record.
Here's my code which works first time I refresh the page but, if I change a date to something other than last months date and refresh again it will just keep adding even though the record already exists.
Please excuse my messy code:
mysql_select_db($database_dbconnect, $dbconnect);
$query_rs_get_cps = "SELECT tl_course_providers.cp_id FROM tl_course_providers ";
$rs_get_cps = mysql_query($query_rs_get_cps, $dbconnect) or die(mysql_error());
$row_rs_get_cps = mysql_fetch_assoc($rs_get_cps);
$totalRows_rs_get_cps = mysql_num_rows($rs_get_cps);
$invoiceDate = date("Y-m-d",strtotime("-1 months"));
$dueDate = date("Y-m-d",strtotime("+1 months"));
do{
$cpid = $row_rs_get_cps['cp_id'];
$cpid_rs_get_invoices = "-1";
if (isset($cpid)) {
$cpid_rs_get_invoices = $cpid;
}
mysql_select_db($database_dbconnect, $dbconnect);
$query_rs_get_invoices = sprintf("SELECT * FROM tl_invoices WHERE tl_invoices.fk_cp_id = %s", GetSQLValueString($cpid_rs_get_invoices, "int"));
$rs_get_invoices = mysql_query($query_rs_get_invoices, $dbconnect) or die(mysql_error());
$row_rs_get_invoices = mysql_fetch_assoc($rs_get_invoices);
$totalRows_rs_get_invoices = mysql_num_rows($rs_get_invoices);
if($row_rs_get_invoices['invoice_date'] != $invoiceDate)
{
// for testing
echo "CP Table: ".$cpid."<br />";
echo "Invoice Table: ".$row_rs_get_invoices['fk_cp_id']."<br />";
$insertSQL = sprintf("INSERT INTO tl_invoices (fk_cp_id, invoice_date, due_date, total, invoice_status) VALUES (%s, %s, %s, %s, %s)",
GetSQLValueString($row_rs_get_cps['cp_id'], "int"),
GetSQLValueString($invoiceDate, "date"),
GetSQLValueString($dueDate, "date"),
GetSQLValueString('0', "text"),
GetSQLValueString('Due', "text")
);
mysql_select_db($database_dbconnect, $dbconnect);
$Result1 = mysql_query($insertSQL, $dbconnect) or die(mysql_error());
}else{
break;
}
}while($row_rs_get_cps = mysql_fetch_assoc($rs_get_cps));