-1

我最终让我的联系表单工作了,我的下一个任务是在字段中添加一些服务器端验证。这是我的表单的php代码:

<?php
    require_once('../_inc/config/db.php');

    $postArray = $_POST;

    foreach($postArray['name'] as $row=>$Name) {
      $name=mysql_real_escape_string($Name);
      $company=mysql_real_escape_string($postArray['company'][$row]);
      $email=mysql_real_escape_string($postArray['email'][$row]);
      $contact=mysql_real_escape_string($postArray['contact'][$row]);

      $sql = "INSERT INTO registered_blue (`name`, `company`, `email`, `contact`, `day`, `event-time`, `event`) VALUES ('".$name."','".$company."','".$email."','".$contact."', '" . $postArray['event-day'] . "', '" . $postArray['event-time'] . "', '" . $postArray['event'] . "')";

      $result = mysql_query($sql);

    }

    if ($result) {
        header('Location: /blue-event/confirmed.php');
        exit();
    } else {
        die('Invalid query: ' . mysql_error());
    }

?>

有人可以指出我正确的方向是在此表单上实现验证的最简单/最有效的方法吗?

4

1 回答 1

0

Well define the validation you want

values are required? I personaly use strlen, others use == '' (use trim if wished for)

Values need to be a specific type? You should look at preg_match or ctype_* (see these function)

Numbers need to me in a specific range, use the < <= >= > operators.

File validation, look at extension and then mime type (always look at the mime to be sure!)

So there are tons of ways to validate and tons of things to validate so you should be make a more specific question.

I also recommend, look at this answer. It gives you a nice start I think

于 2013-06-07T11:31:52.693 回答