防止SQL注入时在布尔值上调用成员函数bind_param()

时间:2019-04-27 15:36:40

标签: php mysql mysqli

我正在填写联系表格,这是用于发送消息和添加通讯(可选)的代码。 问题是执行脚本时出现此错误:

  

致命错误:未捕获错误:在路径131中的bool上调用成员函数bind_param()堆栈跟踪:在第136行的 path 中抛出了#0 {main}

更改一些.ini后,我得到了:

  

致命错误:mysqli_sql_exception未捕获:命令不同步;您现在不能在路径:135中运行此命令:堆栈跟踪:#0路径(135):mysqli-> prepare('INSERT INTO new ...')#1 {main}在第135行的路径中抛出

<?php

ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

session_start();

if(!isset($_POST["firstName"]) || !isset($_POST["surname"]) || !isset($_POST["email"]) || !isset($_POST["message"]))
{
    $_SESSION["index_showerror_0"] = "true";
    $_SESSION["index_showerror_0_message"] = "<b>Błąd!</b> Nie ustawiono zmiennych.";
    header('Location: strona-glowna'); exit();
}

define("ACC_CONNECT", "true");

$firstname = $_POST["firstName"];
$surname = $_POST["surname"];
$email = $_POST["email"];
$message = $_POST["message"];
$message_real = nl2br($_POST["message"]);

// START CHECKING // START CHECKING // START CHECKING // START CHECKING // START CHECKING // START CHECKING // START CHECKING //
$correct = "true";

if(isset($_POST["agreement"]))
{
    $agreement = "true";
}
elseif(!isset($_POST["agreement"]))
{
    $correct = "false";
    $agreement = "false";
    $_SESSION["index_showerror_agreement"] = "true";
    $_SESSION["index_showerror_agreement_message"] = "Zgoda jest wymagana aby wysłać wiadomość.";
}

if(isset($_POST["newsletter"]))
{
    $newsletter = "true";
}
elseif(!isset($_POST["newsletter"]))
{
    $newsletter = "false";
}


if(strlen($firstname) < 3 || strlen($firstname) > 30)
{
    $correct = "false";
    $_SESSION["index_showerror_fname"] = "true";
    $_SESSION["index_showerror_fname_message"] = "Musisz wpisać imię od 3 do 30 znaków!";
}

if(strlen($surname) > 0 && strlen($surname) > 50)
{
    $correct = "false";
    $_SESSION["index_showerror_sname"] = "true";
    $_SESSION["index_showerror_sname_message"] = "Nazwisko może mieć maksymalnie 50 znaków!";
}

if(strlen($email) < 5 || strlen($email) > 75)
{
    $correct = "false";
    $_SESSION["index_showerror_email"] = "true";
    $_SESSION["index_showerror_email_message"] = "Nazwisko może mieć maksymalnie 75 znaków!";
}

if(!filter_var($email, FILTER_VALIDATE_EMAIL))
{
    $correct = "false";
    $_SESSION["index_showerror_email"] = "true";
    $_SESSION["index_showerror_email_message"] = "Adres e-mail jest niepoprawny!";
}

if(strlen($message) < 30 || strlen($message) > 2500)
{
    $correct = "false";
    $_SESSION["index_showerror_message"] = "true";
    $_SESSION["index_showerror_message_message"] = "Wiadomość musi mieć od 30 do 2500 znaków!";
}


// END OF CHECKING // END OF CHECKING // END OF CHECKING // END OF CHECKING // END OF CHECKING // END OF CHECKING // END OF CHECKING //
if(!isset($correct) || $correct == "false")
{
    header('Location: strona-glowna'); exit(); exit();
}
elseif($correct == "true")
{

    require_once "connect.php";
    $conn = new mysqli($db_host, $db_username, $db_password, $db_name);
    if($conn->connect_errno != 0)
    {
        // connection error
        die("Wystąpił błąd podczas próby połączenia z bazą danych, sprbóuj ponowie później. Kod błędu: <b>".$conn->connect_errno."</b>"); die();
    }


    if(!empty($_SERVER['HTTP_CLIENT_IP']))
    {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    }
    elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
    {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
        $ip = $_SERVER['REMOTE_ADDR'];
    }

    $result = $conn->prepare('INSERT INTO messages VALUES (NULL, ?, ?, ?, ?, ?)');
    if($result->bind_param('sssss', $firstname, $surname, $email, $message_real, $ip) === FALSE)
    {
        // error assigning value
        die("Błąd przypisania wartości"); die();
    }


    if($newsletter == "true")
    {
        $nc_result = $conn->prepare('SELECT news_id FROM newsletter WHERE news_email=?');
        if($nc_result->bind_param('s', $email) === FALSE)
        {
            // error assigning value
            die("Błąd przypisania wartości"); die();
        }

        $nc_result->execute();

        if($nc_result->num_rows < 1)
        {

        LINE 135:   $n_signin = $conn->prepare('INSERT INTO newsletter VALUES (NULL, ?, ?, ?)');
        LINE 136:   if($n_signin->bind_param('sss', $firstname.' '.$surname, $email, $ip) === FALSE)
            {
                // error assigning value
                die("Błąd przypisania wartości"); die();
            }

            $n_signin->execute();

        }
    }

    if($result->execute() === TRUE)
    {
        // correct, redirecting
        header('Location: dziekujemy-za-wiadomosc'); exit(); exit();
    }
    else
    {
        // error happened, redirecting
        header('Location: wystapil-blad'); exit(); exit();
    }

}

?>

0 个答案:

没有答案