简体   繁体   English

将一个函数变量作为参数传递给Class的另一个函数

[英]passing one function variable as a argument to another function of a Class

I am new to PHP. 我是PHP的新手。 What i am trying i have performed insertion and i will have to grab the its id so i used ;insert_lastid(); 我正在尝试我已经执行插入,我将不得不抓住它的id,所以我使用; insert_lastid(); function which is same as mysqli_last_insert_id ,so dont get confused with it .i tried to hold that last id on basis of last id i will perform select operation in another function Here is the code which will clear more my problem . 函数与mysqli_last_insert_id相同,所以不要混淆它。我试图在最后一个id的基础上保持最后一个id我将在另一个函数中执行select操作这里是代码,它将清除更多我的问题。 This function is for insertion 此功能用于插入

public function cars_insert($_POST,$path) {
    $this->insert_query="insert into `_cars` (`_year`,`_make`,`_model`,`_version`,`_city`,`_color`,`_mileage`,`_engine_capacity`,                                                                                 `_engine_type`,`_assembly`,`_image`)VALUES('" .$_POST['year'] . "','" .$_POST['make'] . "','" .$_POST['model'] . "','" .$_POST['version'] . "','" .$_POST['city'] . "','" .$_POST['color'] . "','" .$_POST['mileage'] . "','" .$_POST['engine_capacity'] . "','" .$_POST['engine_type'] . "','" .$_POST['assembly'] . "','" .$path . "')";
    return $this->insert();
    global $car;
    $this->car=insert_lastid();
}

Here it is the second function it will Select on basis of insert_lastid(); 这是它将基于insert_lastid();选择的第二个函数insert_lastid();

 public function cars_id($this->car){
    $this->select_query="SELECT * FROM `_cars` WHERE `_id`='$this->car'";
    return $this->select();
}

The reason that the ID isn't being set is that you have a return statement before you set the variable. 未设置ID的原因是您在设置变量之前有一个return语句。 The moment you hit the return statement the function stops executing. 当您点击return语句时,该函数将停止执行。 Move your code so that the variable assignment is done before the function returns. 移动代码,以便在函数返回之前完成变量赋值。

$insert = $this->insert();
$this->car=insert_lastid();

return $insert;

Your global $car statement and the parameter in cars_id are both unnecessary because $this->car is a member of the class and is accessible from any member function of that class without having to be passed in or global'ed as you would with a normal variable. 您的global $car语句和cars_id中的参数都是不必要的,因为$this->car是该类的成员,并且可以从该类的任何成员函数访问,而无需像传入的那样传入或全局传递。正常变量。

public function cars_id(){
    $this->select_query="SELECT * FROM `_cars` WHERE `_id`='$this->car'";
    return $this->select();
}

As a side note, your code is currently extremely vulnerable to SQL injection attacks unless you have some kind of code in $this->insert() that is checking your SQL statements before running them. 作为旁注,您的代码目前极易受到 SQL注入攻击,除非您在$this->insert()中有某种代码在运行它们之前检查您的SQL语句。 Make sure you properly escape all $_POST variables before using them in a query, or use prepared statements. 确保在查询中使用它们之前正确转义所有$_POST变量,或使用预准备语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM