Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* In response to a Yahoo Answer's Question */
- // http://pastebin.com/2LHNNsqn Same Script With Comments
- /* Message Functions */
- function errorMsg($msg){
- if(is_array($msg)){
- $r = '<table>';
- foreach($msg as $m){
- $r .= '<tr><td style="color: #900;font-weight: bold;">' . $m . '</td></tr>' . PHP_EOL;
- }
- return $r . '</table>';
- }else{
- return '<table><tr><td style="color: #900;font-weight: bold;">' . $msg . '</td></tr></table>';
- }
- }
- function goodMsg($msg){
- return '<table><tr><td style="color: #090;font-weight: bold;">' . $msg . '</td></tr></table>';
- }
- /* Variables */
- $output = '';
- $title = $_POST['title'];
- $content = $_POST['content'];
- $category = $_POST['category'];
- $form = <<<FORM
- <form action="./test.php" method="post" style="background-color: #000;">
- <table>
- <tr>
- <td><font color="white"><b>Title:</b></font></td>
- <td><input type="text" name="title" value="$title"></td>
- </tr>
- <tr>
- <td><font color="white"><b>Content:</b></font></td>
- <td><textarea name="content" rows="6" col="80">$content</textarea></td>
- </tr>
- <tr>
- <td><font color="white"><b>Category</b></font></td>
- <td>
- <select name="category">
- <option value="General Discussion">General Discussion</option>
- <option value="Suggestions">Suggestions</option>
- <option value="Complaints">Complaints</option>
- <option value="Problem Reporting">Problem Reporting</option>
- </select>
- </td>
- </tr>
- <tr>
- <td colspan="2"><input type="submit" name="postbtn" value="Post"></td>
- </tr>
- </table>
- </form>
- FORM;
- /* Logged In Check */
- $username = $_COOKIE['username'];
- $userid = $_COOKIE['userid'];
- if( !$username || !$userid){
- header('Refresh: 5; url=/login/login.php');
- echo errorMsg('You need to be logged in to access this page. <a href="/login/login.php" >Login</a>');
- exit;
- }
- /* Form Checking And Processing */
- if ($_POST['postbtn']){
- $currenttime = date("h:i A");
- $currentdate = date("F d, Y");
- $errors = Array();
- /* Error Checking */
- if ((strlen($title) < 10) || (strlen($title) > 50)){
- $errors[] = 'Title must be longer than 10 and less than 50 characters';
- }
- if ((strlen($content) < 50) || (strlen($content) > 1000)){
- $errors[] = 'Content must be longer than 50 and less than 1000 characters';
- }
- if( count($errors) > 0){
- $output = errorMsg($errors) . $form;
- }else{
- require ("connect.php");
- $title = mysql_real_escape_string( $title );
- $content = mysql_real_escape_string( $content );
- $category = mysql_real_escape_string( $category );
- $query = mysql_query("SELECT id FROM posts WHERE title='$title' LIMIT 1");
- if ($query && mysql_num_rows($query) === 0){
- if( mysql_query("INSERT INTO posts (title, content, time, date, author, category) VALUES ('$title', '$content', '$currenttime', 'on $currentdate', '$username', '$category')") ){
- header('Refresh: 5; url=./posts.php?title=' . $title);
- $output = goodMsg('Your message was posted successfully.<br>If you are not redirected to the post in 5 seconds. Click the link below.<br> <a href="">' . $title . '</a>');
- }else{
- $output = errorMsg('There was an Internal Error, while attempting to save your post. Please try again in a few minutes.') . $form;
- }
- }else{
- $output = errorMsg('There is already a post with that Title, please select another.') . $form;
- }
- // mysql_close($conn);
- }
- }else{
- $output = $form;
- }
- echo $output;
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement