1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

adding thread problem

Discussion in 'PHP' started by Voodooscience, Jun 19, 2006.

  1. #1
    Right, I'm not very good with PHP but I decided the best way to teach myself is to go all out and try a project. Things go wrong of course and I found this forum for help. n_n;
    SEMrush
    Here's what I have so far:

    
    <?php
    
    // For register_global on PHP settings
    $member = $_COOKIE['member'];
    
    session_start(); // you must put this to read session variables
    if (empty($member) || !isset($member)) // fail to read the browser cookie
    {
    // Try to read session
    if (empty($_SESSION['member']) || !isset($_SESSION['member']))
    {
    header("Location: login.php"); // redirect user to login
    exit;
    }
    else
    {
    $member = $_SESSION['member'];
    }
    }
    // MySQL Connection Variables 
    // Fill in your values for the next 4 lines
    $hostname='localhost';
    $user='********'; //'user name for MySQL database';
    $pass='********'; //'Password for database'; 
    $dbase='********'; //'database name';
    
    $connection = mysql_connect("$hostname" , "$user" , "$pass") or die ("Can't connect to MySQL");
    $db = mysql_select_db($dbase , $connection) or die ("Can't select database.");
    
    // User has login but you should check his account validity again
    $qChk = "select id from membership where name='$member' and status='Y' ";
    $rsChk = mysql_query($qChk);
    
    if (mysql_num_rows($rsChk) != '1')
    {
    session_destroy();
    // make the cookie expires instantly.
    setcookie ("member",$member,time()-1957240,"/"); 
    header("Location: login.php");
    exit; 
    }
    ?>
    
    
    
    <? 
    include ("database.php"); 
    
    $f = $_GET['f']; 
    
    
    $result = mysql_query("select title from forums where id = '$f'"); 
    $r = mysql_fetch_array($result); 
    ?> 
    <? 
    if(isset($_POST['preview'])) 
    { 
    ?> 
    <table cellpadding="5" cellspacing="1" border="0" style="width:90%;border:1px solid #000;"> 
      <tr> 
        <td colspan="2" style="background-color:#eee;"><? echo $_POST['title']; ?></td> 
      </tr> 
      <tr> 
        <td style="width:25%;background-color:#fafafa;" valign="top"> 
        <? echo $_POST['username']; ?></td> 
        <td style="width:75%;background-color:#fafafa;" valign="top"> 
        <? 
        //  nl2br turns new lines into line breaks (<br />) 
        // htmlspecialchars prevents use of html 
        echo nl2br(htmlspecialchars($_POST['post']));  
        ?> 
        </td> 
      </tr> 
      <tr> 
        <td style="background-color:#eee;font-size:0;height:15px;" colspan="4">&nbsp;</td> 
      </tr> 
    </table> 
    <br /> 
    <? 
    } 
    ?> 
    <? 
    if(isset($_POST['addthread']) && !empty($_POST['title']) && !empty($_POST['username']) 
       && !empty($_POST['post'])) 
    { 
       $f = $_POST['f']; 
       $title = addslashes(htmlspecialchars($_POST['title'])); 
       $username = addslashes(htmlspecialchars($_POST['username']));
       $post = addslashes(htmlspecialchars($_POST['post'])); 
    
       $timestamp = time(); 
    
    mysql_query("insert into topics (id, fid, title, username, post, last_post_username, timestamp)       values('null', '$f', '$title', '$username', '$post', '$username', '$timestamp')") or die ('cannot add new post' . mysql_error());
    
    
    
       $add_count_result = mysql_query("select topics from forums where id = '$f'"); 
       $add_count_row = mysql_fetch_array($add_count_result); 
       $add_count = $add_count_row['topics']+1; 
    
         mysql_query("update forums set topics = '$add_count' where id = '$f'"); 
        
    
       $thread_id_result = mysql_query("select id from topics order by id desc limit 1"); 
       $thread_id_row = mysql_fetch_array($thread_id_result); 
       $t = $thread_id_row['id']; 
    ?> 
    <table cellpadding="5" cellspacing="1" border="0" style="width:90%;border:1px solid #000;"> 
      <tr> 
        <td style="background-color:#eee;">Message Added</td> 
      </tr> 
      <tr> 
        <td style="width:75%;background-color:#fafafa;" valign="top"> 
        Your Thread was added successfully.<br /> 
        <a href="viewthread.php?t=<? echo $t; ?>">View your thread</a> 
        </td> 
      </tr> 
      <tr> 
        <td style="background-color:#eee;font-size:0;height:15px;" colspan="4">&nbsp;</td> 
      </tr> 
    </table> 
    <? 
    } 
    else{ 
    ?> 
    <form action="addthread.php" method="post"> 
    <input type="hidden" value="<? if(isset($_POST['f'])){ echo $_POST['f']; } else{ echo $f; } ?>"  
    name="f" /> 
    <table cellpadding="5" cellspacing="1" border="0" style="width:90%;border:1px solid #000;"> 
      <tr> 
        <td colspan="2"style="background-color:#eee;">Adding Forum to: <? echo $r['title']; ?></td> 
      </tr> 
      <tr> 
        <td style="width:15%;background-color:#fafafa;">Username:</td> 
        <td style="width:85%;background-color:#fafafa;"> 
        <input type="hidden" name="$username" value="<?=$member;?> <? if(isset($_POST['username'])){ echo $_POST['username']; } ?>">
        </td> 
      </tr> 
      <tr> 
        <td style="width:15%;background-color:#fafafa;">Thread Title:</td> 
        <td style="width:85%;background-color:#fafafa;"> 
        <input type="text" name="title" 
         value="<? if(isset($_POST['title'])){ echo $_POST['title']; } ?> " /> 
        </td> 
      </tr> 
      <tr> 
        <td style="width:15%;background-color:#fafafa;" valign="top">Message:</td> 
        <td style="width:85%;background-color:#fafafa;"> 
    <textarea name="post" cols="" rows="" style="width:80%;height:200px;"> 
    <? if(isset($_POST['post'])){ echo $_POST['post']; } ?> 
    </textarea> 
        </td> 
      </tr> 
      <tr> 
        <td style="width:15%;background-color:#fafafa;" valign="top">&nbsp;</td> 
        <td style="width:85%;background-color:#fafafa;"> 
        <input type="submit" name="preview" value="Preview" /> 
        <input type="submit" name="addthread" value="Post Message" /> 
        </td> 
      </tr> 
      <tr> 
        <td style="background-color:#eee;font-size:0;height:15px;" colspan="4">&nbsp;</td> 
      </tr> 
    </table> 
    </form> 
    <? 
    } 
    ?> 
    
     </table></div>
    
    PHP:
    So here's the problem; you try to add the post and nothing happens. You get the same form page again. Click back and look at the forums and still there's no post appeared. What's wrong with the code? :p
     
    Voodooscience, Jun 19, 2006 IP
    SEMrush