我被一种应该能够更新日历中现有事件的信息的表单所困扰。通过表单将数据从我的 event.php 页面传输到我的 event_updated.php 页面时,我无法让它识别它是从哪个事件发送的。event_updated.php 页面应该能够识别这一点,更新数据库中的事件,然后使用新信息返回 event.php 页面。
这是 event.php 上的表单。$id-variable 是用于识别此页面上的事件的 GET 变量:
<form action="event_updated.php" method="post">
<p>Change name: <input type="text" name="event_name" value="'; if(isset($_POST['event_name'])) echo $_POST['event_name']; echo '" /><br />
New location: <input type="text" name="location" value="'; if(isset($_POST['location'])) echo $_POST['location']; echo '" /><br />
Starting (ex: 2013-05-01 18:00:00): <input type="text" name="starttime" value="'; if(isset($_POST['starttime'])) echo $_POST['starttime']; echo '" /><br />
Ending (ex: 2013-05-01 18:30:00): <input type="text" name="endtime" value="'; if(isset($_POST['endtime'])) echo $_POST['endtime']; echo '" /></p>
<input type="submit" value="Update event" />
<input type="hidden" name="this_event" value="'; $_POST['$id']; echo '" />
<input type="hidden" name="submitted" value="TRUE" />
</form>
这是 event_updated.php 中的代码:
<?php
$page_title = "The event has been updated";
include ('../includes/topnavigation.php');
require_once ('../mysqli_connect.php');
session_start(); // Start session
// If no session value is set then they are redirected to index.php
if (!isset($_SESSION['user_id'])) {
require_once ('../includes/login_functions.inc.php');
$url = absolute_url();
header("Location: $url");
exit(); // Exit script
}
require_once ('../mysqli_connect.php'); // Connect to DB
//Check if the form has been submitted
if (isset($_POST['submitted'])) {
$errors = array(); //Initialize an error array
// Check for event name
if (empty($_POST['event_name'])) {
$errors[] = 'You have not given your event a name.';
} else {
$en = trim($_POST['event_name']);
}
// Check for event name
if (empty($_POST['this_event'])) {
$errors[] = 'this_event not transferred correctly.';
} else {
$en = trim($_POST['this_event']);
}
// Check for event location
if (empty($_POST['location'])) {
$errors[] = 'You have not determined where the meeting is taking place.';
} else {
$l = trim($_POST['location']);
}
if (empty($_POST['starttime'])) {
$errors[] = 'You have not determined when the meeting is starting.';
} else {
$s = trim($_POST['starttime']);
$s = strtotime($s);
$s = date('Y-m-d H:i:s',$s);
}
if (empty($_POST['endtime'])) {
$errors[] = 'You have not determined when the meeting is ending.';
}
else {
$e = trim($_POST['endtime']);
$e = strtotime($e);
$e = date('Y-m-d H:i:s',$e);
}
if (empty($errors)) { // If everything is ok
// Create the event in the database
require_once('../mysqli_connect.php'); // DB connect
$tbl_name="events";
$sql="UPDATE events SET creator_id='{$_SESSION['user_id']}',event_name=event_name, ,endtime=endtime,starttime=starttime,duration=duration,location=location WHERE this_event=event_id";
$result= @mysqli_query($dbc, $sql);
if($result){
// retrieve event from database
$user_id = $_SESSION['user_id'];
echo $user_id;
$event = "SELECT * FROM events e WHERE e.creator_id = {$_SESSION['user_id']} AND e.event_name = '$en'";
$r = @mysqli_query ($dbc, $event); // Run the query
if ($r) {
$row = mysqli_fetch_array($r, MYSQLI_ASSOC);
$event_id = $row['event_id'];
echo $event_id;
header("Location: event.php?id=$event_id"); // $row['event_name']
} else {
echo mysqli_error($dbc);
}
echo "Succesfull";
echo "<BR>";
echo "<a href='newevent.php'>Back to main page</a>";
} else {
echo mysqli_error($dbc);
echo "ERROR";
echo $result;
echo '<h2>Error!</h2>
<p class="error">The following error(s) occured:<br />';
foreach ($errors as $msg) { // Print each error
echo " - $msg<br />\n";
}
}
mysqli_close($dbc);
}
else {
echo '<h2>Error!!!</h2>
<p class="error">The following error(s) occured:<br />';
foreach ($errors as $msg) { // Print each error
echo " - $msg<br />\n";
}
}
}
?>
event.php 的完整代码:
<?php
$page_title = 'View event';
include ('../includes/topnavigation.php');
session_start(); // Start session
// If no session value is set then they are redirected to index.php
if (!isset($_SESSION['user_id'])) {
require_once ('../includes/login_functions.inc.php');
$url = absolute_url();
header("Location: $url");
exit(); // Exit script
}
$id = $_GET['id'];
$loggedin = $_SESSION['user_id'];
require_once ('../mysqli_connect.php'); // Connect to DB
$event = "SELECT * FROM events e, users u WHERE u.user_id = e.creator_id AND e.event_id = $id";
$participants = "SELECT u.first_name, u.last_name FROM users u WHERE u.user_id IN (SELECT user FROM participants WHERE event = $id)";
$creator = "SELECT creator_id FROM events";
$add = "SELECT user_id as UserID, CONCAT(first_name, ' ', last_name) AS Name FROM users ORDER BY reg_date DESC";
$r = @mysqli_query ($dbc, $event); // Run the query
if ($r) { // If OK, display event
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
// Table with event info
echo '<h2>Event: ' . $row['event_name'] . '</h2>
<table>
<tr>
<td align="left" valign="top">
<table align="left" cellspacing="0" cellpadding="5">
<tr>
<td align="left">Date:</td><td align="left">' . $row['date'] . '</td>
</tr>
<tr>
<td align="left">Start:</td><td align="left">' . $row['starttime'] . '</td>
</tr>
<tr>
<td align="left">Duration:</td><td align="left">' . $row['duration'] . '</td>
</tr>
<tr>
<td align="left">Location:</td><td align="left">' . $row['location'] . '</td>
</tr>
<tr>
<td align="left">Creator:</td><td align="left"><a href="usermeetings.php?id='. $row['creator_id'] . '">' . $row['first_name'] . ' ' . $row['last_name'] . '</a></td>
</tr>';
}
mysqli_free_result ($r); // Free up the ressources
} else { // If failure
echo '<p class="error">Event info could not be retrieved. We apologize for any inconveniences.</p>';
// Debugging message:
echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $event . '</p>';
} // End of IF
$r = @mysqli_query ($dbc, $participants); // Run the query
if ($r) { // If OK, display event
echo '
<tr>
<td align="left">All participants:</td>
<td align="left">';
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
echo $row['first_name'] . ' ' . $row['last_name'] . '<br />';
}
mysqli_free_result ($r); // Free up the ressources
} else { // If failure
echo '<p class="error">Could not retrieve list of attending users. We apologize for any inconveniences.</p>';
// Debugging message:
echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $participants . '</p>';
} // End of IF
echo '
</td>
</tr>
</table>'; // Table is closed
echo '</td>
<td align="left" valign="top">';
$r = @mysqli_query ($dbc, $add);
if ($r) {
// Table header
echo '<h3>You have created this event</h3>
<p>Add participants, update info or simply delete it: <a href="deleteevent.php?id=' . $id .'">DELETE</a></p>
<p><b>Update event</b></p>
<form action="event_updated.php" method="post">
<p>Change name: <input type="text" name="event_name" value="'; if(isset($_POST['event_name'])) echo $_POST['event_name']; echo '" /><br />
New location: <input type="text" name="location" value="'; if(isset($_POST['location'])) echo $_POST['location']; echo '" /><br />
Starting (ex: 2013-05-01 18:00:00): <input type="text" name="starttime" value="'; if(isset($_POST['starttime'])) echo $_POST['starttime']; echo '" /><br />
Ending (ex: 2013-05-01 18:30:00): <input type="text" name="endtime" value="'; if(isset($_POST['endtime'])) echo $_POST['endtime']; echo '" /></p>
<input type="submit" value="Update event" />
<input type="hidden" name="this_event" value="'; if(isset($_POST['$id'])) echo $_POST['this_event']; echo '" />
<input type="hidden" name="submitted" value="TRUE" />
</form>
<table align="left" cellspacing="3" cellpading="3">
<tr>
<td align="left"><b>Add participants</b></td>
</tr>
';
// Fetch and print records:
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
echo '<tr>
<td align="left"><a href="usermeetings.php?id='. $row['UserID'] . '">' . $row['Name'] . '</td>
<td align="left"><a href="addparticipant.php?add='. $row['UserID'] . '&id=' . $id .'">ADD</a></td>
</tr>
';
}
echo '</table>'; // Table is closed
mysqli_free_result ($r); // Free up the ressources
} else { // If failure
echo '<br clear="all" /><p class="error">The user list could not be retrieved. We apologize for any inconveniences.</p>';
// Debugging message:
echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $add . '</p>';
}
echo '</td>
</tr>
</table>';
mysqli_close($dbc); // closes DB connection
?>