我有一个像下面这样的表格,我想从用户那里得到一些输入。我的目标是在提交到数据库之前验证数据。我的问题是我该怎么做?
<form action="../actions/insertcomment.php" method="post">
<p class ="ctitle">Leave a Comment:</p>
<p><label for="postid"><b>PostID:</b></label>
<input type="text" id="postid" name="postid" maxlength="5" /> <br/>
<label for="name"><b>Name:</b></label>
<input type="text" id="name" name="name" maxlength="25" /> <br/>
<label for="email"><b>Email:</b></label>
<input type="text" id="email" name="email" maxlength="50" /> <br/>
<label for="website"><b>Website:</b></label>
<input type="text" id="website" name="website" maxlength="25" /> <br/>
<label for="content"><b>Comment:</b></label>
<textarea id="content" name="content" cols="10" rows="4" maxlength="100"></textarea> <br/>
<input type="submit" value="Submit Comment" name="submit_comment" /> <br/>
</p>
</form>
我的 insercomment.php 如下:
<html>
<link rel = "stylesheet" type = "text/css"
href = "../common/style.css" />
<?php
include("../common/dbconnect.php");
$con=new dbconnect();
$con->connect();
error_reporting(E_ALL);
//$postid= $_GET['id'];
if($_POST) {
$postid= $_POST['postid'];
$users_name = $_POST['name'];
$users_email = $_POST['email'];
$users_website = $_POST['website'];
$users_comment = $_POST['content'];
$postid = htmlspecialchars($postid);
$users_name = htmlspecialchars($users_name);
$users_email = htmlspecialchars($users_email);
$users_website = htmlspecialchars($users_website);
$users_comment = htmlspecialchars($users_comment);
$sSql = "INSERT INTO comments
( post_id,name, email, website,content)
VALUES ( $postid, '$users_name',
'$users_email', '$users_website', '$users_comment' )";
//echo $sSql;
mysql_query($sSql);
//$update=mysql_affected_rows();
//echo "<h2>$update Record Inserted</h2><br />";
echo '<h2> Your Comment is submitted</h2><br />';
}
?>
在这里,我不使用“method="post"> 任何此类代码或示例都值得赞赏。