显示上次插入数据的数据时出现问题

时间:2010-03-31 10:04:30

标签: php mysql

我正在设计员工rota planner ....有三个表Staff(Staff details),Event(Event details)和Job(JobId,JobDate,EventId(fk),StaffId(fk))。我需要用员工姓名显示最后插入的工作明细。我已经在这里呆了几个小时而且无处可去。我在这里先向您的帮助表示感谢。我的代码如下:

$eventId = $_POST['eventid'];
$selectBox = $_POST['selectbox'];
$timePeriod = $_POST['time'];
$selectedDate = $_POST['date'];
$count = count($selectBox);

echo "<fieldset><center>";
echo "<form name=submit action='' method=post>";

//display the selected event
$eventsql = "SELECT EventName FROM Event WHERE EventId= ".$eventId;
$result = mysql_query($eventsql);

while ($eventarray = mysql_fetch_array($result))
{
    $eventName = $eventarray['EventName'];
    echo "<h3><font color=red>".$eventName." </font></h3>"; 
}

//constructing the staff selection  
if (empty($selectBox))
{
    echo "<p>You didn't select any member of staff to be assigned.";
    echo "<p><input type='button' value='Go Back' onClick='history.go(-1)'>";
} else
    {
        echo "<p> You selected ".$count. " staff for this show.";

        for ($i=0;$i<$count;$i++) 
        {
            $selectId[$i] = $selectBox[$i];

            //insert the details into the Job table in the database
            $insertJob = "INSERT INTO Job (JobDate, TimePeriod, EventId, StaffId) 
                            VALUES ('".$selectedDate."', '".$timePeriod."', ".$eventId.", 
                            ".$selectId[$i].")";
            $exeinsertJob = mysql_query($insertJob) or die (mysql_error());


        }
    }   

    //display the staff details (last inserted) assigned to the job
    $insertedlist = "SELECT Staff.LastName, Staff.FirstName, Job.JobDate, Job.TimePeriod
                        FROM Staff, Job
                        WHERE Job.StaffId = Staff.StaffId
                        AND Job.JobDate = ".$selectedDate;
    $exeinsertedlist = mysql_query($insertedlist) or die (mysql_error());

    if ($exeinsertedlist) 
    {
        echo "<p><table cellspacing='1' cellpadding='3'>";
        echo "<tr><th>Last Name</th><th>First Name </th><th>Date</th><th>Hours</th></tr>";

        while ($joblistarray = mysql_fetch_array($exeinsertedlist))
                {                   
                echo "<tr><td align=center>".$joblistarray['LastName']."
                    </td><td align=center>".$joblistarray['FirstName']."
                    </td><td align=center>".$joblistarray['JobDate']."
                    </td><td align=center>".$joblistarray['TimePeriod']."</td></tr>";
                }

            echo "</table>";

            echo "<h3><a href=AssignStaff.php>Add More Staff?</a></h3>";
        }
        else {
                    echo "The Job list can not be displayed at this time. Try again.";
                    echo "<p><input type='button' value='Go Back' onClick='history.go(-1)'>";
                }




echo "</center>";   
echo "</fieldset>";
echo "</form>"; 

2 个答案:

答案 0 :(得分:0)

你有mysql_inserted_id来获取最后插入的作业ID。

http://php.net/manual/en/function.mysql-insert-id.php

答案 1 :(得分:0)

要注意的几个问题:

  • 您的Job.JobDate值可能需要在insertedlist查询中用引号括起来(除非这是时间戳)。
  • 您在哪里设置$eventname变量?