如何从HTML表单中检索Mysql数据并显示在输入表单上以供后续更新

时间:2014-02-17 05:58:47

标签: php html forms

* 仍在等待此问题的答案 *

我是HTML& amp;的新手PHP。 我需要开发一个交互式月度账户管理表来管理我的每月公用事业账单支付记录。

我需要做什么? 我喜欢使用一个HTML表单,用于提交一组新数据,检索特定月份和年份的数据集,以及更新月份和年份特定记录的数据字段。

已完成的工作:

  1. 在MySQL DB中创建了名为“monthlyaccount”的表 共有9列:月,年,电,水,电缆,电话, 抵押贷款,CarLoan,保险
  2. 创建了一个包含9个输入字段的HTML表单(Month除外) 和年份,所有7个输入字段都是数字类型)
  3. 表单中有3个按钮:提交,检索和更新
  4. 应该做什么:

    1. 提交按钮:在我填写表格后,点击数据 提交按钮,将调用一个php文件(putDataToDB.php) 后端将连接MySQL DB并将数据插入到 在表格中输入的数据库表

    2. 检索按钮:在HTML表单中,我只会填写月份和年份 输入数据字段,然后单击将获取的Retrieve按钮 MySQL DB中该月份和年份的数据并显示它们 在表格的其余7个字段

    3. 更新按钮:我检索了一个月的数据后(通过检索 按钮),我可以更新或更正任何特定的数据字段,然后单击 更新按钮,它将更新MySQl DB中的特定数据

    4. 有关PHP编程的注意事项:

      1. 我对所有三个按钮使用了type =“ submit ”(提交,检索) 和前端HTML代码中的更新)

      2. 在后端 PHP ,我正在使用“ isset()”功能检查一下 按钮已被点击

      3. 如果点击“提交”,则会使用 INSERT 将所有数据插入到 MySQL DB表通过在表中插入一个新的特定行 月和年

      4. 如果点击“检索”,它将使用 SELECT * FROM  monthlyaccout 来获取特定月份和年份的所有数据  (使用 WHERE 子句)从MySQL DB中将它们呈现在  提交的HTML表单相同

      5. 如果点击“更新”,它将使用UPDATE更新现有内容  特定月份和年份的数据字段

      6. 我需要帮助的地方:在PHP函数内部, retrieveDataForAMonthYearAndPutOnTheForm()

        创建表单的HTML代码:

        <!DOCTYPE html>
        <head>
        <link rel="stylesheet" type="text/css" href="myAccountFormTesting.css">
        </head>
        
        <html>
        <body>
        
        <h1>Monthly Account Data Entry Form</h1>
        
        <form action="putDataToDB.php" method="post">
        
        <fieldset>
        <label for="month">Month:</label>
        <input type="text" name="month" value="January">
        
        <label for="year">Year:</label>
        <input type="number" name="year" value="2014">
        <br>
        <br>
        
        <label for="electricity">Electricity:</label>
        <input type="number" name="electricity" min="0" step="any" value="">
        
        <label for="electricityDueDate">Due Date:</label>
        <input type="date"   name="electricityDueDate" value="">
        
        <label for="electricityPaymentStatus">Payment Status:</label>
        <input type="radio"  name="electricityPaymentStatus" value="yes">Yes 
        <input type="radio" name="electricityPaymentStatus" value="no">No
        <br>
        <br>
        
        <label for="water">Water:</label>
        <input type="number" name="water" min="0" step="any" value="">
        
        <label for="waterDueDate">Due Date:</label>
        <input type="date"   name="waterDueDate" value="">
        
        <label for="waterPaymentStatus">Payment Status:</label>
        <input type="radio"  name="waterPaymentStatus" value="yes">Yes 
        <input type="radio" name="waterPaymentStatus" value="no">No
        <br>
        <br>
        
        <label for="cable">Cable:</label>
        <input type="number" name="cable" min="0" step="any" value="">
        
        <label for="cableDueDate">Due Date:</label>
        <input type="date"   name="cableDueDate" value="">
        
        <label for="cablePaymentStatus">Payment Status:</label>
        <input type="radio"  name="cablePaymentStatus" value="yes">Yes 
        <input type="radio" name="cablePaymentStatus" value="no">No
        <br>
        <br>
        
        <label for="phone">Phone:</label>
        <input type="number" name="phone" min="0" step="any" value="">
        
        <label for="phoneDueDate">Due Date:</label>
        <input type="date"   name="phoneDueDate" value="">
        
        <label for="phonePaymentStatus">Payment Status:</label>
        <input type="radio"  name="phonePaymentStatus" value="yes">Yes 
        <input type="radio" name="phonePaymentStatus" value="no">No
        <br>
        <br>
        
        <label for="mortgage">Mortgage:</label>
        <input type="number" name="mortgage" min="0" step="any" value="">
        
        <label for="mortgageDueDate">Due Date:</label>
        <input type="date"   name="mortgageDueDate" value="">
        
        <label for="mortgagePaymentStatus">Payment Status:</label>
        <input type="radio"  name="mortgagePaymentStatus" value="yes">Yes 
        <input type="radio" name="mortgagePaymentStatus" value="no">No
        <br>
        <br>
        
        <label for="carloan">CarLoan:</label>
        <input type="number" name="carloan" min="0" step="any" value="">
        
        <label for="carloanDueDate">Due Date:</label>
        <input type="date"   name="carloanDueDate" value="">
        
        <label for="carloanPaymentStatus">Payment Status:</label>
        <input type="radio"  name="carloanPaymentStatus" value="yes">Yes 
        <input type="radio" name="carloanPaymentStatus" value="no">No
        <br>
        <br>
        
        <label for="insurance">Insurance:</label>
        <input type="number" name="insurance" min="0" step="any" value="">
        
        <label for="insuranceDueDate">Due Date:</label>
        <input type="date"   name="insuranceDueDate" value="">
        
        <label for="insurancePaymentStatus">Payment Status:</label>
        <input type="radio"  name="insurancePaymentStatus" value="yes">Yes 
        <input type="radio" name="insurancePaymentStatus" value="no">No
        <br>
        <br>
        
        <input type="submit" name="retrieve_button" value="Retrieve Data from DB" style="float:left;">
        <br>
        <br>
        <input type="submit" name="update_button" value="Update a Record of DB" style="float:left;">
        
        <input type="submit" name="submit_button" value="Submit a New Record to DB" style="float:right;">
        
        <fieldset>
        </form>
        </body>
        </html>
        

        服务器端PHP代码:

        <?php
        // Set local time
        date_default_timezone_set('America/Toronto');
        
        // Connect to MySQL DB
        $con=mysqli_connect("localhost","root","********","account");
        
        // Check connection
        if (mysqli_connect_errno())
        {
          echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }//if-MySQL connect
        
        // If Retrieve button is clicked
        if (isset($_POST['retrieve_button']))
        {
           // Call a function which will retrieve the data for a specific Month-Year and displays on the form
           retrieveDataForAMonthYearAndPutOnTheForm();
        }//if-Retrieve button
        
        // If Update button is clicked
        if (isset($_POST['update_button']))
        {
           // Call a function which will update any data column after retrieve for a specific Month-Year
           UpdateDataAfterRetrieve();
        }//if-Update button
        
        // If Submit button is clicked
        if (isset($_POST['submit_button']))
        {
           // Call a function to check if a row for the Month-Year is already existed in the DB (if existed, returns 0)
           $returnfromCheckMonthYear=checkMonthYear();
        
          // Execute insert query only if the Month-Year is not existed in the DB
          if($returnfromCheckMonthYear)
         {
             $sql="INSERT INTO monthlyaccount
             (ACCOUNTMONTH,
              ACCOUNTYEAR,
        
              ELECTRICITY,
              ELECTRICITY_DUE_DATE,
              ELECTRICITY_PAYMENT_STATUS,
        
              WATER,
              WATER_DUE_DATE,
              WATER_PAYMENT_STATUS,
        
              CABLE,
              CABLE_DUE_DATE,
              CABLE_PAYMENT_STATUS,
        
              PHONE,
              PHONE_DUE_DATE,
              PHONE_PAYMENT_STATUS,
        
              MORTGAGE,
              MORTGAGE_DUE_DATE,
              MORTGAGE_PAYMENT_STATUS,
        
              CARLOAN,
              CARLOAN_DUE_DATE,
              CARLOAN_PAYMENT_STATUS,
        
              INSURANCE,
              INSURANCE_DUE_DATE,
              INSURANCE_PAYMENT_STATUS,
        
              LAST_UPDATE)
        
              VALUES
             ('$_POST[month]',
              '$_POST[year]',
        
              '$_POST[electricity]',
              '$_POST[electricityDueDate]',
              '$_POST[electricityPaymentStatus]',
        
              '$_POST[water]',
              '$_POST[waterDueDate]',
              '$_POST[waterPaymentStatus]',
        
              '$_POST[cable]',
              '$_POST[cableDueDate]',
              '$_POST[cablePaymentStatus]',
        
              '$_POST[phone]',
              '$_POST[phoneDueDate]',
              '$_POST[phonePaymentStatus]',
        
              '$_POST[mortgage]',
              '$_POST[mortgageDueDate]',
              '$_POST[mortgagePaymentStatus]',
        
              '$_POST[carloan]',
              '$_POST[carloanDueDate]',
              '$_POST[carloanPaymentStatus]',
        
              '$_POST[insurance]',
              '$_POST[insuranceDueDate]',
              '$_POST[insurancePaymentStatus]',
        
              now())";
        
            // If can't connect MySQL, Go for Fishing !
            if (!mysqli_query($con, $sql))
            {
              die('Error: ' . mysqli_error($con));
            }//if-!mysql
        
         }//if($returnfromCheckMonthYear)
        
        }//if-isset-Submit
        
        // Function: To check if a record already existed in the DB table for the Month-Year (we don't need to insert any new row for that Month-Year)
        // Returns: 1= if not existed, 0= if existed
        function checkMonthYear()
        {
          $returnValue=0;
          $currentMonth=$_POST[month];
          $currentYear=$_POST[year];
        
          $connectsql = $GLOBALS['con']; 
        
          $sqlCheckMonthYear="SELECT * FROM monthlyaccount WHERE ACCOUNTMONTH = '$currentMonth' AND ACCOUNTYEAR = '$currentYear'";
        
          $result=mysqli_query($connectsql, $sqlCheckMonthYear);
        
          while($row=mysqli_fetch_array($result))
          {
            $retrievedMonth=$row['ACCOUNTMONTH'];
            $retrievedYear=$row['ACCOUNTYEAR'];
          }//while
        
          // Set the return value based on the matching of retrieved values of Month and Year
          if(($retrievedMonth==$currentMonth) && ($retrievedYear==$currentYear))
          {
              // Month-Year already existed in the table
              $returnValue=0;
          }
          else
          {
              // Month-Year NOT existed in the table
              $returnValue=1;
          }
        
          return $returnValue;
        }//function checkMonthYear()
        
        // Function: It retrieves the data from MySQL DB for a specific Month-Year and displays on the form
        // Return: None
        function retrieveDataForAMonthYearAndPutOnTheForm()
        {
          $currentMonthFromForm=$_POST[month];
          $currentYearFromForm=$_POST[year];
        
          $connectsql = $GLOBALS['con']; 
        
          $retrievedDataFromTable="SELECT * FROM monthlyaccount WHERE ACCOUNTMONTH = '$currentMonthFromForm' AND ACCOUNTYEAR = '$currentYearFromForm'";
        
          $result=mysqli_query($connectsql, $retrievedDataFromTable);
        
          // Get all data from the table and assign to variables for later usage
          while($row=mysqli_fetch_array($result))
          {
            $retrievedMonthToDisplay=$row['ACCOUNTMONTH'];
            $retrievedYearToDisplay=$row['ACCOUNTYEAR'];
        
            $retirevedElectricityToDisplay=$row['ELECTRICITY'];
            $retrievedElectricityDueDateToDisplay=$row['ELECTRICITY_DUE_DATE'];
            $retrievedElectricityPaymentStatusToDisplay=$row['ELECTRICITY_PAYMENT_STATUS'];
        
            $retirevedElectricityToDisplay=$row['WATER'];
            $retrievedElectricityDueDateToDisplay=$row['WATER_DUE_DATE'];
            $retrievedElectricityPaymentStatusToDisplay=$row['EWATER_PAYMENT_STATUS'];
        
            $retirevedElectricityToDisplay=$row['CABLE'];
            $retrievedElectricityDueDateToDisplay=$row['CABLE_DUE_DATE'];
            $retrievedElectricityPaymentStatusToDisplay=$row['CABLE_PAYMENT_STATUS'];
        
            $retirevedElectricityToDisplay=$row['PHONE'];
            $retrievedElectricityDueDateToDisplay=$row['PHONE_DUE_DATE'];
            $retrievedElectricityPaymentStatusToDisplay=$row['PHONE_PAYMENT_STATUS'];
        
            $retirevedElectricityToDisplay=$row['MORTGAGE'];
            $retrievedElectricityDueDateToDisplay=$row['MORTGAGE_DUE_DATE'];
            $retrievedElectricityPaymentStatusToDisplay=$row['MORTGAGE_PAYMENT_STATUS'];
        
            $retirevedElectricityToDisplay=$row['CARLOAN'];
            $retrievedElectricityDueDateToDisplay=$row['CARLOAN_DUE_DATE'];
            $retrievedElectricityPaymentStatusToDisplay=$row['CARLOAN_PAYMENT_STATUS'];
        
            $retirevedElectricityToDisplay=$row['INSURANCE'];
            $retrievedElectricityDueDateToDisplay=$row['INSURANCE_DUE_DATE'];
            $retrievedElectricityPaymentStatusToDisplay=$row['INSURANCE_PAYMENT_STATUS'];
          }//while
        
          // ****Now need to dispaly all retrieved data on the original form, HOW ??? 
          // ****Is there any reference ID we could use for accessing the form created before by HTML ???***
        
        }//function retrieveDataForAMonthYearAndPutOnTheForm()
        
        // Function: It will update any data column(s) for a specific Month-Year (we'll use update SQL query to update the specific row of a Month-Year)
        // Return: None
        function UpdateDataAfterRetrieve()
        {
         $updatesql=
              "UPDATE monthlyaccount
              SET 
              ACCOUNTMONTH='$_POST[month]',
              ACCOUNTYEAR='$_POST[year]',
        
              ELECTRICITY= '$_POST[electricity]',
              ELECTRICITY_DUE_DATE= '$_POST[electricityDueDate]',
              ELECTRICITY_PAYMENT_STATUS='$_POST[electricityPaymentStatus]',
        
              WATER='$_POST[water]',
              WATER_DUE_DATE='$_POST[waterDueDate]',
              WATER_PAYMENT_STATUS='$_POST[waterPaymentStatus]',
        
              CABLE='$_POST[cable]',
              CABLE_DUE_DATE='$_POST[cableDueDate]',
              CABLE_PAYMENT_STATUS='$_POST[cablePaymentStatus]',
        
              PHONE='$_POST[phone]',
              PHONE_DUE_DATE='$_POST[phoneDueDate]',
              PHONE_PAYMENT_STATUS='$_POST[phonePaymentStatus]',
        
              MORTGAGE='$_POST[mortgage]',
              MORTGAGE_DUE_DATE='$_POST[mortgageDueDate]',
              MORTGAGE_PAYMENT_STATUS='$_POST[mortgagePaymentStatus]',
        
              CARLOAN='$_POST[carloan]',
              CARLOAN_DUE_DATE='$_POST[carloanDueDate]',
              CARLOAN_PAYMENT_STATUS='$_POST[carloanPaymentStatus]',
        
              INSURANCE='$_POST[insurance]',
              INSURANCE_DUE_DATE='$_POST[insuranceDueDate]',
              INSURANCE_PAYMENT_STATUS='$_POST[insurancePaymentStatus]',
        
              LAST_UPDATE=now()
        
              WHERE ACCOUNTMONTH = '$_POST[month]' AND ACCOUNTYEAR = '$_POST[year]'";
        
              $connectsql = $GLOBALS['con']; 
        
              // If can't connect MySQL, abort !
             if (!mysqli_query($connectsql, $updatesql))
             {
              die('Error: ' . mysqli_error($connectsql));
             }//if-!mysql
        
        }//function UpdateDataAfterRetrieve()
        
        mysqli_close($con);
        ?>
        

        感谢您的帮助。

0 个答案:

没有答案