-1

我在更新 MySQL 数据库时遇到问题。我希望能够在更多信息进入时更新图表(MySQL 数据库),我公司的员工不会一次填写所有信息,而是在更多信息进入时填写此图表。

这是我的代码:

<?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();

?>

所以基本上,当用户创建行(使用单独的脚本)时,他/她将填写“一些”帖子条目。我的主要问题是当条目存在时,它们被替换为空白帖子提交。我需要 MySQL 忽略这些“空白”帖子条目,而不是替换存在的数据。

编辑:在 arxanas 的帮助下,我能够想出这段代码,这是带有工作代码的最终产品。

<?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();

?> 
4

2 回答 2

2
$fields = array(
    "ID" => "ID",
    "Date Received" => "DateReceived",
    "Mortgagee/Vendor" =>"Mortgagee",
    ...
);
$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);
于 2012-08-24T19:52:45.727 回答
-1

相当简单,当您处理帖子时,您将拥有:

$values = array();
foreach($_POST as $key => $val) {
   if (empty($key)) {
       continue;
   }
   $fields[] = mysql_real_escape_string($key) . "='" . mysql_real_escape_string($val) . "'";
}

$value_list = implode(', ', $fields);
$sql = "UPDATE ... SET " . $value_list;

但这引出了一个问题——如果用户想要清空一个字段怎么办?这个简单的代码将简单地忽略任何空字段,并在数据库中保持原始值不变。

于 2012-08-24T19:57:12.967 回答