使用帖子变量更新日历中的现有事件

时间:2013-05-18 10:16:27

标签: php mysql forms

我坚持使用一个应该能够更新日历中现有事件信息的表单。当通过表单从我的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

?>

2 个答案:

答案 0 :(得分:0)

<input type="submit" value="Update event" name="submit"/>

if (isset($_POST['submitted'])) {

应该是

if (isset($_POST['submit'])) {

编辑1:还

<form action="event_updated.php" method="post">
    <p>Change name: <input type="text" name="event_name" value="<?php if(isset($_POST['event_name'])) echo $_POST['event_name'];?>" /><br />
    New location: <input type="text" name="location" value="<?php if(isset($_POST['location'])) echo $_POST['location'];?>" /><br />
    Starting (ex: 2013-05-01 18:00:00): <input type="text" name="starttime" value="<?php if(isset($_POST['starttime'])) echo $_POST['starttime'];?>" /><br />
    Ending (ex: 2013-05-01 18:30:00): <input type="text" name="endtime" value="<?php  if(isset($_POST['endtime'])) echo $_POST['endtime'];?>" /></p>
    <input type="submit" value="Update event" />
    <input type="hidden" name="this_event" value="<?php echo $_POST['$id'];>?" />
    <input type="hidden" name="submitted" value="TRUE" />
</form>

答案 1 :(得分:0)

首先,当您的帖子数据可用时,您的混音。您已经拥有该事件的$ id,只需将其直接设置为以下形式的this_event值:

<form action="event_updated.php" method="post">
....
    <input type="hidden" name="this_event" value="$id" />
    <input type="hidden" name="submitted" value="TRUE" />
</form>

现在应该设置this_event。但是你要覆盖event_name的$ en变量:

if (empty($_POST['this_event'])) {
    $errors[] = 'this_event not transferred correctly.';
} else {
    $en = trim($_POST['this_event']);
}

event_id放在一个单独的变量中:

if (empty($_POST['this_event'])) {
    $errors[] = 'this_event not transferred correctly.';
} else {
    $this_event = trim($_POST['this_event']);
}

最后,你要踢自己,你没有在数据库更新中使用任何变量,这就是它每次都失败的原因:

$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);

应该是:

$sql="UPDATE events SET creator_id='{$_SESSION['user_id']}',event_name=$event_name, ,endtime=$endtime,starttime=$starttime,duration=$duration,location=$location WHERE event_id=$this_event";
$result= @mysqli_query($dbc, $sql);
祝你好运!

编辑:您从event_updated.php重定向到event.php不是event_id:

header("Location: event.php?id=$event_id"); // $row['event_name'] 

应该是:

header("Location: event.php?id=$this_event");