表单无法更新php

时间:2013-12-20 12:50:15

标签: php mysql

我正在尝试使用下面的表单执行更新,但是,每次我尝试更新表单时,我都会收到“检测到空字段”错误但是,我似乎无法找到错误。我可以帮忙吗。

<form action="HR_core_updatepersonnel.php?fighterID=<?php echo $rows->fighterID; ?>" method="post" class="personnel_form">

<fieldset class="group">
    <div class="col">
        <p class="text">
            <label for="first Name">First Name</label> 
            <input type="text" name="firstname" value="<?php echo $rows->firstName; ?>" placeholder="John" required>
        </p>
        <p class="text">
            <label for="Middle Name">Middle Name</label> 
            <input type="text" name="middlename" placeholder="Karr" value="<?php echo $rows->middleName; ?>" required>
        </p>
        <p class="text">
            <label for="Last Name">Last Name</label> 
            <input type="text" name="lastname" placeholder="Levy" value="<?php echo $rows->lastName; ?>" required>
        </p>
        <p class="text">
            <label for="Addres">Address</label> 
            <input type="text" class="address" value="<?php echo $rows->address; ?>" name="address" required>
        </p>
    </div>
    <div class="col">
        <p class="text">
            <label for="Home No">Home No</label> 
            <input type="number" name="home_num" value="<?php echo $rows->telephone1; ?>" required>
        </p>
        <p class="text">
            <label for="Cell No">Cell No</label> 
            <input type="number" name="cell_num" value="<?php echo $rows->telephone2; ?>" required>
        </p>
        <p class="text">
            <label for="Email">Email</label> 
            <input type="email" name="email" value="<?php echo $rows->emailAddress; ?>" placeholder="johnkarr@example.com" required>
        </p>
        <p class="text">
            <label for="TRN">TRN</label> 
            <input type="number" name="trn" value="<?php echo $rows->taxRegistrationNo; ?>" required>
        </p>
    </div>     
</fieldset>

