从基于数据库值的选项中选择

时间:2016-02-13 22:06:40

标签: php mysql

我正在研究我的项目的最后一部分,我正在构建网站,在这部分中,我想显示作业的选项(作业是否仍在进行中或已完成) 我在mysql枚举值中给出了我的行,“已完成”,“InProgress” 当学生选择工作时,JobStatus将成为“InProgress” 并且学生可以从他的JobLists页面更改此值,完成后,他可以将其更改为已完成。它将在数据库中更改 这是我的代码尝试,在此代码中,它向我显示更新查询的错误 JobStatus = '".$_POST['JobStatus']未定义?任何人都可以帮助PLEASE伙计

<?php 
  //Connect to DB
include('CIEcon.php');

$sqlCommand ="SELECT Accounts.SSU , Jobs.JobName, Jobs.Description, Jobs.DueDate,Jobs.JobId, JobsLists.JobStatus FROM JobsLists,Jobs,Accounts WHERE  Accounts.SSU = JobsLists.SSU AND Jobs.JobId = JobsLists.JobId  And Accounts.SSU = '".$_SESSION['SSU']."'  ";

$result = mysqli_query($dbCIE,$sqlCommand) or die(mysql_error()); 

 echo "<form  action='JobsLists.php' method='post'>";


while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td> <input type='checkbox'  name='JobId[]'  value='". $row['JobId'] ."'  /> </td>";
echo "<td align=center>" . $row['SSU'] . "</td>";
echo "<td align=center>" . $row['JobName'] . "</td>";
echo "<td align=center> " . $row['Description'] . "</td>";
echo "<td align=center>" . $row['DueDate'] . "</td>";
echo "<td align=center>" . 
"<select>
  <option name = JobStatus[".$row['JobId']."] value='InProgress' selected> In Progress </option>
  <option name = JobStatus[".$row['JobId']."] value='Completed'          >     Completed </option>
</select>" . "</td>"; // need to be worked on.. 
echo "</tr>";
}
"</table>";

//Connect to DB
include('CIEcon.php');

// save the SSU for the current user to save the sata when insert jobs in jobslist
$SSU = $_SESSION['SSU']; 
/////


//handle this when to save a status. 
if( isset($_POST['save']) ){
    if( empty($_POST['JobId']) || $_POST['JobId'] == 0 ){
      echo"<h4>  Status Wasn't Changed..  </h4>";
              }else{ 
include('CIEcon.php');  //$dbCIE 
foreach($_POST['JobId'] AS $i){
    /// update JobsLists table with the new status..  
     $sqlUpdate = "UPDATE  JobsLists SET JobStatus = '".$_POST['JobStatus'][$i]."'  WHERE JobId = '" . $i . "'";
     $resultUpdate = mysqli_query($dbCIE,$sqlUpdate) or die(mysqli_error($dbCIE));

}

// TEST ONLY ////////----------------------------------------////////////
if (mysqli_affected_rows($dbCIE) > 0) {
  echo "<h4> You have successfully Saved your statuse </h4>";

}else{ echo "<h4> Error occurred </h4> "; }
////////----------------------------------------////////////


                }  // end of else, when user select something.. 
            } 


?>

1 个答案:

答案 0 :(得分:1)

这是因为你没有命名你试图用...发送值的选择框.HTML <option>没有名字,只有一个值。该值是指定给<select>

$_POST的名称的值
 while($row = mysqli_fetch_array($result))
    {
    echo "<tr>";
    echo "<td> <input type='checkbox'  name='JobId[]'  value='". $row['JobId'] ."'  /> </td>";
    echo "<td align=center>" . $row['SSU'] . "</td>";
    echo "<td align=center>" . $row['JobName'] . "</td>";
    echo "<td align=center> " . $row['Description'] . "</td>";
    echo "<td align=center>" . $row['DueDate'] . "</td>";
    echo "<td align=center>" . 
    echo "<select name='JobStatus[".$row['JobId']."]'>";
        if($row['JobStatus'] == "InProgress"){
           echo  "<option value='InProgress' selected>In Progress</option>";  
           echo "<option value='Completed'>Completed</option>";
        } else {
            echo  "<option value='InProgress'>In Progress</option>";  
           echo "<option value='Completed' selected> Completed </option>";

        } 

    echo "</select>" . "</td>"; // need to be worked on.. 
    echo "</tr>";
    }
    "</table>";