使用prepared语句在PHP中发布数据安全性

使用prepared语句在PHP中发布数据安全性,php,jquery,html,prepared-statement,Php,Jquery,Html,Prepared Statement,我正在使用准备好的语句来使用ajax提交表单。我尝试了下面的代码,它正在工作。我需要知道一些证券。我在谷歌上查了一下,找到了一些答案,并编写了下面的代码 我想知道我的帖子数据是否正确?我是否需要对字符串进行过滤\u消毒 我输入了naren和naren的并提交。下面是数据库输出 数据库输出 我在最后一行得到了斜杠(/)和撇号 Process.php function register($conn){ global $currentdate; $name=$conn->rea

我正在使用准备好的语句来使用ajax提交表单。我尝试了下面的代码,它正在工作。我需要知道一些证券。我在谷歌上查了一下,找到了一些答案,并编写了下面的代码

我想知道我的帖子数据是否正确?我是否需要对字符串进行
过滤\u消毒

我输入了
naren
naren的
并提交。下面是数据库输出

数据库输出

我在最后一行得到了斜杠
(/)
和撇号

Process.php

function register($conn){
    global $currentdate;
    $name=$conn->real_escape_string(trim($_POST['name']));
    $country=$conn->real_escape_string(trim($_POST['country']));
    $mobileno=$conn->real_escape_string(trim($_POST['mobileno']));
    $email=$conn->real_escape_string(trim($_POST['email']));

    if($name == "") {
        $errorMsg="Name field is required";
        $code="1";
    } else if($country == "") {
        $errorMsg="Country field is required";
        $code="2";
    } elseif ($mobileno=="") {
        $errorMsg="Mobile number is required";
        $code="3";
    } elseif (is_numeric(trim($mobileno))==false) {
        $errorMsg="Only contain a number";
        $code="3";
    } elseif (strlen($mobileno)<10) {
        $errorMsg="Contain minimun 10 number ex:9892555555";
         $code="3";
    } elseif (strlen($mobileno)>10) {
        $errorMsg="Contain maximum 10 number ex:9892555555";
        $code="3";
    } elseif ($email =="") {
        $errorMsg="Email filed is required";
        $code="4";
    } elseif (!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $email)) {
        $errorMsg="Please enter valid email id";
        $code="4";
    } else {
        $query="INSERT INTO `register` (name, country, mobileno, email ,date_of_added) VALUES (?,?,?,?,?)";

        if($stmt = $conn->prepare($query)) {
            $stmt->bind_param("sisss", 
                                $name,$country,$mobileno,
                                $email,$currentdate);
            $stmt->execute();
            $errorMsg="Data Inserted";
            $code="5";
            $_SESSION['thankyouSession'] = "true";
        }else{
            $code= "6";
            $errorMsg='Something is wrong';
        }

        $stmt->close();
        $conn->close();
    }
    $response['error']=$errorMsg;
    $response['error_no']=$code;
    echo json_encode($response); 
 }
数据库连接

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

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

据我所知,您不需要准备语句的真实转义字符串

对于POST变量,您最好使用isset,如:

$name = isset($_POST['name']) ? $conn->real_escape_string(trim($_POST['name'])) : '';
防止未定义的错误

当我查看您的查询时,$country需要是一个整数

$country= isset($_POST['country']) ? (int)$_POST['country']) : 0;
然后在elseif中检查$country>0

对于电子邮件检查,您还可以使用:

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
// No valid email
}
也许在对受影响的行进行彻底检查后,请确保确实插入了数据


在html中显示数据库结果时,可能需要一个清理功能

您的脚本可以打开。甚至你也应该考虑使用<代码> MySqLII< <代码>或<代码> PDO API,而不是级联值<代码> Global $CurrDeDATE;<为什么这么做听起来很糟糕,有人帮我吗here@RiggsFolly,我在函数之前添加了时区代码。因此,我添加了全局$currentdate@RiggsFolly,您的脚本容易受到SQL注入攻击。我需要知道我的代码哪里错了?我检查了6个版本,4个用户78%Danijel回答如果你使用的是准备好的声明,你不需要转义数据。这是准备查询的主要功能之一。prepare将查询发送到数据库,并对其进行编译和优化。因此,当您在执行expecute之前绑定数据时,该数据不能像将值连接到字符串中然后执行字符串那样影响查询的执行。据我所知,您不需要准备语句的真正的_escape _字符串。那么,为什么你的下一行转义输入呢?我复制了他的代码,因为插入或更新是不必要的
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
// No valid email
}