<fieldset class="group">
    <legend>Department Information</legend>
    <div class="col">
        <p class="text">
            <label for="Hire Date">Hire Date</label> 
            <input type="date" name="hiredate" id="datepicker" value="<?php echo $rows->hireDate; ?>" placeholder="YYYY-MM-DD" required>
        </p>
        <p class="text">
            <label for="Station">Station</label>
            <?php
            $locationID = $rows->StationLocation_locationID;
            $location = mysql_query("SELECT * FROM `stationlocation`");
            echo '<select name="location" required>';
            while($row = mysql_fetch_array($location)){
                if($row['locationID'] == $locationID)
                    echo '<option selected>'. $row['exactlocation'].'</option>';
                else
                    echo '<option>'. $row['exactlocation'].'</option>';
            }
            echo '</select>';
            ?>
        </p>
        <p class="text">
            <label for="Status">Status</label>
            <?php 
            $status = $rows->status;

            $check0 = $check1 = "";
            if ($status == "Yes") 
            { 
                $check0 = " CHECKED"; 
            } 
            else 
            { 
                $check1 = "CHECKED"; 
            }          
            echo '<input type="radio" name="status" value="Active"'.$check0.'>Active';
            echo '<input type="radio" name="status" value="Inactive"'.$check1.'>Inactive';
            ?>
        </p>
    </div>
    <div class="col">
        <p class="text">
            <label for="Work Shift">Work Shift</label> 
            <?php
            $table_name = 'firefighterinfo';
            $column_name = 'workShift';

            $workShift = $rows->workShift;
            echo "<select name=\"$column_name\"><option></option>";
            $result = mysql_query("SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS
                                  WHERE TABLE_NAME = '$table_name' AND COLUMN_NAME = '$column_name'") or die (mysql_error());

            while($row = mysql_fetch_array($result))
            {
                $enumList = explode(",", str_replace("'", "", substr($row['COLUMN_TYPE'], 5, (strlen($row['COLUMN_TYPE'])-6))));

                foreach($enumList as $value){
                    if($workShift == $value)
                        echo '<option selected>'.$value.'</option>';
                    else
                         echo '<option>'.$value.'</option>';
                }
            }

            echo "</select>";
            ?>
        </p>
        <p class="text">
            <label for="Rank">Rank</label>
            <?php
            $rankID = $rows->Rank_rankID;
            $rank = mysql_query("SELECT * FROM `rank`");
            echo '<select name="rank" required>';
            while($row = mysql_fetch_array($rank))
            {
                if($row['rankID'] == $rankID)
                    echo '<option selected>'. $row['rank']. '</option>';
                else
                    echo '<option>'. $row['rank']. '</option>';
            }
            echo '</select>';

            ?>
        </p>
        <p class="text">
            <label for="Transferred">Transferred</label>
            <?php
            $transfer = $rows->transfer;

            $check0 = $check1 = "";
            if ($transfer == "Yes") 
            { 
                $check0 = " CHECKED"; 
            } 
            else 
            { 
                $check1 = "CHECKED"; 
            }          
            echo '<input type="radio" name="transferred" id="transferred1" value="Yes"'.$check0.'>Yes';
            echo '<input type="radio" name="transferred" id="transferred2" value="No"'.$check1.'>No';
            ?>
        </p>
    </div>
</fieldset>

<fieldset class="group" id="optionGroup">
    <legend>Transfer</legend>
    <div id="mydiv">
    <div class="col">
        <p class="text">
            <label for="Transferred To">Transferred To</label>
            <?php
             $transferID = $rows2->transferToStation;
             $transferTo = mysql_query("SELECT * FROM `stationlocation`");
             echo '<select name="transferTo" required>';
             while($row = mysql_fetch_array($transferTo))
             {
                 if($row['locationID'] == $transferID)
                     echo '<option selected>'.$row['exactlocation']. '</option>';
                 else
                     echo '<option>'. $row['exactlocation']. '</option>';
             }
             echo '</select>';
            ?>
        </p>
        <p class="text">
            <label for="Date Transferred">Date Transferred</label>
            <input type="date" name="dateTransferredTo" value="<?php echo $rows2->transferToDate; ?>" id="datepicker1" placeholder="YYYY-MM-DD" >
        </p>         
    </div>
    <div class="col">
        <p class="text">
            <label for="Transferred From">Transferred From</label>
            <?php
            $transferID = $rows2->transferFromStation;
            $transferFrom = mysql_query("SELECT * FROM `stationlocation`");
             echo '<select name="transferFrom" required>';
             while($row = mysql_fetch_array($transferFrom))
             {
                 if($row['locationID'] == $transferID)
                     echo '<option selected>'.$row['exactlocation']. '</option>';
                 else
                     echo '<option>'. $row['exactlocation']. '</option>';
             }
             echo '</select>';
            ?>
        </p>
        <p class="text">
            <label for="Date Transferred">Date Transferred</label>
            <input type="date" name="dateTransferredFrom" value="<?php echo $rows2->transferFromDate; ?>" id="datepicker2" placeholder="YYYY-MM-DD" >
        </p>
        <p class="text">
            <label for="Rank Transferred With">Rank Transferred With</label>
            <?php
            $rankID = $rows2->rankTransferredWith;
            $rank = mysql_query("SELECT * FROM `rank`");
            echo '<select name="rankTransferredWith" required>';
            while($row = mysql_fetch_array($rank))
            {
                if($row['rankID'] == $rankID)
                    echo '<option selected>'. $row['rank']. '</option>';
                else
                    echo '<option>'. $row['rank']. '</option>';
            }
            echo '</select>';
            ?>
        </p>
    </div>
    </div>
</fieldset>

<fieldset class="group">
    <legend>Characteristics</legend>
    <div class="col">
        <p class="text">
            <label for="D.O.B">D.O.B</label>
            <input type="date" name="dob" id="datepicker3" value="<?php echo $rows->dateOfBirth; ?>" placeholder="YYYY-MM-DD" required>
        </p>
        <p class="text">
            <label for="Marital Status">Marital Status</label>
            <?php
            $table_name = 'firefighterinfo';
            $column_name = 'maritalStatus';

            $maritalStatus = $rows->maritalStatus;
            echo "<select name=\"$column_name\">"; 
            $result = mysql_query("SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS
                                   WHERE TABLE_NAME = '$table_name' AND COLUMN_NAME = '$column_name'") or die (mysql_error());

            while($row = mysql_fetch_array($result))
            {
                $enumList = explode(",", str_replace("'", "", substr($row['COLUMN_TYPE'], 5, (strlen($row['COLUMN_TYPE'])-6))));

                foreach($enumList as $value){
                    if($maritalStatus == $value)
                        echo '<option selected>'.$value.'</option>';
                    else
                        echo '<option>'.$value.'</option>';
                }
            }

            echo "</select>";
            ?>
        </p>
        <p class="text">
            <label for="Gender">Gender</label>
            <?php
            $gender = $rows->gender;

            $check0 = $check1 = "";
            if ($gender == "Male") 
            { 
                $check0 = " CHECKED"; 
            } 
            else 
            { 
                $check1 = "CHECKED"; 
            }

            echo '<input type="radio" name="gender" value="Male"'.$check0.' >Male';
            echo '<input type="radio" name="gender" value="Female"'.$check1.' >Female';
            ?>
        </p>
    </div>
    <div class="col">
        <p class="text">
            <label for="Height">Height</label>
            <input type="text" name="height" value="<?php echo $rows->height; ?>" placeholder="185cm" required>
        </p>
        <p class="text">
            <label for="Weight">Weight</label>
            <input type="text" name="weight" value="<?php echo $rows->weight; ?>" placeholder="100lbs" required>
        </p>
    </div>
</fieldset>

<fieldset class="group">
    <legend>Next of Kin</legend>
    <div class="col">
        <p class="text">
            <label for="Next of Kin Name">Next of Kin Name</label>
            <input type="text" name="kinname" value="<?php echo $rows->kinName; ?>">
        </p>
    </div>
    <div class="col">
        <p class="text">
            <label for="Contact No.">Contact No</label>
            <input type="number" name="kinnumber" value="<?php echo $rows->kinContactNo; ?>">
        </p>
    </div>
</fieldset>

<fieldset class="group">
    <legend>Equipment Sizes</legend>
    <div class="col">
        <p class="text">
            <label for="Helmet Size">Helmet Size</label>
            <?php
            $table_name = 'firefighterinfo';
            $column_name = 'helmetSize';

            $helmetSize = $rows->helmetSize;
            echo "<select name=\"$column_name\"><option></option>";
            $result = mysql_query("SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS
                                   WHERE TABLE_NAME = '$table_name' AND COLUMN_NAME = '$column_name'") or die (mysql_error());

            while($row = mysql_fetch_array($result))
            {
                $enumList = explode(",", str_replace("'", "", substr($row['COLUMN_TYPE'], 5, (strlen($row['COLUMN_TYPE'])-6))));

                foreach($enumList as $value){
                    if($helmetSize == $value)
                        echo '<option selected>'.$value.'</option>';
                    else
                         echo '<option>'.$value.'</option>';
                }
            } 
            echo "</select>";
            ?>
        </p>
        <p class="text">
            <label for="Coat Size">Coat Size</label>
            <?php
            $table_name = 'firefighterinfo';
            $column_name = 'coatSize';

            $coatSize = $rows->coatSize;
            echo "<select name=\"$column_name\"><option></option>";
            $result = mysql_query("SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS
                                   WHERE TABLE_NAME = '$table_name' AND COLUMN_NAME = '$column_name'") or die (mysql_error());

            while($row = mysql_fetch_array($result))
            {
                $enumList = explode(",", str_replace("'", "", substr($row['COLUMN_TYPE'], 5, (strlen($row['COLUMN_TYPE'])-6))));

                foreach($enumList as $value){
                    if($coatSize == $value)
                        echo '<option selected>'.$value.'</option>';
                    else
                         echo '<option>'.$value.'</option>';
                }
            } 
            echo "</select>";
            ?>
        </p>
        <p class="text">
            <label for="Pant Size">Pant Size</label>
            <?php
            $table_name = 'firefighterinfo';
            $column_name = 'pantSize';

            $pantSize = $rows->pantSize;
            echo "<select name=\"$column_name\"><option></option>";
            $result = mysql_query("SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS
                                   WHERE TABLE_NAME = '$table_name' AND COLUMN_NAME = '$column_name'") or die (mysql_error());

            while($row = mysql_fetch_array($result))
            {
                $enumList = explode(",", str_replace("'", "", substr($row['COLUMN_TYPE'], 5, (strlen($row['COLUMN_TYPE'])-6))));

                foreach($enumList as $value){
                    if($pantSize == $value)
                        echo '<option selected>'.$value.'</option>';
                    else
                         echo '<option>'.$value.'</option>';
                }
            } 
            echo "</select>";
            ?>
        </p>
    </div>
    <div class="col">
        <p class="text">
            <label for="Boot Size">Boot Size</label>
            <?php
            $table_name = 'firefighterinfo';
            $column_name = 'bootSize';

            $bootSize = $rows->bootSize;
            echo "<select name=\"$column_name\" ><option></option>";
            $result = mysql_query("SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS
                                   WHERE TABLE_NAME = '$table_name' AND COLUMN_NAME = '$column_name'") or die (mysql_error());

            while($row = mysql_fetch_array($result))
            {
                $enumList = explode(",", str_replace("'", "", substr($row['COLUMN_TYPE'], 5, (strlen($row['COLUMN_TYPE'])-6))));

                foreach($enumList as $value){
                    if($bootSize == $value)
                        echo '<option selected>'.$value.'</option>';
                    else
                         echo '<option>'.$value.'</option>';
                }
            } 
            echo "</select>";
            ?>
        </p>
        <p class="text">
            <label for="Glove Size">Glove Size</label>
            <?php
            $table_name = 'firefighterinfo';
            $column_name = 'gloveSize';

            $gloveSize = $rows->gloveSize;
            echo "<select name=\"$column_name\" ><option></option>";
            $result = mysql_query("SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS
                                   WHERE TABLE_NAME = '$table_name' AND COLUMN_NAME = '$column_name'") or die (mysql_error());

            while($row = mysql_fetch_array($result))
            {
                $enumList = explode(",", str_replace("'", "", substr($row['COLUMN_TYPE'], 5, (strlen($row['COLUMN_TYPE'])-6))));

                foreach($enumList as $value){
                    if($gloveSize == $value)
                        echo '<option selected>'.$value.'</option>';
                    else
                         echo '<option>'.$value.'</option>';
                }
            } 
            echo "</select>";
            ?>
        </p>        
    </div>
</fieldset>

<fieldset class="group">
    <legend>Account Information</legend>
    <div class="col">
        <p class="text">
            <label for="Username">Username</label>
            <input type="text" name="username" value="<?php echo $rows3->username; ?>">
        </p>
    </div>
    <div class="col">
        <p class="text">
            <label for="Password">Password</label>
            <input type="text" name="password" value="<?php echo $rows3->password; ?>">
        </p>
    </div>
</fieldset>
<input type="submit" name="save" value="Save">


<?php
/*This section will update database record*/

//This statement checks if values in there corresponding fields are set
if(isset($_POST['firstname']) && isset($_POST['middlename']) && isset($_POST['lastname']) && isset($_POST['address']) && isset($_POST['cell_num']) && isset($_POST['email']) && isset($_POST['trn']) && isset($_POST['hiredate']) && isset($_POST['location']) && isset($_POST['status']) && isset($_POST['workShift']) && isset($_POST['rank']) && isset($_POST['transferred']) && isset($_POST['dob']) && isset($_POST['maritalStatus']) && isset($_POST['gender']) && isset($_POST['height']) && isset($_POST['weight']) && isset($_POST['helmetSize']) && isset($_POST['coatSize']) && isset( $_POST['pantSize']) && isset($_POST['bootSize']) && isset($_POST['gloveSize']) && isset($_POST['username']) && isset($_POST['password']))
{

$password_hash = md5('password');

$data = array();

foreach($_POST AS $k=>$v)
{
    $data[$k] = $v;
}

// This statement checks if fields are empty
if(!empty($firstname) && !empty($middlename) && !empty($lastname) && !empty($address) && ($cell_num > 0) && !empty($email) && ($trn > 0) && !empty($hiredate) && ($location > 0) && !empty($status) && !empty($workShift) && !empty($rank) && !empty($transferred) && !empty($dob) && !empty($maritalStatus) && !empty($gender) && !empty($height) && !empty($weight) && !empty($helmetSize) && !empty($coatSize) && !empty($pantSize) && !empty($bootSize) && !empty($gloveSize) && !empty($username) && !empty($password))
{
            $updateQuery1 = "UPDATE `firefighterinfo` SET 
                                    firstName='".mysql_real_escape_string($firstname)."',
                                    middleName='".mysql_real_escape_string($middlename)."',
                                    lastName='".mysql_real_escape_string($lastname)."',
                                    address='".mysql_real_escape_string($address)."',
                                    telephone1='".mysql_real_escape_string($home_num)."',
                                    telephone2='".mysql_real_escape_string($cell_num)."',
                                    emailAddress='".mysql_real_escape_string($email)."',
                                    taxRegistrationNo='".mysql_real_escape_string($trn)."',
                                    hireDate='".mysql_real_escape_string($hiredate)."',
                                    StationLocation_locationID='".mysql_real_escape_string($location)."',
                                    status='".mysql_real_escape_string($status)."',
                                    workShift='".mysql_real_escape_string($workShift)."',
                                    Rank_rankID='".mysql_real_escape_string($rank)."',
                                    transfer='".mysql_real_escape_string($transferred)."',
                                    dateOfBirth='".mysql_real_escape_string($dob)."',
                                    maritalStatus='".mysql_real_escape_string($maritalStatus)."',
                                    gender='".mysql_real_escape_string($gender)."',
                                    height='".mysql_real_escape_string($height)."',
                                    weight='".mysql_real_escape_string($weight)."',
                                    kinName='".mysql_real_escape_string($kinname)."',
                                    kinContactNo='".mysql_real_escape_string($kinnumber)."',
                                    helmetSize='".mysql_real_escape_string($helmetSize)."',
                                    coatSize='".mysql_real_escape_string($coatSize)."',
                                    pantSize='".mysql_real_escape_string($pantSize)."',
                                    bootSize='".mysql_real_escape_string($bootSize)."',
                                    gloveSize='".mysql_real_escape_string($gloveSize)."' 
                                    WHERE `firefighterinfo`.`fighterID`='".$getID."'";

    mysql_query($updateQuery1) or die(mysql_error());

    $id = mysql_insert_id();

    if($_POST['transferred'] == 'Yes')
    {
        $updateQuery2 = "UPDATE `transfer` 
                         JOIN `firefighterinfo` 
                         ON `transfer`.`FireFighterInfo_fighterID`=`firefighterinfo`.`fighterID` SET
                                   FireFighterInfo_fighterID='".mysql_real_escape_string($id)."',
                                   transferToStation='".mysql_real_escape_string($transferTo)."',
                                   transferToDate='".mysql_real_escape_string($dateTransferredTo)."',
                                   transferFromStation='".mysql_real_escape_string($transferFrom)."',
                                   transferFromDate='".mysql_real_escape_string($dateTransferredFrom)."',
                                   rankTransferredWith='".mysql_real_escape_string($rankTransferredWith)."'
                                   WHERE `transfer`.`FireFighterInfo_fighterID`='".$getID."'";                           
        mysql_query($updateQuery2) or die(mysql_error());

    }
    else
    {
        $updateQuery2 = "UPDATE `transfer` 
                         JOIN `firefighterinfo` 
                         ON `transfer`.`FireFighterInfo_fighterID`=`firefighterinfo`.`fighterID` SET
                               FireFighterInfo_fighterID='".mysql_real_escape_string($id)."',
                               transferToStation='".mysql_real_escape_string('Not Applicable')."',
                               transferToDate='".mysql_real_escape_string('0000-00-00')."',
                               transferFromStation='".mysql_real_escape_string('Not Applicable')."',
                               transferFromDate='".mysql_real_escape_string('0000-00-00')."',
                               rankTransferredWith='".mysql_real_escape_string('Not Applicable')."'
                               WHERE `transfer`.`FireFighterInfo_fighterID`='".$getID."'";                                   
        mysql_query($updateQuery2) or die(mysql_error());

    }// end if/else statement


    $updateQuery3 = "UPDATE `user` 
                     JOIN `firefighterinfo` 
                     ON `user`.`FireFighterInfo_fighterID`=`firefighterinfo`.`fighterID` SET
                               FireFighterInfo_fighterID='".mysql_real_escape_string($id)."', 
                               username='".mysql_real_escape_string($username)."', 
                               password='".mysql_real_escape_string($password_hash)."'
                               WHERE `user`.`FireFighterInfo_fighterID`='".$getID."'";                      
    mysql_query($updateQuery3) or die(mysql_error());

    // check if queries are set
    if(isset($updateQuery1) && isset($updateQuery3))
    {
        echo '<script type="text/javascript">
                    alert("Update Successful!");
                </script>';
        header('Location:HR_core_viewpersonnel.php'); // directs user to page if update successful
    }
    else
    {
        echo '<script type="text/javascript">
                    alert("Update Unsuccessful!");
                </script>';
    }
    mysql_close(); // close mysql connection      
} // end if statement
else
{
    echo '<script type="text/javascript">
                alert("Empty Field(s) Detected!");
              </script>';
}   
}
?>

5 个答案:

答案 0 :(得分:2)

您检查空$firstname $middlename ...等,而不是$_POST['firstname']等。 如果你的php配置中的register_globals关闭(默认),那么你的方法将不起作用。

答案 1 :(得分:1)

 extract($_POST)

之前if(!empty($firstname)

这将有助于您将值$firstname用于代替$_POST['firstname']$middlename而非撰写$_POST['middlename']等等

答案 2 :(得分:0)

你的第二个if中的变量(如$firstname)永远不会被设置,所以你为什么期望那里有一些价值?

你的if条件应该是

if(!$empty($_POST['firstname']) ... )

所以用$variable替换$_POST['variable']并自己检查脚本的其余部分,因为你的sql语句也不起作用。

答案 3 :(得分:0)

如果你不能打开“register_globals”(这是不安全的那么糟糕的主意)
您需要在脚本中更改所有变量,例如
$ firstname,$ middlename,$ address,$ email ....
成:
$ data ['firstname']等....

但如果你不关心安全问题,你也可以这样做:

foreach($_POST AS $k=>$v)
{
    $data[$k] = $v;
    $$k = $v;
}

答案 4 :(得分:-1)

请替换

foreach($_POST AS $k=>$v)
{
    $data[$k] = $v;
}

foreach($_POST AS $k=>$v)
{
    $k = $v;
}

希望它能为你效劳