PHP类:不插入数据:第二个查询

时间:2013-02-26 01:59:58

标签: php class pdo

我有一个叫做联系人的课程。在这个类中,我有一个名为addContact()的方法。第一个语句执行正确,但似乎没有得到$ db-> lastInsertId()。需要一些帮助。这是我的代码:

        public function addContact($addcontactfirstname,$addcontactmiddlename,$addcontactlastname,$addcontactstreetnumber, $addcontactstreetname, $addcontactsuburb, $addcontactcity, $addcontactemailhome, $addcontactemailwork,$addcontacthomephone, $addcontactcellphone, $addcontactworkphone){
        $addsuccessfully = true;
        $addcontact_id = 0;

        try {
            $db = database::databaseConnect();

            $stmt1 = $db->prepare('INSERT INTO personalinfo (firstname, middlename, lastname) VALUES (:addcontactfirstname, :addcontactmiddlename, :addcontactlastname)');
            $stmt1->bindParam(':addcontactfirstname', $addcontactfirstname, PDO::PARAM_STR);
            $stmt1->bindParam(':addcontactmiddlename', $addcontactmiddlename, PDO::PARAM_STR);
            $stmt1->bindParam(':addcontactlastname', $addcontactlastname, PDO::PARAM_STR);

            $successful1 = $stmt1->execute();
            $addcontact_id = $db->lastInsertId();

            if($successful1){
                //$addcontact_id = $db->lastInsertId();
                $successful1 = true;

                $stmt2 = $db->prepare('INSERT INTO contactinfo (contact_id, streetnumber, streetname, suburbname, cityname, emailhome, emailwork, homephone, cellphone, workphone) VALUES (:addcontact_id, :addcontactstreetnumber, addcontactstreetname, :addcontactsuburb, :addcontactcity, :addcontactemailhome, :addcontactemailwork,:addcontacthomephone, :addcontactcellphone, :addcontactworkphone)');
                $stmt2->bindParam(':addcontact_id', $addcontact_id, PDO::PARAM_INT);
                $stmt2->bindParam(':addcontactstreetnumber', $addcontactstreetnumber, PDO::PARAM_STR);
                $stmt2->bindParam(':addcontactstreetname', $addcontactstreetname, PDO::PARAM_STR);
                $stmt2->bindParam(':addcontactsuburb', $addcontactsuburb, PDO::PARAM_STR);
                $stmt2->bindParam(':addcontactcity', $addcontactcity, PDO::PARAM_STR);
                $stmt2->bindParam(':addcontactemailhome', $addcontactemailhome, PDO::PARAM_STR);
                $stmt2->bindParam(':addcontactemailwork', $addcontactemailwork, PDO::PARAM_STR);
                $stmt2->bindParam(':addcontacthomephone', $addcontacthomephone, PDO::PARAM_STR);
                $stmt2->bindParam(':addcontactcellphone', $addcontactcellphone, PDO::PARAM_STR);
                $stmt2->bindParam(':addcontacthomephone', $addcontactworkphone, PDO::PARAM_STR);

                $successful2 = $stmt2->execute();

                if($successful2){
                    $successful2 = true;
                }

                if(!$successful1 && !$successful2){
                    $addsuccessfully = false;
                }

            }

            if($successful1 === true && $successful2 === true){
                $addsuccessfully = true;
            }
        } 

        catch (PDOException $e) {
            $addsuccessfully = false;
        }

        return $addsuccessfully;
    }

我有一个从我的视图页面调用的函数。这是我的功能:

     function addContact($addcontactfirstname,$addcontactmiddlename,$addcontactlastname,$addcontactstreetnumber, $addcontactstreetname, $addcontactsuburb, $addcontactcity, $addcontactemailhome, $addcontactemailwork,$addcontacthomephone, $addcontactcellphone, $addcontactworkphone){
     global $addsuccessfully;
     contacts::addContact($addcontactfirstname,$addcontactmiddlename,$addcontactlastname,$addcontactstreetnumber, $addcontactstreetname, $addcontactsuburb, $addcontactcity, $addcontactemailhome, $addcontactemailwork,$addcontacthomephone, $addcontactcellphone, $addcontactworkphone);
     return $addsuccessfully;
 }

这是我调用该函数的页面。页面确实表示无法添加联系人。我知道第一个查询的工作方式与联系人在数据库中显示的一样,但它不会将第二个位添加到数据库的contactinfo表中。这是我的观点页面:

<?php
/*The first thing that need to take place on this page is to ensure that the $admin value = 1.
 * If the value is not 1 the user will get redirected to the home page.  If the value of
 * $admin = null, it then indicates that the user is not logged in.  The system will then tell the
 * user that he need to logon first, but also warn the user that if he is not an admin user he won't be
 * allowed access to this page.  This is to ensure that the user don't type the url address in 
 * his browser to try and access this page.  This means that only admin users will be able to 
 * view this page while logged on and will be able to add new users.  This will be an admin
 * protected page.  Protcted so the user must be logged in and and admin user.
 */
ini_set('display_errors', 1);
error_reporting(E_ALL);

