2013-02-26 49 views
0

我有一个叫联系人的类。在这个类中,我有一个名为addContact()的方法。第一个语句执行正确,但它似乎没有得到$ db-> lastInsertId()。需要一些帮助。这里是我的代码:PHP类:不插入数据:第二个查询

 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: ./'); 
} 

>

+0

你能告诉我们我们需要的代码吗?你能把它分解成一个更简单的用例吗? – 2013-02-26 02:00:37

回答

0

从上PDO

的文档

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

注:

这种方法可能不会返回跨越不同PDO驱动程序有意义或一致的结果,因为底层的数据库甚至可能不 支持自动递增字段或序列的概念。

没有看到的模式没有办法知道,但它可能是你没有一个自动递增字段在数据库中,以便不被返回的插入ID。在这种情况下,你的第二块代码会失败,但第一块会成功。

+0

我的数据库表在两个名为id的表上都有自动增量。该id是个人信息表将被用作contactinfo表中的contact_id,其也具有自动递增的id字段。这几乎就好像代码在尝试执行查询之前不会接收最后插入的内容。 '$ contact_id = $ db-> lastInsertId();' – Willem 2013-02-26 02:49:56

+0

您是否在ID字段上创建了一个强制唯一性的索引?在插入行之前,不应该从调用返回,所以如果模式正确,您应该返回一个ID。你可以添加表的模式到你的问题? – 2013-02-26 15:57:24

+0

这里是我如何创建第二个表: 'CREATE TABLE IF NOT EXISTS'contactinfo'('id' int(11)NOT NULL AUTO_INCREMENT,'contact_id' int(11)NOT NULL,'streetnumber' int(11)NOT NULL,'streetname' varchar(255)NOT NULL,'suburbname' varchar(255)NOT NULL,'cityname' varchar(255)NOT NULL,emailhome' varchar(255)NOT NULL,'emailwork' varchar(255)NOT NULL ,'omephone'varchar(15)NOT NULL,'cellphone' varchar(15)NOT NULL, 'workphone' varchar(15)NOT NULL,'deleted' int(11)NOT NULL DEFAULT'0',PRIMARY KEY id'))ENGINE = InnoDB的默认字符集= LATIN1 AUTO_INCREMENT = 3;' – Willem 2013-02-26 19:22:32