2016-08-05 5 views
1

bind_paramを使用してDB挿入を実行する際に問題がありますが、同じbind_paramメソッドで更新できますが挿入できません。ここで私の更新機能(動作しているもの)と挿入(動作していないもの)。バインドパラメータを使用してクエリが挿入されないのはなぜですか?同じbind_paramを使用して更新します

function update() 
{ 
    $query = " 
     UPDATE " . $this->table_name . " 
      SET category_key= :ck, 
       product_name = :pn, 
       unit_of_measurement = :uom, 
       price = :pc, 
       pieces = :ps, 
       last_date_added = :lda, 
       last_added_by = :lab, 
       reorder = :rd, 
       note = :no 
     WHERE product_key LIKE :pk 
    "; 

    $stmt = $this->conn->prepare($query); 

    $stmt->bindParam(':ck', $this->category_key); 
    $stmt->bindParam(':pn', $this->product_name); 
    $stmt->bindParam(':uom', $this->unit_of_measurement); 
    $stmt->bindParam(':pc', $this->price); 
    $stmt->bindParam(':ps', $this->pieces); 
    $stmt->bindParam(':lda', $this->last_date_added); 
    $stmt->bindParam(':lab', $this->last_added_by); 
    $stmt->bindParam(':no', $this->note); 
    $stmt->bindParam(':pk', $this->product_key); 
    $stmt->bindParam(':rd', $this->reorder); 

    // execute the query 
    if ($stmt->execute()) { 
     return true; 
    } else { 
     return false; 
    } 
} 


function insert() 
{ 
    $query = " 
     INSERT INTO product_entry (
       category_key, 
       product_key, 
       product_name, 
       unit_of_measurement, 
       price, 
       pieces, 
       date_added, 
       added_by, 
       exp_date, 
       store_key, 
       branch_code, 
       note 
     ) 
     VALUES (
       :ck, 
       :pk, 
       :pn, 
       :uom, 
       :pc, 
       :ps, 
       :lda, 
       :lab, 
       :ep, 
       :sk, 
       :bc, 
       :no 
    )"; 
    $stmt = $this->conn->prepare($query); 

    $stmt->bindParam(':sk', $this->store_key); 
    $stmt->bindParam(':bc', $this->branch_code); 
    $stmt->bindParam(':ck', $this->category_key); 
    $stmt->bindParam(':pn', $this->product_name); 
    $stmt->bindParam(':uom', $this->unit_of_measurement); 
    $stmt->bindParam(':pc', $this->price); 
    $stmt->bindParam(':ps', $this->pieces); 
    $stmt->bindParam(':lda', $this->last_date_added); 
    $stmt->bindParam(':lab', $this->last_added_by); 
    $stmt->bindParam(':no', $this->note); 
    $stmt->bindParam(':pk', $this->product_key); 
    $stmt->bindParam(':ep', $this->exp_date); 

    // execute the query 
    if ($stmt->execute()) { 
     return true; 
    } else { 
     return false; 
    } 
} 

そして、ここに私のHTMLフォームは、ちょうど包み

<form method="post">  


<div class="form-group"> 
<input type="hidden" class="form-control" id="sk" name="store_key" value='<?php echo $product->store_key; ?>'> 
<input type="hidden" class="form-control" id="bc" name="branch_code" value='<?php echo $product->branch_code; ?>'> 
<label for="pn">Product Name</label> 
<input type="text" class="form-control" id="pn" name="product_name" value='<?php echo $product->product_name; ?>'> 
</div> 
<div class="form-group"> 
    <label for="ms">Measurement</label> 
    <input type="text" class="form-control" id="uom" name="unit_of_measurement" value='<?php echo $product->unit_of_measurement; ?>'> 
</div> 
<div class="form-group"> 
    <label for="qn">Price</label> 
    <input type="text" class="form-control" rows="3" id="pc" name="price" value='<?php echo $product->price; ?>'/> 
</div> 
<div class="form-group"> 
    <label for="ps">Pieces</label> 
    <input type="text" class="form-control" id="ps" name="pieces" value='<?php echo $product->pieces; ?>'/> 
</div> 
<div class="form-group"> 
    <label for="rd">Re-order</label> 
    <input type="text" class="form-control" id="rd" name="reorder" value='<?php echo $product->reoder; ?>'/> 
</div> 
<div class="form-group"> 
    <label for="ep">Expiry Date</label> 
    <input type="date" class="form-control" id="ep" name="exp_date" value='<?php echo $product->exp_date; ?>'/> 
</div> 
<div class="form-group"> 
    <label for="ab">Added By</label> 
    <input type="text" class="form-control" id="lab" name="last_added_by" value='<?php echo $product->last_added_by; ?>'/> 
</div>` 
<div class="form-group"> 
    <label for="no">Note</label> 
    <textarea class="form-control" rows="3" id="no" name="note"><?php echo $product->note; ?></textarea> 
</div> 
<button type="submit" name="submit" class="btn btn-success">Submit</button> 
</form> 

で更新および挿入の両方が同じクラスから呼び出され、製品テーブルを更新し、同じフォームとPHPページ(から実行されていることに注意してください動作します)、product_entryテーブルには挿入されません。

+2

あなたelseステートメントにあなたが 'ますprint_r(の$ this - > conn->のerrorInfo())のような便利なものを入れた場合は、'あなたは問題が – RiggsFolly

+1

なぜテーブルが 'にハードコードされたものを自分自身に語っているだろうinsert'を実行しますが、 'update'では動的ですか?適切なテーブル名を使用していることを確認しますか? –

+0

はい、私はハードコーディングされた方法が問題ではないことを確認しました。しかし、私はあなたのガイドを試してみます。 –

答えて

0

代わりにbindValue()を使用してみてください。

$stmt = $this->conn->prepare($query); 

$stmt->bindValue(':sk', $this->store_key); 
$stmt->bindValue(':bc', $this->branch_code); 
$stmt->bindValue(':ck', $this->category_key); 
$stmt->bindValue(':pn', $this->product_name); 
$stmt->bindValue(':uom', $this->unit_of_measurement); 
$stmt->bindValue(':pc', $this->price); 
$stmt->bindValue(':ps', $this->pieces); 
$stmt->bindValue(':lda', $this->last_date_added); 
$stmt->bindValue(':lab', $this->last_added_by); 
$stmt->bindValue(':no', $this->note); 
$stmt->bindValue(':pk', $this->product_key); 
$stmt->bindValue(':ep', $this->exp_date); 

I usualy do this way: 

$stmt->execute(array(":sk"=>$this->store_key,":bc"=>$this->branch_code,":ck"=>$this->category_key,"cn"=>$this->product_name),":uom"=>$this->unit_of_measurement,":pc"=>$this->price,":ps"=>$this->pieces,":lda"=>$this->last_date_added,":lab"=>$this->last_added_by,":no"=>$this->note,":pk"=>$this->product_key,":ep"=>$this->exp_date)); 
関連する問題