我有一个将数组值传递给 create_parts.php 的表单,然后我尝试将这些值传递给parts类以插入到数据库中。此时我刚丢失......表单传递给 create_parts.php ,我可以回显数组的结果。但是在我的课程中我无法获得任何值,因此我插入数据库只是一堆NULL
。
<?php
include_once '../config/database.php';
include_once '../objects/parts_object.php';
$database = new Database();
$db = $database->getConnection();
$parts= new parts($db);
$parts->est_part_idArr=$_POST['est_part_id'];
$parts->part_qtyArr=$_POST['part_qty'];
$parts->part_numArr=$_POST['part_num'];
$parts->part_discArr=$_POST['part_disc'];
$parts->part_costArr=$_POST['part_cost'];
$parts->create();
我能够在这里回应结果并获得
{"est_part_idArr":["123","124"],"part_qtyArr":["4","6"],"part_numArr":
["2334","3344"],"part_discArr":["part","parts"],"part_costArr":["56","33"]
并回显$ stmt
{"queryString":"INSERT INTO \r\n partsT\r\n SET \r\n est_part_id=:est_part_id, part_qty=:part_qty, part_num=:part_num, part_disc=:part_disc, part_cost=:part_cost"}
但是在 part_object.php 中我无法正常工作。我可以插入数据库,但它全是NULL
。
class parts{
private $conn;
private $table_name = "partsT";
public $est_part_idArr;
public $part_qtyArr;
public $part_numArr;
public $part_discArr;
public $part_costArr;
public function __construct($db){
$this->conn = $db; }
function create(){
$query = "INSERT INTO
" . $this->table_name . "
SET
est_part_id=:est_part_id, part_qty=:part_qty, part_num=:part_num, part_disc=:part_disc, part_cost=:part_cost";
$stmt = $this->conn->prepare($query);
if(!empty($this->$est_part_idArr)){
for ($i = 0; $i < count($this->$est_part_idArr); $i++) {
if(!empty($this->$est_part_idArr[$i])){
$est_part_id = $this->$est_part_idArr[$i];
$part_qty = $this->$part_qtyArr[$i];
$part_num = $this->$part_numArr[$i];
$part_disc = $this->$part_discArr[$i];
$part_cost = $this->$part_costArr[$i];
$stmt->execute(array(
':est_part_id' => $est_part_id,
':part_qty' => $part_qty,
':part_num' => $part_num,
':part_disc' => $part_disc,
':part_cost' => $part_cost));
}
}
}
$this->est_part_idArr=htmlspecialchars(strip_tags($this->est_part_idArr));
$this->part_qtyArr=htmlspecialchars(strip_tags($this->part_qtyArr));
$this->part_numArr=htmlspecialchars(strip_tags($this->part_numArr));
$this->part_discArr=htmlspecialchars(strip_tags($this->part_discArr));
$this->part_costArr=htmlspecialchars(strip_tags($this->part_costArr));
$stmt->bindParam(":est_part_id", $this->est_part_idArr[0]);
$stmt->bindParam(":part_qty", $this->part_qtyArr[0]);
$stmt->bindParam(":part_num", $this->part_numArr[0]);
$stmt->bindParam(":part_disc", $this->part_discArr[0]);
$stmt->bindParam(":part_cost", $this->part_costArr[0]);
if($stmt->execute()){
return true;
}else{
return false;
}
}
此时我知道
public $est_part_idArr;
public $part_qtyArr;
public $part_numArr;
public $part_discArr;
public $part_costArr;
错了我只是无法得到任何工作。
答案 0 :(得分:0)
您在变量前面缺少一些$this
来访问您的公共类成员。让我们以此行为例:
$est_part_id = $est_part_idArr[$i];
在我看来应该是这样的:
$est_part_id = $this->est_part_idArr[$i];
另一方面,当您将参数绑定到代码中的某些行时,您正在使用以下内容:
$stmt->bindParam(":est_part_id", $this->est_part_id);
我认为应该
$stmt->bindParam(":est_part_id", $this->est_part_idArr);
或
$stmt->bindParam(":est_part_id", $this->est_part_idArr[0]);