連絡先というクラスがあります。このクラスには、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テーブルに2番目のビットは追加されません。これが私のビューページです:
<?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: ./');
}
?>