Sufi
Sufi

Reputation: 238

How to input image in database using pdo?

I use PDO to input data to database. I try use this scripts but it doesn't work for image file. This error message:

Notice:

Undefined index: gambar in C:\xampp\htdocs\data\add-buku.php on line 23 SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

How to fix it? Thanks in advance

<?php if(isset($_POST['input-buku'])){
$id_buku=trim($_POST['id_buku']);
$judul_buku=trim($_POST['judul_buku']);
$isbn=trim($_POST['ISBN']);
$harga=trim($_POST['harga']);
$gambar=trim($_FILES['gambar']['name']);
if(strlen($gambar)>0){
if(is_uploaded_file($_FILES['gambar']['tmp_name'])){
move_uploaded_file($_FILES['gambar']
['tmp_name'],"../images/buku".$gambar);
} } try {
    $stmt = $con->prepare("SELECT id_buku from tb_buku where id_buku=:id_buku ");
        $stmt->execute(array(':id_buku'=>$id_buku));
        $row=$stmt->fetch(PDO::FETCH_ASSOC);

        if($row > 0) 
        {
            $error[] = "maaf ID yang Anda inputkan sudah ada !";
        }
        else
        {
            if($crud->create_buku($id_buku, $judul_buku,$isbn, $harga  $gambar))
            {
                $crud->redirect('add-buku.php?berhasil');
            }
        }
    }
    catch(PDOException $e)
    {
        echo $e->getMessage();
    }
}?>

HTML

 <form role="form" method="post">
<div class="form-group">
<label for="id_buku">ID Buku</label>
<input type="text" class="form-control" name="id_buku" value="<?php if(isset($error)){echo $id_buku;}?>" placeholder="Masukan ID Buku">
</div><div class="form-group">
<label for="ISBN">ISBN</label>
<input type="text" class="form-control" name="ISBN" placeholder="Masukan ISBN"></div>
<div class="form-group">
<label for="judul_buku">Judul Buku</label>
<input type="text" class="form-control" name="judul_buku" value="<?php if(isset($error)){echo $judul_buku;}?>" placeholder="Masukan Judul Buku"></div>
<div class="form-group">
<label for="harga">Harga</label>
<input type="text" class="form-control" name="harga" value="<?php if(isset($error)){echo $harga;}?>" placeholder="Masukan Harga Buku"></div>
<div class="form-group">
<label for="gambar">Gambar</label>
<input type="file" name="gambar" value="<?php if(isset($error)){echo $gambar;}?>">
<p class="help-block">Pilih Gambar Buku</p></div>
 <div class="box-footer">
<button type="submit" class="btn btn-primary">Submit</button></div>
</form>

Upvotes: 1

Views: 117

Answers (1)

Sivabalan
Sivabalan

Reputation: 1131

May be you have not added enctype="multipart/form-data" in your form. This is also give like that error. Better post your html code also

Upvotes: 1

Related Questions