希望你能帮我解决这个问题。
我使用 Dreamweaver 来帮助我编写代码,因为我仍在学习 PHP,并且一直在尝试创建循环。
我有一个课程提供者表和一个发票表。
我正在尝试做的事情:
检查课程提供者 ID 的发票表 - 如果存在 - 检查上个月日期 - 如果存在 - 不要插入任何内容。
如果没有包含课程提供者 ID 和上个月日期的记录,请插入一条记录。
这是我第一次刷新页面时的代码,但是,如果我将日期更改为上个月以外的日期并再次刷新,即使记录已经存在,它也会继续添加。
请原谅我乱七八糟的代码:
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));