如何将循环值作为集合数组传递并插入一次


How to pass looping value as a collective array and insert once

我将foreach值传递到一个函数中,以便插入数据库。WHta现在所做的是,它不是为一个ID存储多个值,而是为每个值创建一个ID。

例如:

我有一个数组速率[10,20,30,70200]。

现在,当我将这个数组传递给负责插入这些值的函数时,我希望创建一个id来存储所有这些值。类似:

Id(column):1 Day_1(column):10 Day_3(column):20 Day_7(column):30 Day_15(column):70 Day_30(column):200.

但它的存储方式是这样的:

Id(列(:1 Day_1(列(:10 Day_3(列(=10 Day_7(列(%10 Day_15(列(+10 Day_30(列(:10。

Id(列(:2 Day_1(列(:20 Day_3(列(+20 Day_7(列(=20 Day_15(列(/20 Day_30(列(:120。

等等。。。

这就是我传递值的方式:

$rate=$data["rate"];
foreach($rate as $key=>$value)
{
    echo $key."->";
    echo $value;
    $car->rate = $value;
    $car->rentalRate();
}

这就是函数捕获值并多次插入的方式:

public function rentalRate($rate = NULL)
{
    $rate = $rate == NULL ? $this->rate : $rate;
    echo "Rate inserted!";
    $this->generateReport();
    echo $rate;
    $sql="INSERT INTO rental(day_1,day_3,day_7,day_15,day_30)VALUES('{$rate}','{$rate}', '{$rate}','{$rate}','{$rate}')";
    $stmt =connection::$pdo->prepare($sql);
    $stmt->execute();
}

我尝试了这种方式,将速率数组本身传递给函数

$rate=$data["rate"];//array rate[10,20,40,60,80]
$car->rate = $rate;//assigning the variable in the function with array value
$car->rentalRate();//calling the function

//在功能中,

 public function rentalRate($rate = NULL)
        {
            $rate=array();
            $rate = $rate == NULL ? $this->rate : $rate;
            echo "Rate inserted!";
            $this->generateReport();
            echo $rate;
            $sql="INSERT INTO rental(day_1,day_3,day_7,day_15,day_30)VALUES('{$rate[0]}','{$rate[1]}', '{$rate[2]}','{$rate[3]}','{$rate[4]}')";
            $stmt =connection::$pdo->prepare($sql);
            $stmt->execute();
        }

这只插入一条记录,我想要,但值为0。也许有人能告诉我如何获得速率数组值并插入到表中吗?恐怕如果我在这里使用foreach,它仍然会为每个值插入多条记录。。

我的全功能

<?php
interface db
{
    public static function addConnection();
}
interface rental
{
    public function addCar();
    public function rentalRate();
}
abstract class report
{
    public function generateReport()
    {
        echo "All done.Now generate report.";
    }
}
class connection implements db 
{
    public static $servername = "localhost";
    public static $username = "root";
    public static $password = "";
    public static $dbname = "carrental";
    public static $port="3306";
    public static $pdo;
    public static function addConnection()
    {
      try 
      {
          self::$pdo = new PDO("mysql:host=localhost;port=3306;dbname=carrental", self::$username, self::$password);
          self::$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      } catch(PDOException $e)
      {
          echo 'ERROR: ' . $e->getMessage();
      }
      self::$pdo->query("use carrental");
    }
}
class car extends report implements rental
{
    public $name;
    public $maker;
    public $type;
    public $colour;
    public $passanger;
    public $rate;
    public function __construct($param1,$param2,$param3,$param4,$param5,$param6)
    {
        $this->name=$param1;
        $this->maker=$param2;
        $this->type=$param3;
        $this->colour=$param4;
        $this->passanger=$param5;
        $this->rate=$param6;
        connection::addConnection();
    }
    public function addCar()
    {
        $sql="INSERT INTO car(car_name,car_maker,car_type,car_colour,num_passanger)VALUES('{$this->name}','{$this->maker}', '{$this->type}','{$this->colour}','{$this->passanger}')";
        $stmt =connection::$pdo->prepare($sql);
        $stmt->execute();
        echo "Data inserted!";
        //$this->rentalRate();
    }
    public function rentalRate($rate = NULL)
    {
        $rate=array();
        $rate = $rate == NULL ? $this->rate : $rate;
        echo "Rate inserted!";
        $this->generateReport();
        echo $rate;
        $sql="INSERT INTO rental(day_1,day_3,day_7,day_15,day_30)VALUES('{$rate[0]}','{$rate[1]}', '{$rate[2]}','{$rate[3]}','{$rate[4]}')";
        $stmt =connection::$pdo->prepare($sql);
        $stmt->execute();
    }
}
//$car1=new car("Honda Accord","Honda","5 wheeler","Red",10);
//$car1->addCar();
//$car1->generateReport();
?>
public function rentalRate($rate = NULL)
{
    $rate = (NULL===$rate) ? $this->rate : $rate;
    if ( !is_array($rate) || 5!=count($rate) ) {
        throw new InvalidArgumentException('$rate must be an array of five elements');
    }
    // since you already use prepare() make use of the placeholder/parameter functionality
    $sql='INSERT INTO rental(day_1,day_3,day_7,day_15,day_30)VALUES(?,?,?,?,?)';
    // hopefully the error mode has been set to PDO::ERRMODE_EXCEPTION ?
    // otherwise error handling code is required here
    $stmt =connection::$pdo->prepare($sql);
    // same here: expections or error handling is required
    $stmt->execute($rate);
}

然后忘记foreach循环,只调用$car->rentalRate($data["rate"]);