2013-02-26 22 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