POST服务器端表单验证

时间:2016-07-01 13:57:14

标签: php

有一些问题从下面的php脚本确切地告诉我发生错误的位置。它通过HTML表单中的帖子获取数据。然后在将它们插入数据库之前尝试一些验证。有人发现什么吗?

<?php
if( isset( $_POST['submit_form'] ) )
{

validate_data($data)
{
  $data = trim($data);
  $data = stripslashes($data);
  $data = strip_tags($data);
  $data = htmlspecialchars($data);
  $data = mysqli_real_escape_string($data);
 return $data;
}
 $address = validate_data($_POST['name']);
  $address = validate_data($_POST['address']);
  $zipcode = validate_data($_POST['zipcode']);
  $county = validate_data($_POST['county']);
  $phone = validate_data($_POST['phone']);
  $email = validate_data($_POST['email']);
  $password = validate_data($_POST['password']);
  $pwVerified = validate_data($_POST['pwVerified']);

 //create connection

 // Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO company (name, address, zipcode, county, phone, email, password, pwVerified )
VALUES 
                ( '$name', '$address', '$zipcode','$county','$phone', '$email', '$password', '$pwVerified')";

if ($conn->query($sql) === TRUE) {

   // echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

}else 
 { echo "there is a problem";}
include 'sign.php';

?>

1 个答案:

答案 0 :(得分:-1)

应该是功能 validate_data($ data)。评论1和2.谢谢。

<?php
if( isset( $_POST['submit_form'] ) )
{

function validate_data($data)
{
  $data = trim($data);
  $data = stripslashes($data);
  $data = strip_tags($data);
  $data = htmlspecialchars($data);
  $data = mysqli_real_escape_string($conn, $data);
 return $data; //the rest