require_once 'functions/functions.php';
checkLoggedIn(page::ADDCONTACT); 

echo $message;

if ($pageID == 1){
    require_once 'includes/adminmenu.php';

    if($_POST){
        $addcontactfirstname = $_POST['addcontactfirstname'];
        $addcontactmiddlename = $_POST['addcontactmiddlename'];
        $addcontactlastname = $_POST['addcontactlastname'];
        $addcontactstreetnumber = $_POST['addcontactstreetnumber'];
        $addcontactstreetname = $_POST['addcontactstreetname'];
        $addcontactsuburb = $_POST['addcontactsuburb'];
        $addcontactcity = $_POST['addcontactcity'];
        $addcontactemailhome = $_POST['addcontactemailhome'];
        $addcontactemailwork = $_POST['addcontactemailwork'];
        $addcontacthomephone = $_POST['addcontacthomephone'];
        $addcontactcellphone = $_POST['addcontactcellphone'];
        $addcontactworkphone = $_POST['addcontactworkphone'];
        $errors = array();
        $homephonelength = false;
        $cellphonelength = false;
        $workphonelength = false;
        //$addsuccessfully = true;


        stripUserInput($addcontactfirstname,$addcontactmiddlename,$addcontactlastname,$addcontactstreetnumber,$addcontactstreetname,$addcontactsuburb,$addcontactcity,$addcontactemailhome,$addcontactemailwork,$addcontacthomephone,$addcontactcellphone,$addcontactworkphone);

        if(empty($addcontactfirstname)){
            $errors[] = 'First name can\'t be empty!';
        }

        if(empty($addcontacthomephone) && empty($addcontactcellphone) && empty($addcontactworkphone)){
            $errors[] = 'You must enter at least one telephone number!';
        }             

        if(!empty($addcontacthomephone)){
            //$phonenumber = $addcontacthomephone;
            $homephonelength = chechPhoneLenght($addcontacthomephone);

            if($homephonelength === true){
                $errors[] = 'The home phone number you entered is too short!';
            }
        }

        if(!empty($addcontactcellphone)){
            //$phonenumber = $addcontactcellphone;
            $cellphonelength = chechPhoneLenght($addcontactcellphone);

            if($cellphonelength === true){
                $errors[] = 'The mobile phone number you entered is too short!';
            }
        }

        if(!empty($addcontactworkphone)){
            //$phonenumber = $addcontactworkphone;
            $workphonelength = chechPhoneLenght($addcontactworkphone);

            if($workphonelength === true){
                $errors[] = 'The work phone number you entered is too short!';
            }
        }            

        if(!empty($addcontactemailhome)){
            $email = $addcontactemailhome;
            is_valid_email($email);

            if (is_valid_email($email) === false){
                $errors[] = 'You have entered an invalid home email address!';
            }
        }

        if(!empty($addcontactemailwork)){
            $email = $addcontactemailwork;
            is_valid_email($email);

            if(is_valid_email($email) === false){
                $errors[] = 'You have entered an invalid work email address!';
            }
        }

        if(empty($errors)){
            //Add the contact
            $addsuccessfully = addContact($addcontactfirstname,$addcontactmiddlename,$addcontactlastname,$addcontactstreetnumber, $addcontactstreetname, $addcontactsuburb, $addcontactcity, $addcontactemailhome, $addcontactemailwork,$addcontacthomephone, $addcontactcellphone, $addcontactworkphone);

            if($addsuccessfully === true){
                echo 'New contact added successfully!';
            }else{

                echo 'New contact could not be add.  Please go <a href="addcontact.php">back</a> and try again!';
            }
        }else{
            echo '<b>Please fix the following errors and try again!</b><br>';
            foreach ($errors as $key => $error_message){
                echo '<font color="red"><em>' . $error_message . '</font></em><br>';
            }
            ?>

            <h1>Add new contact</h1>
            <p><em>Fields marked with <font color="red">*</font> must be completed.</em></p>
            <form action="addcontact.php" method="post">
                <table cellpadding="5">
                    <tr>
                        <td>
                            <b>First name:</b> <font color="red">*</font>
                        </td>
                        <td>
                            <input type="text" name="addcontactfirstname" value="<?php echo $addcontactfirstname; ?>" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <b>Middle name:</b>
                        </td>
                        <td>
                            <input type="text" name="addcontactmiddlename" value="<?php echo $addcontactmiddlename; ?>" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <b>Last name:</b>
                        </td>
                        <td>
                            <input type="text" name="addcontactlastname" value="<?php echo $addcontactlastname; ?>" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <b>Street number:</b>
                        </td>
                        <td>
                            <input type="text" name="addcontactstreetnumber" value="<?php echo $addcontactstreetnumber; ?>" />
                        </td>
                    </tr> 
                    <tr>
                        <td>
                            <b>Street name:</b>
                        </td>
                        <td>
                            <input type="text" name="addcontactstreetname" value="<?php echo $addcontactstreetname; ?>" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <b>Suburb:</b>
                        </td>
                        <td>
                            <input type="text" name="addcontactsuburb" value="<?php echo $addcontactsuburb; ?>" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <b>City:</b>
                        </td>
                        <td>
                            <input type="text" name="addcontactcity" value="<?php echo $addcontactcity; ?>" />
                        </td>              
                    </tr>
                    <tr>
                        <td>
                            <b>Email (H):</b>
                        </td>
                        <td>
                            <input type="text" name="addcontactemailhome" value="<?php echo $addcontactemailhome; ?>" />
                        </td>
                    </tr>    
                    <tr>
                        <td>
                            <b>Email (W):</b>
                        </td>
                        <td>
                            <input type="text" name="addcontactemailwork" value="<?php echo $addcontactemailwork; ?>" />
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <font color="blue"><em><b>NOTE:</b> You must enter at least one telephone number.</em><br>  The number must include the area code e.g 065553322!</font>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <b>Phone (H):</b>
                        </td>
                        <td>
                            <input type="text" name="addcontacthomephone" value="<?php echo $addcontacthomephone; ?>" />
                        </td>
                    </tr> 
                    <tr>
                        <td>
                            <b>Mobile:</b>
                        </td>
                        <td>
                            <input type="text" name="addcontactcellphone" value="<?php echo $addcontactcellphone; ?>" />
                        </td>
                    </tr> 
                    <tr>
                        <td>
                            <b>Phone (W):</b>
                        </td>
                        <td>
                            <input type="text" name="addcontactworkphone" value="<?php echo $addcontactworkphone; ?>" />
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2" align="right">
                            <input type="submit" value="Add contact" value="<?php echo $addcontactfirstname; ?>" />
                        </td>
                    </tr>
                </table>
            </form>


            <?php
        }

    }else{

    ?>

    <h1>Add new contact</h1>
    <p><em>Fields marked with <font color="red">*</font> must be completed.</em></p>
    <form action="addcontact.php" method="post">
        <table cellpadding="5">
            <tr>
                <td>
                    <b>First name:</b> <font color="red">*</font>
                </td>
                <td>
                    <input type="text" name="addcontactfirstname" />
                </td>
            </tr>
            <tr>
                <td>
                    <b>Middle name:</b>
                </td>
                <td>
                    <input type="text" name="addcontactmiddlename" />
                </td>
            </tr>
            <tr>
                <td>
                    <b>Last name:</b>
                </td>
                <td>
                    <input type="text" name="addcontactlastname" />
                </td>
            </tr>
            <tr>
                <td>
                    <b>Street number:</b>
                </td>
                <td>
                    <input type="text" name="addcontactstreetnumber" />
                </td>
            </tr> 
            <tr>
                <td>
                    <b>Street name:</b>
                </td>
                <td>
                    <input type="text" name="addcontactstreetname" />
                </td>
            </tr>
            <tr>
                <td>
                    <b>Suburb:</b>
                </td>
                <td>
                    <input type="text" name="addcontactsuburb" />
                </td>
            </tr>
            <tr>
                <td>
                    <b>City:</b>
                </td>
                <td>
                    <input type="text" name="addcontactcity" />
                </td>              
            </tr>
            <tr>
                <td>
                    <b>Email (H):</b>
                </td>
                <td>
                    <input type="text" name="addcontactemailhome" />
                </td>
            </tr>    
            <tr>
                <td>
                    <b>Email (W):</b>
                </td>
                <td>
                    <input type="text" name="addcontactemailwork" />
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <font color="blue"><em><b>NOTE:</b> You must enter at least one telephone number.</em><br>  The number must include the area code e.g 065553322!</font>
                </td>
            </tr>
            <tr>
                <td>
                    <b>Phone (H):</b>
                </td>
                <td>
                    <input type="text" name="addcontacthomephone" />
                </td>
            </tr> 
            <tr>
                <td>
                    <b>Mobile:</b>
                </td>
                <td>
                    <input type="text" name="addcontactcellphone" />
                </td>
            </tr> 
            <tr>
                <td>
                    <b>Phone (W):</b>
                </td>
                <td>
                    <input type="text" name="addcontactworkphone" />
                </td>
            </tr>
            <tr>
                <td colspan="2" align="right">
                    <input type="submit" value="Add contact" />
                </td>
            </tr>
        </table>
    </form>
    <?php
    }
}

if ($pageID == 0){
    return header('Location: ./');
}

&GT;

1 个答案:

答案 0 :(得分:0)

来自PDO上的文档

  

string PDO :: lastInsertId([string $ name = NULL])返回ID   最后插入的行,或序列对象的最后一个值,   取决于底层驱动程序。例如,PDO_PGSQL()需要   您可以为name参数指定序列对象的名称。

     

注意:

     

此方法可能不会在不同的PDO驱动程序之间返回有意义或一致的结果,因为底层数据库可能不均匀   支持自动增量字段或序列的概念。

如果没有查看架构,则无法知道,但您的数据库中可能没有自动增量字段,因此不会返回插入ID。在这种情况下,您的第二个代码块将失败,但第一个代码块将成功。