0

I am new to PHP. What i am trying i have performed insertion and i will have to grab the its id so i used ;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 . 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();

 public function cars_id($this->car){
    $this->select_query="SELECT * FROM `_cars` WHERE `_id`='$this->car'";
    return $this->select();
}
8
  • Format your code properly. Commented Jul 4, 2013 at 5:30
  • 2
    Are you really doing some code AFTER a return in a function ? Commented Jul 4, 2013 at 5:34
  • Are you new to PHP or new to programming? Are you posting to SO instead of learning how to program? Commented Jul 4, 2013 at 5:35
  • @OcuS yes i am trying to pass that this-car to function cars_id(), but maybe i am writing wrong syntax for that Commented Jul 4, 2013 at 5:39
  • 1
    You learn programming from BOOKS not SO. SO should be used for edge cases :) not most basic of basic things. Commented Jul 4, 2013 at 5:53

1 Answer 1

2

The reason that the ID isn't being set is that you have a return statement before you set the variable. The moment you hit the return statement the function stops executing. 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.

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. Make sure you properly escape all $_POST variables before using them in a query, or use prepared statements.

Sign up to request clarification or add additional context in comments.

5 Comments

you mean by defining $car as global I didnt have to pass as argument and it is availble all over in the class and i can use it in a function :) just use it in a Where statement
Any variable that is a member of the class (i.e. accessed with $this->) can be accessed in any function that is also a member of that class without being passed to the function. For example if I have class A which has $foo and bar() as members, I can access $foo inside of bar() directly using $this->foo without having to pass it in.
the function can you use like $this->insert_lastid
@Snow_ash You need to accept answers as well..if it suffice for the query been posted.
@badwolf i am not getting the id not at all i have tried to make $this-car=22 but the query did work :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.