3
I’m a beginner in PHP and I’m creating a personal project to consolidate my knowledge until I came across a problem, by entering a name that has an apostrophe this apostrophe makes the INSERT INTO
not run causing the data not to be written to the Mysql database.
The query and the rest is all correct because I tested remove the apostrophe and the data were recorded correctly and were displayed on the site correctly, I would like to know how to treat these eventual errors of characters at the time of insertion.
PHP code
<?php require_once "topo-painel.php"; ?>
<?php
$array_erro = array(
UPLOAD_ERR_OK => "Sem erro.",
UPLOAD_ERR_INI_SIZE => "O arquivo enviado excede o limite definido no PHP.ini .",
UPLOAD_ERR_FORM_SIZE => "O arquivo enviado excede o limite definido no formulário.",
UPLOAD_ERR_PARTIAL => "O upload do arquivo não terminou ou foi cancelado antes de ser concluido.",
UPLOAD_ERR_NO_FILE => "Nenhum arquivo foi enviado.",
UPLOAD_ERR_NO_TMP_DIR => "Não foi definida uma pasta temporaria.",
UPLOAD_ERR_CANT_WRITE => "Falha ao escrever arquivo em disco",
UPLOAD_ERR_EXTENSION => "Uma extensão do PHP interrompeu o upload do arquivo."
);
if ( isset($_POST["cadastrar"]) ) {
$champ_nome = $_POST["nome-champ"];
$champ_desc = $_POST["desc-champ"];
$champ_avatar = $_FILES["avatar-champ"]["name"];
$adiciona_champ = "INSERT INTO campeoes (champ_nome, champ_descricao, champ_avatar) VALUES ('$champ_nome', '$champ_desc', 'img/campeoes/{$champ_avatar}' )";
$executa_champ = mysqli_query($conexao, $adiciona_champ);
$arquivo_temporario = $_FILES["avatar-champ"]["tmp_name"];
$arquivo = basename( $_FILES["avatar-champ"]["name"] );
$diretorio = "../img/campeoes";
if ( move_uploaded_file($arquivo_temporario, $diretorio."/".$arquivo) ) {
$mensagem = "Arquivo publicado";
}else {
$numero_erro = $_FILES["avatar-champ"]["error"];
$mensagem = $array_erro[$numero_erro];
}
}
?>
<?php
?>
<?php //require_once "menu-painel.php"; ?>
<div class="col-md-10 col-md-offset-2">
<div class="container">
<div class="col-md-4 col-md-offset-3 mtl">
<form action="adiciona-campeao.php" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="nome-champ">Nome do campeão</label>
<input type="text" class="form-control" id="nome-champ" name="nome-champ" placeholder="Informe o nome do campeão">
</div>
<div class="form-group">
<label for="desc-champ">Descrição do campeão</label>
<input type="text" class="form-control" id="desc-champ" name="desc-champ" placeholder="Informe a descrição do campeão">
</div>
<div class="form-group">
<label for="avatar-champ">Avatar do campeão</label>
<input type="hidden" name="MAX_FILE_SIZE" value="3145728" />
<input type="file" name="avatar-champ" value="avatar" id="avatar-champ">
<p class="help-block">Tamanho máximo de 3MB</p>
</div>
<input type="submit" value="CADASTRAR" name="cadastrar" class="btn btn-outlined btn-white" />
</form>
<?php
if ( isset($mensagem) ) {
echo $mensagem;
}
?>
</div>
</div>
</div>
<?php require_once "rodape-painel.php"; ?>
You could test with
$champ_nome = filter_input(INPUT_POST, 'nome-champ', FILTER_SANITIZE_SPECIAL_CHARS);
and return the result obtained? So the parameter taken from the post with this function will be treated. You have several forms besides theFILTER_SANITIZE_SPECIAL_CHARS.
Documentation: http://php.net/manual/en/function.filter-input.php– Rafael Withoeft
Rafael Withoeft Thank you very much, it worked perfectly the name was recorded in the bank as follows Cho'Gath and displayed with the apostrophe on the site Thank you very much again.
– Felipe Dumont
For nothing! : ), when capturing parameters for $_POST or $_GET, at all times as possible, use filter_input.
– Rafael Withoeft
@Qmechanic73 Done... thank you :)
– Rafael Withoeft
vc can use tbm or mysqli_real_escape_string
– Tafarel_Brayan
May not, MUST. the filter input is not the correct solution for this case, but the escape.
– Bacco
@Felipedumont recommend also besides the implementation of filter, the use of Prepared Statement. See: http://php.net/manual/en/mysqli-stmt.bind-param.php or http://php.net/manual/mysqli.prepare.php or http://stackoverflow.com/questions/9629328/how-to-use-mysqli-prepared-statements-in-php
– Rafael Withoeft
convert to htmlentities for this case and general cases , it is an error. It is right to escape the special and reserved characters of sql.
– Daniel Omine
@Felipedumont I have provided a new, coolest solution for you in the answer. Note the Edit part. As there were many questions about filter_input (with good reason), I decided to edit the answer and provide a more correct solution. Thank you for the remark of Bacco and Daniel;
– Rafael Withoeft