似乎无法让bindValue()
在第158行工作到161(谈论函数'messageUpdate'
内部的那些),得到错误:
PHP Parse错误:语法错误,意外':',期待','或')'
帮助将不胜感激,代码在下面。我试图让用户编辑他们的消息,如果它与他们的用户名匹配,然后使用它被提交回同一文件的表单。
<?php
class editMessage {
// PROPERTIES,
// Location of the message page.
public $MESSAGES_PAGE = 'messages.php';
// Location of the login page.
public $LOGIN_PAGE = '../index.php';
// Lcaotion of the logout script.
public $LOGOUT_FILE = '../login/logout.php';
// Stores the id that most likely belongs to a corresponding message.
private $id;
// Stores the matching username found from the id using an sql query.
private $MatchingUsername;
// Stores fetched message.
private $fetchedMessage;
// Store the number of rows resulting from sql query.
private $NumberofRows;
// Stores the updated message.
private $updatedMessage;
// $MatchingRowNOTFound is used to see if id can be found in database. If it is equal to one, then the id does exist.
public $MatchingRowNotFound = 0;
// $MultipleRowsFound, means that there are multiple messages with the same id (more than one). This means that there is an error.
public $MultipleRowsFound = 1;
// Maximum String length that can be inputted.
public $String_MAX_LENGTH = 191;
// CONSTRUCTOR,
function __CONSTRUCT($id, $updatedMessage) {
$this->id = (int) $id;
$this->updatedMessage = $updatedMessage;
}
// METHODS (FUNCTIONS),
private function ErrorMessage_Redirect() {
// Delay of two seconds before redirect.
$delay_seconds = 2;
// Display the following echoes if requirements where not met. Then after $delay_seconds redirect to login page.
header('refresh: '.$delay_seconds . ';url= '.$this->MESSAGES_PAGE);
echo "<center>";
echo "<span style=\"color: red;\">";
echo "<strong>",'Something went wrong could not retrieve/update message!',"</strong>";
echo "</span>";
echo "</center>";
// Stop the script.
exit;
}
// Function for submitting edited form data.
public function formSubmit() {
// For connecting to the database.
require '../../databases/personalWebserver_PDOmysqlconnect.php';
// Function(s) relating to login.
require '../login/login-logout-access_functions.php';
// Check if user is logged in, running one of the login related function(s).
LoggedInCheck($this->LOGIN_PAGE);
LastActivityLogOutCheck($this->LOGOUT_FILE);
// Fetch the matching username that matches the inputted id, also check that there isn't more than one matching row later on.
$sql_messageUsername = $conn->prepare("SELECT `name` FROM `messageInfo` WHERE `id` = :message_id");
$sql_messageUsername->bindValue(':message_id', $this->id, PDO::PARAM_INT);
$sql_messageUsername->execute();
// Save the username inside of a variable.
$this->MatchingUsername = $sql_messageUsername->fetchColumn();
// Save the rowCount inside of a variable.
$this->NumberofRows = $sql_messageUsername->rowCount();
// Check to see if $id is not empty.
if ('' === trim($this->id)) {
// Close database connection.
$conn = null;
// Redirect to messages page, because the id is somehow empty.
header('Location: '.$this->MESSAGES_PAGE . '?Idempty');
// Stop the script.
exit;
// Check to see if there is precisely one matching (user)name, belonging to the id number.
} else if ($this->NumberofRows == $this->MatchingRowNotFound || $this->NumberofRows > $this->MultipleRowsFound) {
// Close database connection.
$conn = null;
// See the function above.
$this->ErrorMessage_Redirect();
} else {
// Check to see whether the user inputted id, matches the current username session.
if ($this->MatchingUsername === $_SESSION['username']) {
// Fetch the message from database for editing purposes.
$sql_fetchMessage = $conn->prepare("SELECT `message` FROM `messageInfo` WHERE `id` = :message_id AND `name` = :username");
$sql_fetchMessage->bindValue(':message_id', $this->id, PDO::PARAM_INT);
$sql_fetchMessage->bindValue(':username', $_SESSION['username'], PDO::PARAM_STR);
$sql_fetchMessage->execute();
// Stores the message inside of property.
$this->fetchedMessage = $sql_fetchMessage->fetchColumn();
// Close database connection.
$conn = null;
?>
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<title>Edit message</title>
<link rel="stylesheet" type="text/css" href="../assets/styles/edit_message.css">
<link rel="shortcut icon" href="../assets/images/edit-message_icon.png">
</head>
<body>
<div class="container">
<div class="wrapper">
<div id="messageEdit_wrapper">
<div class="messageEdit">
<h2><em>Edit Message Form</em></h2>
<form action="<?= htmlspecialchars($_SERVER['PHP_SELF']) ?>" method="post" accept-charset="UTF-8">
<label for="editedMessage">Edit message<span style="color: red;">*</span>:</label>
<input type="text" name="editedMessage" value="<?= htmlspecialchars($this->fetchedMessage) ?>" maxlength="191"/>
<button type="submit" name="submit_edited-message"><strong>Send</strong></button>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
<?php
} else {
// Close database connection.
$conn = null;
// See the function above.
$this->ErrorMessage_Redirect();
}
}
}
public function messageUpdate() {
// Function(s) relating to login.
require '../login/login-logout-access_functions.php';
// Check if user is logged in, running one of the login related function(s).
LoggedInCheck($this->LOGIN_PAGE);
LastActivityLogOutCheck($this->LOGOUT_FILE);
// Check if message isn't empty.
if ('' === trim($this->updatedMessage)) {
// Redirect to messages page, because the id is somehow empty.
header('Location: '.$this->MESSAGES_PAGE . '?Editempty');
// Stop the script.
exit;
// Check if message doesn't exceed certain length.
} else if (strlen($this->updatedMessage) > $this->String_MAX_LENGTH) {
// Redirect to messages page, because the id is somehow empty.
header('Location: '.$this->MESSAGES_PAGE . '?EditStringLengthExceeded');
// Stop the script.
exit;
} else {
// For connecting to the database.
require '../../databases/personalWebserver_PDOmysqlconnect.php';
// Update database table with new message.
$sql_messageUpdate = $conn->prepare("UPDATE `messageInfo` SET `message` = :updatedMessage WHERE `ID` = :message_id AND `username` = :username");
$sql_messageUpdate->bindValue(':updatedMessage', $this->updatedMessage, PDO::PARAM:STR);
$sql_messageUpdate->bindValue(':message_id', $this->id, PDO::PARAM:INT);
$sql_messageUpdate->bindValue(':username', $_SESSION['username'], PDO::PARAM:STR);
// Check if message is actually updated.
if ($sql_messageUpdate->execute()) {
// Close database connection.
$conn = null;
// Redirect to messages page, because the id is somehow empty.
header('Location: '.$this->MESSAGES_PAGE . '?EditSuccess');
// Stop the script.
exit;
} else {
// Close database connection.
$conn = null;
// Redirect to messages page, because the id is somehow empty.
header('Location: '.$this->MESSAGES_PAGE . '?EditFailure');
// Stop the script.
exit;
}
}
}
}
// Check if info was submitted.
if (isset($_POST['submit_edited-message'])) {
// Calling above class 'editMessage', and passing along value.
$editMessage = new editMessage($_GET['id'], $_POST['editedMessage']);
// Run function inside of class.
$editMessage->messageUpdate();
} else {
// Calling above class 'editMessage', and passing along value.
$editMessage = new editMessage($_GET['id'], '');
// Check if id is set.
if (isset($_GET['id'])) {
// Executing function inside of class above.
$editMessage->formSubmit();
} else {
header('Location: '.$editMessage->MESSAGES_PAGE);
exit;
}
}
?>