【问题标题】:Unable to update database from form retrieved by SELECT OPTION无法从 SELECT OPTION 检索到的表单更新数据库
【发布时间】:2016-01-30 17:13:13
【问题描述】:

我不知道$updateApproval 语句有什么问题。一切都很好,$_POST 能够从表单中检索数据。 SQL语句在我运行它时在phpMyAdmin上运行良好,替换变量所以不应该有任何错误。

我是在不知情的情况下发生冲突,还是有其他原因导致我的更新语句不起作用?尝试在这里和那里切换,但它只是保持安静,没有出现任何错误。我为您提供您需要的信息,如果很乏味,我很抱歉。任何帮助是极大的赞赏。谢谢。

这是我的数据库:

同意书

consent
-----------------------------------------------------------------------------------------
consent_id | staff_id | approval_id | type_of_leave | consent_date_from | consent_date_to

请假类型表

leavetype
----------------------------
type_of_leave | leave_type |

员工表

 staff
 ------------------------------------------------------------------
 staff_id | role_id | staff_name | gender | staff_email | password |

员工休假表

staffleave
----------------------------------------------------------------------
leave_log | staff_id | annual_leave | sick_leave .....//other leaves and so on

表格在这里。我实际上已经将select option 放入表单中,因此有<td> <tr> 标签。

<td>
    <div class="form-group">
        <form action="doApproval.php" method="post" name="register">
            <input hidden name="getStaffId" value="<?php echo $staffId  ?>" >               
            <input hidden name="getConsentId" value="<?php echo $consentId ?>" >            
            <input hidden name="getLeaveId" value="<?php echo $leaveId ?>" >  
                 <div class="form-group">
                       <select class="form-control" onchange="this.form.submit()" id="select" name="getConsentChange">
                             <option value="1" <?php if ($getCurrentStatus == 1) echo "selected"; ?>>Approve</option>
                             <option value="2" <?php if ($getCurrentStatus == 2) echo "selected"; ?>>Reject</option>
                             <option <?php if ($getCurrentStatus == 3) echo "selected"; ?>>Pending</option>
                       </select>
                 </div>
                     <noscript><input type="submit" value="Submit"></noscript>
        </form>
     </div>
</td>

POST 会在这里。保存员工休假天数的查询效果很好,但不能保存他们的休假状态。

$staffId = $_POST['getStaffId'];
$consentId = $_POST['getConsentId'];
$getConsent = $_POST['getConsentChange'];
$getLeaveId = $_POST['getLeaveId'];

$updateApproval = "UPDATE consent SET approval_id = $getConsent WHERE consent.staff_id = '$staffId' AND consent.consent_id = $getConsent"; //Update statement that is not working

$leaveCheckpoint = "SELECT * FROM consent, staffleave, staff WHERE staffleave.staff_id = staff.staff_id 
AND staff.staff_id = consent.staff_id AND consent.consent_id = '$consentId'";

$checkpointQuery = (mysqli_query($link, $leaveCheckpoint)) or die("Retrieve checkpoint error " . mysqli_error($link));

if ($checkLeave = mysqli_fetch_array($checkpointQuery)) {
if ($checkLeave['staff_id'] = '$staffId' && $checkLeave['consent_id'] = '$consentId') {

    //retrieving the number of leaves staff have took

   if ($getLeaveId == 1 && $getConsent == 1) {
        $updatedLeave1 = $chkAnnual + $dateDiff;
        $recordLeave = "UPDATE staffleave SET annual_leave = '$updatedLeave1' WHERE staff_id = '$staffId'";
    } else if ($getLeaveId == 2 && $getConsent == 1) {
        $updatedLeave2 = $chkSick + $dateDiff;
        $recordLeave = "UPDATE staffleave SET sick_leave = '$updatedLeave2' WHERE staff_id = '$staffId'";
    } else if ......// so on when they meet the condition, it works fine and able to insert.
else {
        ?>
        <script type="text/javascript">
            alert("No data was updated in the process")
            window.location = "manageStaffLeave.php";
        </script>     
 }
<?php
   }
$successConsent = mysqli_query($link, $recordLeave) or die("Insert Leave Date Error " . mysqli_error($link));
 }

$approvalUpdate = (mysqli_query($link, $updateApproval)) or die("Update error " . mysqli_error($link));

mysqli_close($link);
?>

<!DOCTYPE html>
<body>
 if ($approvalUpdate && $successConsent) {
        ?>
        <script type="text/javascript">
            window.location = "manageStaffLeave.php";
        </script>
        <?php
    }
    ?>
</body>

【问题讨论】:

  • &lt;input hidden name="getStaffId" value="&lt;?php echo $staffId ?&gt;" &gt; &lt;input hidden name="getConsentId" value="&lt;?php echo $consentId ?&gt;" &gt; &lt;input hidden name="getLeaveId" value="&lt;?php echo $leaveId ?&gt;" &gt; 试试把 ;?

标签: php html forms mysqli


【解决方案1】:

我想你错过了';'

<input hidden name="getStaffId" value="<?php echo $staffId; ?>" > 
<input hidden name="getConsentId" value="<?php echo $consentId; ?>" > 
<input hidden name="getLeaveId" value="<?php echo $leaveId; ?>" >

【讨论】:

  • 我不明白你。隐藏在表单中的值可以POST 结束,所以我不认为它缺少引号。
【解决方案2】:

你犯了一个基本错误:

$checkLeave['staff_id'] = '$staffId' &amp;&amp; $checkLeave['consent_id'] = '$consentId

在这里,您将字符串'$staffId' 影响到数组$checkLeave['staff_id']$consentId$checkLeave['consent_id']

去掉引号和等号进行比较:

$checkLeave['staff_id'] == $staffId &amp;&amp; $checkLeave['consent_id'] == $consentId

【讨论】:

  • 抱歉回复晚了。改变了它,没有任何区别:/
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-01
  • 2014-09-10
  • 2021-05-20
  • 2015-01-27
  • 1970-01-01
  • 1970-01-01
  • 2017-07-25
相关资源
最近更新 更多