I have a problem updating an MySQL Database. I want to be able to update a chart (MySQL Database) as more information comes in, the employees at my firm do not fill in all the information all at once and fill in this chart as more information comes in.
Here is my code:
<?php
//retrieve our data from POST
$ID = $_POST['ID'];
$Date_Received = $_POST['DateReceived'];
$Mortgagee_Vendor = $_POST['Mortgagee'];
$Mortgagor_Purchaser = $_POST['Mortgagor'];
$We_Act_For = $_POST['WeActFor'];
$Amount = $_POST['Amount'];
$Full_Address = $_POST['FullAddress'];
$Lot = $_POST['Lot'];
$Plan = $_POST['Plan'];
$Municipality = $_POST['Municipality'];
$Solicitor = $_POST['Solicitor'];
$Closing_Date = $_POST['ClosingDate'];
$Has_Union = $_POST['Union'];
$Lawyer_Clerk = $_POST['LawyerClerk'];
$Date_File_Opened = $_POST['FileOpened'];
$Search_Date = $_POST['Search'];
$Requisition_Date = $_POST['ReqDate'];
$Requisition_Sub = $_POST['ReqSubmittedReplied'];
$Mortgage_Inst = $_POST['MortgageInst'];
$Appointment_Date = $_POST['AppDate'];
$Payout_Req_Sent = $_POST['PayoutSent'];
$Payout_Req = $_POST['PayoutRequest'];
$Requisition_Recieved = $_POST['ReqReceived'];
$Draft_Deed = $_POST['DraftDeed'];
$Report_Letter = $_POST['ReportLetter'];
$Undertakings = $_POST['Undertakings'];
$File_Closed = $_POST['FileClosed'];
$Special_Notes = $_POST['SpecialNotes'];
$Deal_Active = $_POST['DealActive'];
//connect to database
$dbhost = 'localhost';
$dbname = 'database';
$dbuser = 'user';
$dbpass = 'Password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname, $conn);
//sanitize all fields
$ID = mysql_real_escape_string($ID);
$Date_Received = mysql_real_escape_string($Date_Received);
$Mortgagee_Vendor = mysql_real_escape_string($Mortgagee_Vendor);
$Mortgagor_Purchaser = mysql_real_escape_string($Mortgagor_Purchaser);
$We_Act_For = mysql_real_escape_string($We_Act_For);
$Amount = mysql_real_escape_string($Amount);
$Full_Address = mysql_real_escape_string($Full_Address);
$Lot = mysql_real_escape_string($Lot);
$Plan = mysql_real_escape_string($Plan);
$Municipality = mysql_real_escape_string($Municipality);
$Solicitor = mysql_real_escape_string($Solicitor);
$Closing_Date = mysql_real_escape_string($Closing_Date);
$Has_Union = mysql_real_escape_string($Has_Union);
$Lawyer_Clerk = mysql_real_escape_string($Lawyer_Clerk);
$Date_File_Opened = mysql_real_escape_string($Date_File_Opened);
$Search_Date = mysql_real_escape_string($Search_Date);
$Requisition_Date = mysql_real_escape_string($Requisition_Date);
$Requisition_Sub = mysql_real_escape_string($Requisition_Sub);
$Mortgage_Inst = mysql_real_escape_string($Mortgage_Inst);
$Appointment_Date = mysql_real_escape_string($Appointment_Date);
$Payout_Req_Sent = mysql_real_escape_string($Payout_Req_Sent);
$Payout_Req = mysql_real_escape_string($Payout_Req);
$Requisition_Recieved = mysql_real_escape_string($Requisition_Recieved);
$Draft_Deed = mysql_real_escape_string($Draft_Deed);
$Report_Letter = mysql_real_escape_string($Report_Letter);
$Undertakings = mysql_real_escape_string($Undertakings);
$File_Closed = mysql_real_escape_string($File_Closed);
$Special_Notes = mysql_real_escape_string($Special_Notes);
$Deal_Active = mysql_real_escape_string($Deal_Active);
//insert values to mysql database
mysql_query("UPDATE `all_info` SET `Date Received` = '$Date_Received', `Deal Active` = '$Deal_Active', `Mortgagee/Vendor` = '$Mortgagee_Vendor', `Mortgagor/Purchaser` = '$Mortgagor_Purchaser', `We Act For` = '$We_Act_For', `Amount` = '$Amount', `Full Address` = '$Full_Address', `Lot` = '$Lot', `Plan` = '$Plan', `Municipality` = '$Municipality', `Solicitor` = '$Solicitor', `Closing Date` = '$Closing_Date', `Lawyer/Clerk` = '$Lawyer_Clerk', `File Opened` = '$Date_File_Opened', `Search Date` = '$Search_Date', `Requisition Date` = '$Requisition_Date', `Requisition Submitted/Replied` = '$Requisition_Sub', `Mortgage Instructions Received` = '$Mortgage_Inst', `Appointment Date for Clients` = '$Appointment_Date', `Pay Out Request Sent` = '$Payout_Req_Sent', `Payout/Funding Sent`= '$Payout_Req', `Requisition Received` = '$Requisition_Recieved', `Draft Deed/Adjustment Sent` = '$Draft_Deed', `Reporting Letter` = '$Report_Letter', `Undertaking Satisfied` = '$Undertakings', `File Closed` = '$File_Closed', `Special Notes` = '$Special_Notes' WHERE ID = '$ID'");
echo mysql_error();
mysql_close();
?>
So basically, when a user creates the row (using separate script) he/she will fill in “some” of the post entries. My main problem is when entries exist, they are replaced with blank post submission. I need MySQL to ignore these “blank” post entries and not replace data that exists.
EDIT: with the help of arxanas I was able to come up with this code, this is the final product with working code.
<?php
$ID = $_POST['ID']
$Date_Received = $_POST['DateReceived'];
$Mortgagee_Vendor = $_POST['Mortgagee'];
$Mortgagor_Purchaser = $_POST['Mortgagor'];
$We_Act_For = $_POST['WeActFor'];
$Amount = $_POST['Amount'];
$Full_Address = $_POST['FullAddress'];
$Lot = $_POST['Lot'];
$Plan = $_POST['Plan'];
$Municipality = $_POST['Municipality'];
$Solicitor = $_POST['Solicitor'];
$Closing_Date = $_POST['ClosingDate'];
$Has_Union = $_POST['Union'];
$Lawyer_Clerk = $_POST['LawyerClerk'];
$Date_File_Opened = $_POST['FileOpened'];
$Search_Date = $_POST['Search'];
$Requisition_Date = $_POST['ReqDate'];
$Requisition_Sub = $_POST['ReqSubmittedReplied'];
$Mortgage_Inst = $_POST['MortgageInst'];
$Appointment_Date = $_POST['AppDate'];
$Payout_Req_Sent = $_POST['PayoutSent'];
$Payout_Req = $_POST['PayoutRequest'];
$Requisition_Recieved = $_POST['ReqReceived'];
$Draft_Deed = $_POST['DraftDeed'];
$Report_Letter = $_POST['ReportLetter'];
$Undertakings = $_POST['Undertakings'];
$File_Closed = $_POST['FileClosed'];
$Special_Notes = $_POST['SpecialNotes'];
$Deal_Active = $_POST['DealActive'];
// connect to database
$dbhost = 'localhost';
$dbname = 'dbname';
$dbuser = 'user';
$dbpass = 'pass';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname, $conn);
$fields = array(
"ID" => "ID",
"Date Received" => "DateReceived",
"Mortgagee/Vendor" =>"Mortgagee",
"Mortgagor/Purchaser" => "Mortgagor",
"We Act For" => "WeActFor",
"Amount" => "Amount",
"Full Address" => "FullAddress",
"Lot" => "Lot",
"Plan" => "Plan",
"Municipality" => "Municipality",
"Solicitor" => "Solicitor",
"Closing Date" => "ClosingDate",
"Union" => "Union",
"Lawyer/Clerk" => "LawyerClerk",
"File Opened" => "FileOpened",
"Search Date" => "Search",
"Requisition Date" => "ReqDate",
"Requisition Submitted/Replied" => "ReqSubmittedReplied",
"Mortgage Instructions Received" => "MortgageInst",
"Appointment Date for Clients" => "AppDate",
"Pay Out Request Sent" => "PayoutSent",
"Payout/Funding Sent" => "PayoutRequest",
"Requisition Received" => "ReqReceived",
"Draft Deed/Adjustment Sent" => "DraftDeed",
"Reporting Letter" => "ReportLetter",
"Undertaking Satisfied" => "Undertakings",
"File Closed" => "FileClosed",
"Special Notes" => "SpecialNotes",
"Deal Active" => "DealActive",
);
$update = array();
foreach ($fields as $column => $field) {
if (isset($_POST[$field]) && strlen($_POST[$field])) {
$update[] = "`".$column."` = '".mysql_real_escape_string($_POST[$field])."'";
}
}
$sql = "UPDATE `all_info` SET ".implode(", ", $update)."WHERE ID = '$ID'";
mysql_query($sql);
echo mysql_error();
echo "<br><br><center>Table Sucessfully Updated</center>";
mysql_close();
?>
1 Answer