PDL unlink help

0

I'm trying to migrate MYSQL to the PDO, and I came across a problem with Unlink.

With the code below I can do the UPDATE in the BD Banner table, and also store the new file in the destination folder (img_banner), but not replacing the previous file in the destination folder.

<?php
if(isset($_POST['banner'])){
$codigo = $_GET['codigo'];
$imagem = $_POST['imagem'];
$sqlUpdate = 'UPDATE banner SET imagem = ? WHERE codigo = ?';
$dados = array($imagem, $codigo);

$pasta = '../img_banner/';
if (isset($_POST['banner'])){
$check = @$_POST['apagar'];
foreach($check as $imagem){
$delcheck = $sqlUpdate = 'UPDATE banner SET imagem = ? WHERE codigo = ?' or die (mysql_error()); 
unlink($pasta.'/'.$imagem);
if ($delcheck >= '1'){
echo 'Imagem deletada com sucesso!';
}else{
echo 'Erro ao deletar imagem, tente novamente!';
}}}}
?>

<?php include 'upload_banner.php'; ?>
<form action="" method="POST" enctype="multipart/form-data">
    <input size="1" type="hidden" name="codigo" value="<?php echo $codigo?>" readonly>
    <input type="hidden" type="checkbox" name="apagar[]" value="<?php echo $imagem ?>" checked readonly>
    <label>Selecione uma nova imagem:</label><br />
    <input type="file" name="imagem[]" accept="image/*" ><br />
    <input type="submit" name="banner" value="Atualizar">
</form>

With this code you are introducing the following errors below:

Error 01:

Notice: Undefined index: image in C: \ wamp \ www \ Commerce \ admin \ edit_banner__initial-upd.php on line 41

Error 02:

Notice: unlink (../ img_banner //); Permission denied in C: \ wamp \ www \ Commerce \ admin \ edit_banner_initial-upd.php on line 53

Any help will be very welcome.;)

    
asked by anonymous 03.11.2017 / 00:42

1 answer

0

Hello, I was able to solve the problem, and put the code working without errors for future doubts of the friends.

<?php
    $sql = $pdo->prepare("SELECT * FROM banner WHERE codigo = $codigo") or die(mysql_error());
    $sql->execute();
    foreach($sql->fetchAll() as $res);

    if(isset($_POST['banner'])){
    $codigo = $_GET['codigo'];
    $sqlUpdate = 'UPDATE banner SET imagem = $imagem WHERE codigo = $codigo';
    $dados = array($codigo);

    $pasta = '../img_banner';
    if (isset($_POST['banner'])){
    $check = @$_POST['apagar'];
    foreach($check as $imagem){
    $delcheck = $sqlUpdate = 'UPDATE banner SET imagem = $imagem WHERE codigo = $codigo' or die (mysql_error()); 
    unlink($pasta.'/'.$imagem);
    if ($delcheck >= '1'){
    echo 'Imagem deletada com sucesso!';
    }else{
    echo 'Erro ao deletar imagem, tente novamente!';
    }}}}
?>

<?php include 'upload_banner.php'; ?>
<form action="" method="POST" enctype="multipart/form-data">
    <input size="1" type="hidden" name="codigo" value="<?php echo $res['codigo']; ?>" readonly>
    <input type="hidden" type="checkbox" name="apagar[]" value="<?php echo $res['imagem']; ?>" checked readonly>
    <label>Selecione uma nova imagem:</label><br />
    <input type="file" name="imagem[]" accept="image/*" ><br />
    <input type="submit" name="banner" value="Atualizar">
</form>

Until next time;;)

    
03.11.2017 / 14:50