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

时间:2015-07-09 08:51:23

标签: php arrays

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

例如:

我有一个阵列费率[10,20,30,70,200]。

现在,当我将此数组传递给负责插入这些值的函数时,我希望创建一个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(列):20。

依此类推......

这就是我传递价值的方式:

$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();

?>

1 个答案:

答案 0 :(得分:0)

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"]);