Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm new to programming and I have this problem.


public function create() {
        $this->load->language('sale/order');

        $json = array();

        if (!$this->user->hasPermission('modify', 'sale/order')) {
            $json['error'] = $this->language->get('error_permission');
        }
        elseif (isset($this->request->get['order_id'])) {
            $order_id = $this->request->get['order_id'];

            $this->load->model('sale/order');

            $order_info = $this->model_sale_order->getOrder($order_id);

            if ($order_info && !$order_info['invoice_no']) {
                if (($order_info['payment_firstname'] && $order_info['payment_lastname']) && ($order_info['payment_company'])) {
                     $this->db->query("
						INSERT INTO `" . DB_PREFIX . "invoice` SET
						order_id = " . (int) $order_info['order_id'] . ",
						prefix = '" . $this->db->escape($order_info['invoice_prefix']) . "',
						no = (SELECT ifnull(max(inv.no), 36552) + 1 FROM `" . DB_PREFIX . "invoice` inv WHERE inv.prefix = '" . $this->db->escape($order_info['invoice_prefix']) . "'),
						date = now(),
						type = ''
					");


                    $invoice_id = $this->db->getLastId();

                    $this->db->query("
						UPDATE `" . DB_PREFIX . "order` SET
						invoice_no = (SELECT no FROM `" . DB_PREFIX . "invoice` WHERE invoice_id = '" . (int) $invoice_id . "')
						WHERE order_id = " . (int) $order_id . "
					");
                }
            }


            if (!isset($invoice_id)) {
                $json['invoice_id'] = $invoice_id;
                $this->get(true);
            } else {
                $json['error'] = $this->language->get('error_action');

            }

        }


Notice: Undefined variable: invoice_id in /home/vagrant/code/public/admin/controller/sale/invoices.php on line 425{"error":"Warning: Could not complete this action!"}

What I have tried:

i already tried isset and empty with "! " and without it
Posted
Updated 3-Feb-22 21:51pm
v2

The error message is pretty explicit: "invoice_id" is not defined, so it has no idea what it contains.

But don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

Fix that through your whole app, and the problem you have noticed will disappear at the same time.
 
Share this answer
 
The variable $invoice_id is local to the block of code starting at the line:
PHP
if ($order_info && !$order_info['invoice_no']) {

After that block completes you have:
PHP
if (!isset($invoice_id)) {
    $json['invoice_id'] = $invoice_id;
    $this->get(true);
} else {
    $json['error'] = $this->language->get('error_action');

But $invoice_id is not known here, hence the error message.

You should learn about scope rules in PHP and how the lifetime of variables is managed.

And, more importantly, follow OriginalGriff's advice on how to guard against SQL injection.
 
Share this answer
 
Comments
George Terchila 25-Feb-22 12:38pm    
the problem is that invoice_id, bcoz I generate in database.
Richard MacCutchan 26-Feb-22 4:13am    
No the problem is as stated above. If you do not understand the rules concerning scope of variables in programming languages, you are going to have many problems.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900