Problem of "reply" page in forum

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kang jia
    New Member
    • Jun 2007
    • 88

    Problem of "reply" page in forum

    hi

    i am doing reply page for forum, users can reply for the specific topic they would like to reply. Firstly of all, when click the topic name in topic page. i will redirect them to reply page. the details of the topic will be retrieved from database, like 'detail' field, 'name'( name of the poster) 'email', 'date'. secondly, i will also retrieve all the replies from other users. and lastly is creating text box and text area to let the user to reply to this speicific topic.

    my code is in reply page is in the following :

    [code=PHP]
    <?php

    date_default_ti mezone_set('Asi a/Singapore');
    mysql_connect(' localhost','roo t','') or die(mysql_error ());
    mysql_select_db ('member') or die(mysql_error ());



    // get value of id that sent from address bar
    $id=$_GET['id'];

    $sql="SELECT * FROM forum_question WHERE id='$id'";
    $result=mysql_q uery($sql) or die(mysql_error ());

    $rows=mysql_fet ch_array($resul t);
    ?>
    <table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCC C">
    <tr>
    <td><table width="100%" border="0" cellpadding="3" cellspacing="1" bordercolor="1" bgcolor="#FFFFF F">
    <tr>
    <td bgcolor="#F8F7F 1"><strong>< ? echo $rows['topic']; ?></strong></td>
    </tr>

    <tr>
    <td bgcolor="#F8F7F 1"><strong>Deta il :</strong><? echo $rows['detail']; ?></td>
    </tr>

    <tr>
    <td bgcolor="#F8F7F 1"><strong>B y :</strong> <? echo $rows['name']; ?> </td>
    </tr>
    <tr>
    <td bgcolor="#F8F7F 1"><strong>Emai l : </strong><? echo $rows['email'];?></td>
    </tr>

    <tr>
    <td bgcolor="#F8F7F 1"><strong>D ate/time : </strong><? echo $rows['datetime']; ?></td>
    </tr>
    </table></td>
    </tr>
    </table>
    <BR>
    <?php
    //$tbl_name2="for um_answer"; // Switch to table "forum_answ er"

    $sql2="SELECT * FROM forum_answer WHERE q_id = '$id'";
    $result2=mysql_ query($sql2) or die(mysql_error ());

    while($rows=mys ql_fetch_array( $result2)){
    ?>
    <table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCC C">
    <tr>
    <td><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFF F">
    <tr>
    <td bgcolor="#F8F7F 1"><strong>I D</strong></td>
    <td bgcolor="#F8F7F 1">:</td>
    <td bgcolor="#F8F7F 1"><? echo $rows['a_id']; ?></td>
    </tr>
    <tr>
    <td width="18%" bgcolor="#F8F7F 1"><strong>Name </strong></td>
    <td width="5%" bgcolor="#F8F7F 1">:</td>
    <td width="77%" bgcolor="#F8F7F 1"><? echo $rows['a_name']; ?></td>
    </tr>
    <tr>
    <td bgcolor="#F8F7F 1"><strong>Emai l</strong></td>
    <td bgcolor="#F8F7F 1">:</td>
    <td bgcolor="#F8F7F 1"><? echo $rows['a_email']; ?></td>
    </tr>
    <tr>
    <td bgcolor="#F8F7F 1"><strong>Comm ent</strong></td>
    <td bgcolor="#F8F7F 1">:</td>
    <td bgcolor="#F8F7F 1"><? echo $rows['a_answer']; ?></td>
    </tr>
    <tr>
    <td bgcolor="#F8F7F 1"><strong>D ate/Time</strong></td>
    <td bgcolor="#F8F7F 1">:</td>
    <td bgcolor="#F8F7F 1"><? echo $rows['a_datetime']; ?></td>
    </tr>
    </table></td>
    </tr>
    </table><br>

    <?
    }

    $sql3="SELECT view FROM forum_question WHERE id ='$id'";
    $result3=mysql_ query($sql3) or die(mysql_error ());

    $rows=mysql_fet ch_array($resul t3);
    $view=$rows['view'];

    // if have no counter value set counter = 1
    if(empty($view) ){
    $view=1;
    $sql4="INSERT INTO forum_question( view) VALUES('$view') WHERE id ='$id'";
    $result4=mysql_ query($sql4) or die(mysql_error ());
    }

    // count more value
    else{
    $addview=$view+ 1;
    $sql5="update forum_question set view ='$addview' WHERE id ='$id'";
    $result5=mysql_ query($sql5) or die(mysql_error ());
    }
    ?>
    <BR>
    <table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCC C">
    <tr>
    <form name="form1" method="post" action="add_ans wer.php">
    <td>
    <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFF F">
    <tr>
    <td width="18%"><st rong>Name</strong></td>
    <td width="3%">:</td>
    <td width="79%"><in put name="a_name" type="text" id="a_name" size="45"></td>
    </tr>
    <tr>
    <td><strong>Ema il</strong></td>
    <td>:</td>
    <td><input name="a_email" type="text" id="a_email" size="45"></td>
    </tr>
    <tr>
    <td valign="top"><s trong>Comment</strong></td>
    <td valign="top">:</td>
    <td><textarea name="a_answer" cols="45" rows="3" id="a_answer"> </textarea></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td><input name="id" type="hidden" value="<? echo $id; ?>"></td>
    <td><input type="submit" name="Submit" value="Submit"> </td>
    </tr>
    </table>
    </td>
    </form>
    </tr>
    </table>
    <BR><BR>
    <?
    echo"<div align='center'> <a href ='main_forum.ph p'>Back to Forum Home page</a></div>";
    ?>
    [/code]

    everytime i query, the error message will come out. saying "
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE id ='15'' at line 1"

    i don;t know where is wrong with it. can any one help me to check where of the syntax is wrong.

    PS my database structure is like this, in forum_question table , i have id as primay key, userid as foreign key, the rest of the field is name, topic, detail, email,datetime, view, reply. ( view and reply is to a field to count how many people have been viewed or replied to the topic)

    in fourm_answer table, we got a_id as primary key, q_id as foreign key( it is the same as id in forum_question table), the rest of field is a_name,a_email, a_answer,a_date time.

    any one can help me with this, it is urgent, as my project timeline is very near. thanks in advance for your kind help! :)
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    Code:
    $sql5="update forum_question set view ='$addview' WHERE id ='$id'";
    Ideally when comparing numeric values leave out the quotes
    Code:
    WHERE id = $id
    But this should not have caused the error.
    I suspect there is sometnig wayward with the $addview variable.
    Echo out the whole query before submitting and lets have a look at it.
    [PHP]echo $sql5;[/PHP] This should be done anyway when developing otherwise you are blind. I am being nice today.

    Comment

    • mwasif
      Recognized Expert Contributor
      • Jul 2006
      • 802

      #3
      Alway stry to identify where you are having issue. You have a lot of queries with the same WHERE clause, it is difficult for someone to identify the problem.

      You have problem with this INSERT statement
      [PHP]$sql4="INSERT INTO forum_question( view) VALUES('$view') WHERE id ='$id'";[/PHP]
      It should be like this
      [PHP]$sql4="INSERT INTO forum_question( id, view) VALUES($id, '$view')";[/PHP]

      You can not use WHERE in INSERT statement. If id column is autoincrement, then simply don't include this in the query e.g.
      [PHP]$sql4="INSERT INTO forum_question( view) VALUES('$view') ";[/PHP]

      Comment

      • ilearneditonline
        Recognized Expert New Member
        • Jul 2007
        • 130

        #4
        Code:
        // get value of id that sent from address bar
        $id=$_GET['id'];
        
        $sql="SELECT * FROM forum_question WHERE id='$id'";
        Not sure if this is the problem, but I know i have this all the time. A query string value is always a string. so what you should try is..

        Code:
        $id = (int)$_GET['id'];
        That will convert your string value to an integer, assuming the id field is an integer in your database.

        Comment

        Working...