从一个html表单更新多个mysql表

时间:2015-01-02 11:34:41

标签: php mysql forms

我无法找到答案,所以我希望有人可以提供帮助:

我有一张表格,为丈夫的妻子孩子提供基本的个人资料。我开始时有一个大表和一个很好的mysql语句。可输入数据,更新数据库和表格,然后显示更新的数据。现在我需要知道丈夫或妻子是否是做某事的人,所以我将这三个分成了单独的表。在php中我创建了相应的类,它们在表单和mysql之间接收/检索数据。不知何故,这适用于第一个查询妻子,但后来其他两个似乎丢失了数据,虽然我看到表单中的所有$ _POST数据被发送到执行查询的类方法。我认为使用这个问题是一个问题 - >在一个表格的多个班级。这是一些代码

$logged_in = user::isLoggedIn();
if($logged_in) {
    // TODO numRows is calling a query of only husband table assuming if no husband no  wife no marital
    $numRows = user::getRowsUCintake((int)$_SESSION['user_id']);  // checks for entry in husband table
    $idNum = (int)$_SESSION['user_id'];
    if ($numRows == 0) {
        // user exists but does not have an intake record
        $wife = new wife();
        $husband = new husband();
        $marital = new marital();
    } else {
        // contact exists and has intake record
        $wife = wife::getWife($idNum);
        $husband = husband::getHusband($idNum);
        $marital = marital::getMarital($idNum);
    }
}else{
    echo 'Sorry you must be registered and logged in to view this page';
    exit;
}


//// large form follows this is just a few lines

    <legend id="legendWhite">Information for Wife</legend>
    <div class="form-group">
        <label class="col-sm-3 control-label" for="wifeFirstName">First Name</label>
        <input type="text" size="20" maxlength="50" class="col-sm-6 form-control" id="wifeFirstName" name="wifeFirstName"
               value="<?php echo htmlspecialchars($wife->getFirstName()); ?>" />
    </div>
    <div class="form-group">
        <label class="col-sm-3 control-label" for="wifeLastName">Last Name</label>
        <input type="text" size="20" maxlength="50" class="col-sm-6 form-control" id="wifeLastName" name="wifeLastName"
               value="<?php echo htmlspecialchars($wife->getLastName()); ?>" />
    </div>
    <div class="form-group">
        <label class="col-sm-3 control-label" for="wifeMiddleName">Middle Name</label>
        <input type="text" size="20" maxlength="50" class="col-sm-6 form-control" id="wifeMiddleName" name="wifeMiddleName"
               value="<?php echo htmlspecialchars($wife->getMiddleName()); ?>">

这是妻子类的添加和编辑方法,丈夫具有相同的不同变量名称。

     public function addWife(){
        // Get the Database connection
        $connection = Database::getConnection();

        // Prepare the data
        //  19 items
        $query = "INSERT INTO wife(id, partyCat, firstName, lastName, middleName, otherName,
    streetAddress, city, state, zip, DOB, priorMarriage, priorDivorce, ssn, employer,     employerStreet, employerCity,
    employerState, employerZip)
          VALUES ('" . Database::prep($_SESSION['user_id']) . "',
          '" . Database::prep($this->partyCat) . "',
          '" . Database::prep($this->firstName) . "',
          '" . Database::prep($this->lastName) . "',

        [cut for brevity]

        '" . Database::prep($this->employer) . "',
        '" . Database::prep($this->employerStreet) . "',
        '" . Database::prep($this->employerCity) . "',
        '" . Database::prep($this->employerState) . "',
        '" . Database::prep($this->employerZip) . "'
      )";

    //        TODO  added space to message problem
        if ($connection->query($query)) {
            $return = array('', 'Wife Record Added [307]', '');
            // add success message
            return $return;
        } else {
            // send fail message
            $return = array('', 'Wife data NOT added.', '');
            return $return;
        }

    }



the form POST goes to a functions.php file which exectes functions based on a task field sent by the POST.  In this case it is ucDivIntake.  This is part of the function

    function ucDivIntake(){
    $results = '';
    if (isset($_POST['save']) && $_POST['save'] == 'Save') {
//        check the token
        $badToken = true;
        if (!isset($_POST['token'])
            || !isset($_SESSION)
            || empty($_POST['token'])
            || $_POST['token'] !== $_SESSION['token']) {
            $results = array('', 'Sorry, go back and try again. [92]');
            $badToken = true;
        }else{
            $badToken = false;
            unset($_SESSION['token']);
            //token ok so validate form data
//            if (empty($_POST['____']))       not val for empty ALL FIELDS OPTIONAL
    //        $results= array('', 'Data submission not validated!');
            // TODO check / finish sanitize filters and set default checkboxes

            // prepare query to send to database
            $itemWife  = array ( 'id' => (int) $_POST['id'],
                 'partyCat' => filter_input(INPUT_POST,'wifePartyCat', FILTER_SANITIZE_STRING,FILTER_FLAG_NO_ENCODE_QUOTES),
                 'firstName'  => filter_input(INPUT_POST,'wifeFirstName', FILTER_SANITIZE_STRING,FILTER_FLAG_NO_ENCODE_QUOTES),
                .....  $itemHusband  = array ( 'id' => (int) $_POST['id'],
                'partyCat' => filter_input(INPUT_POST,'husbandPartyCat', FILTER_SANITIZE_STRING,FILTER_FLAG_NO_ENCODE_QUOTES),
                'firstName'  => filter_input(INPUT_POST,'husbandFirstName', FILTER_SANITIZE_STRING,FILTER_FLAG_NO_ENCODE_QUOTES),
                'lastName'  => filter_input(INPUT_POST,'husbandLastName', FILTER_SANITIZE_STRING,FILTER_FLAG_NO_ENCODE_QUOTES),

.....
      $itemHusband  = array ( 'id' => (int) $_POST['id'],
                'partyCat' => filter_input(INPUT_POST,'husbandPartyCat', FILTER_SANITIZE_STRING,FILTER_FLAG_NO_ENCODE_QUOTES),
                'firstName'  => filter_input(INPUT_POST,'husbandFirstName', FILTER_SANITIZE_STRING,FILTER_FLAG_NO_ENCODE_QUOTES),
                'lastName'  => filter_input(INPUT_POST,'husbandLastName', FILTER_SANITIZE_STRING,FILTER_FLAG_NO_ENCODE_QUOTES),

然后通过首先检查具有该ID的行是否退出来调用new或add

 // call add or update methods to database
            // create objects based on database query of form results and either create new data row or update existing
            $newWife = new wife($itemWife);
            // getWife method queries database for record base in id
            if($newWife->getWife($newWife->getID())){
                // record exists call edit method
                $resultsWife = $newWife->editWife();
            }else{
                $resultsWife = $newWife->addWife();
            }

            $newHusband = new husband($itemHusband);
            if($newHusband->getHusband($newHusband->getID())){
                // record exists call edit method
                $resultsHusband = $newHusband->editHusband();
            }else{
                $resultsHusband = $newHusband->addHusband();
            }

0 个答案:

没有答案