Page 1 of 1

Re-writing 3rd party addon to work with Phoenix

Posted: Wed Apr 13, 2022 10:40 am
by LeeFoster
Good morning Phoenix users.

I am currently modifying a 3rd party social media clone to fully work with Phoenix, currently works locally but not live, with that in mind I am creating this thread to ask questions where there is code that I am not sure how to convert to the Phoenix standard.

There are also some concerns over the fact the addon has it's own db connection that I would like to move away from, hence the code updates.

So here is my first issue, the below section of code puts a post into the db and returns the id to be used in later code.

Code: Select all

$query = mysqli_query($this->con, "INSERT INTO posts VALUES ('', '$body', '$added_by', '$user_to', '$date_added', 'no', 'no', '0', '$imageName')");

            $returned_id = mysqli_insert_id($this->con);
This is my progress so far, it puts the code into the db but does not return the id

Code: Select all

$query = tep_db_query("INSERT INTO posts VALUES ('', '$body', '$added_by', '$user_to', '$date_added', 'no', 'no', '0', '$imageName')");
            
            $returned_id = mysqli_insert_id($this->con);
I know $this->con needs to be changed but not sure what to.

Any help would be appreciated.

Re: Re-writing 3rd party addon to work with Phoenix

Posted: Wed Apr 13, 2022 11:14 am
by ecartz
LeeFoster wrote: Wed Apr 13, 2022 10:40 am

Code: Select all

$query = tep_db_query("INSERT INTO posts VALUES ('', '$body', '$added_by', '$user_to', '$date_added', 'no', 'no', '0', '$imageName')");
            
            $returned_id = mysqli_insert_id($this->con);
If you are using a recent version:

Code: Select all

$query = $db->query("INSERT INTO posts VALUES ('', '$body', '$added_by', '$user_to', '$date_added', 'no', 'no', '0', '$imageName')");
            
            $returned_id = mysqli_insert_id($db);
Note that the standard in Phoenix is to do the escaping just before submitting the query. If that code is escaping properly to avoid SQL injections, it is doing so outside the posted code.

It will take care of it for you if you use perform rather than query.

If this is inside a function, either do globals $db; first or use $GLOBALS['db'] instead.

You can find examples in the current code: https://github.com/CE-PhoenixCart/Phoen ... &type=code

If using an older version, use tep_db_insert_id() without specifying the connection and it will use the same one that query does.

Re: Re-writing 3rd party addon to work with Phoenix

Posted: Wed Apr 13, 2022 12:21 pm
by LeeFoster
ecartz wrote: Wed Apr 13, 2022 11:14 am
LeeFoster wrote: Wed Apr 13, 2022 10:40 am

Code: Select all

$query = tep_db_query("INSERT INTO posts VALUES ('', '$body', '$added_by', '$user_to', '$date_added', 'no', 'no', '0', '$imageName')");
            
            $returned_id = mysqli_insert_id($this->con);
If you are using a recent version:

Code: Select all

$query = $db->query("INSERT INTO posts VALUES ('', '$body', '$added_by', '$user_to', '$date_added', 'no', 'no', '0', '$imageName')");
            
            $returned_id = mysqli_insert_id($db);
Note that the standard in Phoenix is to do the escaping just before submitting the query. If that code is escaping properly to avoid SQL injections, it is doing so outside the posted code.

It will take care of it for you if you use perform rather than query.

If this is inside a function, either do globals $db; first or use $GLOBALS['db'] instead.

You can find examples in the current code: https://github.com/CE-PhoenixCart/Phoen ... &type=code

If using an older version, use tep_db_insert_id() without specifying the connection and it will use the same one that query does.
version is 1.0.8.5

$GLOBALS['db'] seems to have fixed it. On to the next bit