0

我有一个像下面这样的表格,我想从用户那里得到一些输入。我的目标是在提交到数据库之前验证数据。我的问题是我该怎么做?

<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"> 任何此类代码或示例都值得赞赏。

4

6 回答 6

1

你当然应该清理你的输入以防止注入:

    $postid= mysql_real_escape_string($_POST['postid']);

这将使您的所有输入安全地插入数据库。

于 2012-07-14T13:11:11.123 回答
1

您可以使用filter_inputphp.ini 来验证数据。你可以在这里读更多关于它的内容:

php中的filter_input

这是一个关于如何使用它来验证电子邮件的示例:

$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);

whereINPUT_POST是方法,email是字段的名称,($_POST['email'])FILTER_VALIDATE_EMAIL验证选项。

您可能需要考虑在mysqlipdo中使用准备好的语句以使您的应用程序更安全。

要检查变量是否有值,您可以使用函数empty

if(!empty($_POST['email']){
  //do stuff
}

您还可以添加客户端验证。一个好的库是liveValidation,顾名思义,它会在用户输入时验证用户输入,这样用户就不必等待页面刷新才能获得反馈,无论他们的表单是否已成功提交。

于 2012-07-14T13:17:02.820 回答
1

我建议您使用mysql_real_escape_string(). 使用 if else 语句进行验证。举例

if ($_POST['name']...) 
{
    echo "fill in those fields!";
}
else
{
    do stuff...
}

顺便说一句,您应该使用 PDO 准备好的语句,而不是用 PHP 回显 HTML。

于 2012-12-28T06:18:32.450 回答
1

我的经验表明,在插入数据库之前,您应该使用 if-else 语句检查您的输入。最重要的是使用准备好的语句。不要像那样传递原始字符串。始终为您的表格使用准备好的声明。

请参阅:防止 SQL 注入的最佳方法

于 2012-07-14T13:20:39.117 回答
1

最好的方法是在 sql 语句之前检查数据是否有效。

伪命题:

$data1 = $_POST['xyz']; //text
$data2 = $_POST['abc']; //number
...

errors = array
if(data1 is not text) errors[] = data1 must be text
if(data2 is not number) errors[] = data2 must be number
...

if(count(errors) > 0) return errors
else

do the sql insert
return "thank you message"
于 2012-07-14T13:10:17.977 回答
0

你可以这样做: -

if(isset($_POST))
{
     if($_POST['postid']!="" && $_POST['name']!="" &&.....)
     {
         //do your insertion here 
     }
     else
       echo "oops !! Something is missing";
}
于 2012-07-14T13:13:50.687 回答