Error "Warning: mysqli_real_escape_string() expects Parameter 2 "

Asked

Viewed 101 times

-1

I am creating a webservice that will have to pick up some. pdf files saved in the database and send to a user x.

To save this file in the bank, I’m trying to do it this way:

<?php
    require("conexao.php");

    $nome = $_POST['nome'];             
    $email = $_POST['email'];   
    $texto = $_POST['texto'];
    $arq_complementar = $_FILES['arquivo_complementar'];    
    $arquivo_complementar = mysqli_real_escape_string($connection, $arq_complementar);
    print_r($arquivo_complementar);

    $sql = "INSERT INTO cartas(nome, email, texto, arq_complementar) VALUES ('$nome', '$email', '$texto', '$arquivo_complementar')";
    $testar_conexao = mysqli_query($connection,$sql);

    //header("Location: ".$_SERVER['HTTP_REFERER']."");
?>

But the system returns me the following error: "Warning: mysqli_real_escape_string() expects Parameter 2 to be string, array Given in"

Below is the way I’m calling this registration service

<form action="cadastrarMensagem.php" method="POST" enctype="multipart/form-data">
            <fieldset>
                <legend>Integração</legend>
                <label>Nome:</label> <br>
                    <input type="text" name="nome"> <br>
                <label>Email:</label> <br>
                    <input type="email" name="email"> <br>
                <label>Texto da carta:</label> <br>
                    <textarea name="texto"></textarea> <br>
                <label>Arquivo Complementar:</label>
                    <input name="arquivo_complementar" type="file"> <br> <br>
                <input id="button" type="submit" name="Enviar">
            </fieldset>
        </form>
  • I think the message is clear: the function expects a string as the second parameter and you passed an array. You can check that it is an array from the print_r which you did. After fixing this it will probably give array to string conversion error when trying to save an array in the database. In short, review what you did in the code and match what you wanted to do.

1 answer

0


What happens is that $_FILES['arquivo_complementar'] is an array. If you want to get the name of the uploaded file, you can make $_FILES['arquivo_complementar']['name']

Here is a link showing which options this array has:

https://www.geeksforgeeks.org/php_files-array-http-file-upload-variables/

In your code, I didn’t identify the part where you save the file on disk.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.