PHP注释表单


PHP comment form

我已经创建了一个评论表单。它从数据库中获取id并打印出与该id对应的数据,但它也将信息打印到表单中。如何获得一个空白表单,以便用户可以向记录添加评论?

这是表单的代码:
<form method="post" action="pv.php?id=<?php echo $row['ID']?>&action=<?php echo $form_action ?>">
    <fieldset>
        <legend></legend>
        <p>
            <label for="cname">Date Of Birth</label>  *
            <input id="cname" name="dateofbirth" class="required date"  value="<?php echo $row['Date_Of_Birth']?>" />  (eg 1978.11.11)
        </p>
        <p>
            <label for="cgender">Gender</label>   *
            <input type="radio"
                   name="gender"
                   value="Male"
                   <?php if($row['Gender']=='male'){echo 'checked';}?>/>
                   Male
            <input type="radio"
                   name="gender"
                   value="Female"
                   <?php if($row['Gender']=='female'){echo 'checked';}?>/> Female </td>
        </p>
        <p>
            <label for="curl">Title</label>   *
            <select name="title"   id="title"  class="required">
                <option value="">Please Select</option>
                <option value="Mr" <?php if($row['Title']=='Mr'){echo 'selected';}?>>Mr</option>
                <option value="Ms" <?php if($row['Title']=='Ms'){echo 'selected';}?>>Ms</option>
                <option value="Mrs" <?php if($row['Title']=='Mrs'){echo 'selected';}?>>Mrs</option>
                <option value="Miss" <?php if($row['Title']=='Miss'){echo 'selected';}?>>Miss</option>
                <option value="Other" <?php if($row['Title']=='Other'){echo 'selected';}?>>Other</option>
                </select>
        </p>
        <p>
            <label for="ccomment">First Name</label>     *
            <input type="text" name="firstname" value="<?php echo $row['First_Name']?>" maxlength="50" />
        </p>
        <p>
            <label for="cemail">Last Name</label> *
            <input id="cemail" type="text" name="lastname"
                   value="<?php echo $row['Last_Name']?>" maxlength="75" />
        </p>
        <p>
            <label for="ccomment">Address 1</label>*
            <input type="text" name="address1"
                   value="<?php echo $row['Address_Line_1']?>" maxlength="50" />
        </p>
        <p>
            <label for="ccomment">Address 2</label>
            <input type="text" name="address2"
                   value="<?php echo $row['Address_Line_2']?>" maxlength="50" />
        </p>
        <p>
            <label for="ccomment">City</label>*
            <input type="text" name="city"
                   value="<?php echo $row['City']?>"  maxlength="50" />
        </p>
        <p>
            <label for="ccomment">Postcode</label>*
            <input type="text" name="postcode"
                   value="<?php echo $row['Postcode']?>" maxlength= "10" />  (eg LE5 5QE)
        </p>
        <p>
            <label for="ccomment">Contact No</label>*
            <input type="text" name="contactno"
                   value="<?php echo $row['Contact_No']?>" maxlength= "12" />  (eg 077448825723)
        </p>
        <p>
            <label for="ccomment">Email</label>*
            <input type="text" name="email"
                   value="<?php echo $row['Email']?>" maxlength= "40"/>  (eg info@example.com)
        </p>
        <p>
            <label for="ccomment">Comment</label>
            <textarea rows="10" cols="30" name="note"
                      maxlength= "500"><?php echo $row['Additional_Comment']?></textarea>
        </p>
        <p>
            <input class="submit" type="submit" value="Submit"/>
        </p>
        <p>
            <a href='pv.php'>Main Page</a>
       </p>
    </fieldset>
</form>

这是打印页面数据的代码:

if($_GET['action'] == 'comment') {
    $form_action = 'comment_ok';
    $id = $_GET['id'];
    $result = mysql_query("SELECT * FROM project_data WHERE id='$id'");
    $row = mysql_fetch_array($result);
    echo'<b>';
        echo $row['Date_Of_Birth'];
        echo '&nbsp&nbsp';
        echo $row['Gender'];
        echo '&nbsp&nbsp';
        echo $row['Title'];
        echo '&nbsp&nbsp';
        echo $row['First_Name'];
        echo '&nbsp&nbsp';
        echo $row['Last_Name'];
        echo '&nbsp&nbsp';
        echo $row['Address_Line_1'];
        echo '&nbsp&nbsp';
        echo $row['Address_Line_2'];
        echo '&nbsp&nbsp';
        echo $row['City'];
        echo '&nbsp&nbsp';
        echo $row['Postcode'];
        echo '&nbsp&nbsp';
        echo $row['Contact_No'];
        echo '&nbsp&nbsp';
        echo $row['Email'];
        echo '&nbsp&nbsp';
        echo $row['Additional_Comment'];
    echo '</b>';
}

和我用来发送id到表单的代码片段:

echo "<td><a href='pv.php?action=edit&id=" . $row['ID'] .
     "'>Edit</a>&nbsp&nbsp<a href='pv.php?action=delete_ok&id=" . $row['ID'] .
     "'>Delete</a>&nbsp&nbsp**<a href='pv.php?action=comment&id=" . $row['ID'] .
     "'>Comment</a></td>"**;
echo "</tr>";

我该怎么做?

如果您想要一个允许用户填写空白表单的页面,请查看已填写的表单并更新表单。

您可以通过检查是否存在id来执行空白表单或已填写表单,如下所示:

<?
    if (isset(id)) // Here you can check if the id exists, which means you will be doing an update to the SQL query
        echo'
        <form method="post" action="pv.php?id=' . $row['ID'] . '&action=' . $form_action . '">
            <!-- Your form here with values -->
            e.g. <label for="cemail">Last Name</label> *
            <input id="cemail" type="text" name="lastname" value="' . $row['Last_Name'] . '" maxlength="75" />
        </form>';
        // Put your SQL update here to take the values from the above form
    }
    elseif (!isset(id)) // Here there is no id so you will want to insert
    {
        echo'
        <form method="post" action="pv.php?id=' . $row['ID'] . '&action=' . $form_action . '">
            <!-- Your form here with values -->
            e.g. <label for="cemail">Last Name</label> *
            <input id="cemail" type="text" name="lastname" maxlength="75" /> // No value here because will insert new record
        </form>';
        // Put your SQL insert here to create a new record
    }
?>

如果您可能希望用户能够完全添加新评论,而不是编辑已经与他们的id相关联的评论,我认为您必须通过给他们添加另一个评论的选项来做到这一点。为此,我将创建一个新表,然后将注释插入其中,这样您就有一个包含用户信息的表example_users,以及另一个包含用户评论的表user_comments,例如:

<<h3>表em> example_users
id = 1
fname=Joe
lname=Bloggs
email=jbloggs@example.com
<<h3>表em> user_comments
id=1
user_id=1
comment=Comment will be saved here

这样,任何用户都可以有任意数量的评论。您可以通过使用foreach语句在页面上呈现这些内容,以呈现包含所有现有注释的文本框,然后在文本框末尾为任何新注释保留一个空白。然后,他们可以编辑任何评论并添加新评论。

如果您希望表单为空白,那么只需将value属性设置为空即可。如价值= "。除了id字段