1

我用 func_get_args 创建了一个 PHP 函数来将数据插入 MySQL。第一个参数是表,然后是字段和值。它仅适用于 3 个参数,但不适用于更多参数。

public function createDato($list)
{
    try
    {
        $numArgs = func_num_args()/2;
        $argList = func_get_args();
        $j=1;
        if (func_num_args() < 3) die("No se han pasado suficientes elementos");
        $query = "INSERT INTO $argList[0] SET";
        for ($i = 1; $i < $numArgs; $i++) {
            $query .=" $argList[$i] = ?";
        }           
        $this->stmt = $this->db->prepare($query);           
        for ($i; $i < func_num_args(); $i++) {
            $this->stmt->bindParam($j, $argList[$i]);
            $j++;
        }
        if($this->stmt->execute())
        {
            echo "<br>".func_get_arg(0)." creados";
        }
        else
            die('<br>Error creando tupla');
    }
    catch (PDOException $exception)
    {
        echo "Error: " . $exception->getMessage();
    }
}
4

0 回答 0