0
Good afternoon class, I have a system for creating feeds, which is possible to use image, which is configured at the base as longblob.
when I pass the direct variable to the query works perfectly, but when I pass the bind_stop it from the error.
The error is: It does not insert correctly in the base, appears as blob file in phpmyadmin but when displaying the image gets broken.
I’m showing it like this:
echo '<img src="data:'.$feed[0]['fd_imagem_type'].';base64,'.base64_encode( $feed[0]['fd_imagem'] ).'" height="120" class="imgleft1"/>';
functions for insertion:
function type($args = [], $bind = false){
# Essa função retorna o codigo para o valor
# Se o $bind for igual a true, ela vai retornar os simbolos
$type = $binds = "";
$i = 1;
foreach($args as $valor){
switch(gettype($valor)){
case 'integer':
$type .= 'i';
break;
case 'double':
$type .= 'd';
break;
case 'string':
if (array_search($valor, $args) == 'fd_imagem') {
$type .= 'b';
} else {
$type .= 's';
}
break;
default:
$type .= 's';
break;
}
$binds .= "?";
if($i < count($args)){
$binds .= ", ";
}
$i++;
}
if($bind){
return $binds;
}
return $type;
}
function parametros($args=[], $sec=false){
# Essa função retorna os parametros já referenciados
# Se a variavel $sec for igual a true, ela vai retornar os campos separados por vírgulas
$type = type($args);
$parametro[] = &$type;
foreach($args as $key=>$valor){
$parametro[] =& $args[$key];
}
if($sec){
$campos = implode(', ', array_keys($args));
return $campos;
}
return $parametro;
}
function insert($mysqli,$tabela, $args = []){
$campos = parametros($args, true); # aqui passo os nomes dos campos da tabela
$binds = type($args, true); # aqui passo os simbolos (?) para a consulta
$sql = "INSERT INTO {$tabela} ({$campos}) VALUES ({$binds})";
if($prepare = $mysqli->prepare($sql)){
if(call_user_func_array(array($prepare, "bind_param"), parametros($args))){
if($prepare->execute()){
return true;
}
} else {
return $mysqli->error;
}
} else {
return $mysqli->error;
}
}
here I do the Insert:
// verifica se foi enviada alguma imagem
if($_FILES['fd_imagem']['name']!=''){
$_POST['fd_imagem'] = addslashes(file_get_contents($_FILES['fd_imagem']['tmp_name']));
$_POST['fd_imagem_type'] = $_FILES['fd_imagem']['type'];
}else{
$_POST['fd_imagem'] = '';
$_POST['fd_imagem_type'] = '';
}
$result = insert($mysqli,'tabela',$_POST);
P.S. Functions I took from Laura’s example link: As step dynamic parameters in a preparedStatment?
What error occurs?
– Guilherme Nascimento
It does not insert correctly in the base, appears as blob file in phpmyadmin but when displaying the image gets broken. am showing like this: echo '<img src="data:'. $feed[0]['fd_imagem_type']. ';Base64,'. base64_encode( $feed[0]['fd_image'] ).'" height="120" class="imgleft1"/>';
– Greenomac Dias
How is the image broken?
– Rodrigo Jarouche
You have to use the flags of
bindParam
. E.g.:bindParam(':variavel', $variavel, PDO::PARAM_STR)
. In your case,bind_param('s', $variavel);
, to send as string to the database. Remember that the new PHP connection classes were planned to not allow SQL Injection in the first order, so the HTML entities and queries are cleaned before they are sent to the finger database.– Not The Real Hemingway
Good afternoon Rodrigo, when displaying the image it is as if there was no image on the server, IE, his date is saved wrong. Not The Real Hemingay, I’m using bindParam dynamically through the function below: call_user_func_array(array($prepare, "bind_param"), parameters($args))
– Greenomac Dias