我在使用bind_param执行DB插入时遇到问题,但我可以使用相同的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>
请注意,update和insert都是从同一个类调用的,并且是从相同的表单和php页面执行的,该页面更新产品表(有效(,但不插入到product_entry表中。
请尝试使用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));