简体   繁体   English

如何使用pdo在数据库中输入图像?

[英]How to input image in database using pdo?

I use PDO to input data to database. 我使用PDO将数据输入数据库。 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 未定义的索引:第23行的C:\\ xampp \\ htdocs \\ data \\ add-buku.php中的gambar SQLSTATE [HY093]:无效的参数编号:绑定变量的数量与令牌的数量不匹配

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 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>

May be you have not added enctype="multipart/form-data" in your form. 可能是您尚未在表单中添加enctype =“ multipart / form-data”。 This is also give like that error. 这也给像那个错误。 Better post your html code also 更好地还发布您的html代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM