Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a task I've been trying to solve, to do update and insert query into different tables in one click of button.
I did the code as below, the update side of the code performed ok, but the insert part could not insert to the table.

How do I reposition my code to be able to achieve my aim. I am relatively new in php, I have been a C# person.

NB: I called my database connection from config file.

What I have tried:

$code = $_POST['code'];
		$name = $_POST['name'];
		$description = $_POST['description'];
		$qty_stock = $_POST['qty_stock'];
		$price = $_POST['price'];

 $qty_stock = $_POST['qty_stock'];
				 $update_return_inventory = "UPDATE `product_list` SET qty_stock = qty_stock - $qty_stock  WHERE `code` = '{$code}'";			
			     $update_invetroty_save = $this->conn->query($update_return_inventory);
				if ($update_invetroty_save)
				{				
                   //Performing Insert to another table  			
					$insert_inventory_log = "INSERT INTO `inventory_log`(`code`, `name`, `description`, `qty_stock`, `price`) values ('$code', '$name', '$description', '$qty_stock', '$price')";
					$insert_invetroty_save = $this->conn->query($insert_inventory_log);
					if ($insert_invetroty_save)
					{
						$this->settings->set_flashdata("Inventory update successfully saved.");
					}
				}		
Posted
Updated 27-Aug-23 2:41am
v7
Comments
OriginalGriff 14-Jul-23 1:10am    
"It's not working" is one of the most useless problem descriptions we get: it tells us absolutely nothing about the problem. We don't know if you get an error message, or the wrong data, or even that that code compiles successfully!
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
So tell us what happens when you run that code, what you expected to happen, how you checked what happened. Help us to help you!
Use the "Improve question" widget to edit your question and provide better information.
Member 15627495 14-Jul-23 1:35am    
It's a syntax and keyword error.

INSERT INTO table(field_1, ....) VALUES ( value_1, .... ) ;



read about 'commit()' function on php.net and SQLi,
it's good code after an update or an insert :)

it forces the validation of new datas. best pratice this one.

1 solution

On a more awake inspection, a few things occur:
1) The syntax of an INSERT command doesn't contain the SET keyword and requires parentheses: SQL INSERT INTO Statement[^]
2) Always list the fields you are INSERTing into: if you don't then SQL assumes "left to right" order for the columns - and since many tables contain an IDENTITY of UNIQUEIDENTIFER column at the start your new data will try to set that and fail. Listing the columns makes your code more robust and able to handle DB changes without your code needing to be modified.
3) 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?
 
Share this answer
 

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