1

I've done some research and figured out that it's not possible to do through one query, but instead could for example be done through a transaction. But I can't get the SQL query to work properly. If I use the statements one by one they work all good.

What should I do?

The goal is to update two tables with information from one form.

$query = "

START TRANSACTION;

UPDATE projects

SET

projects.project_name = '".$mysqli->real_escape_string($_POST['project_name'])."',

projects.project_group = '".$mysqli->real_escape_string($_POST['project_group'])."',

projects.project_notes = '".$mysqli->real_escape_string($_POST['project_notes'])."',

projects.project_created = '".$mysqli->real_escape_string($_POST['project_created'])."',

projects.project_start = '".$mysqli->real_escape_string($_POST['project_start'])."',

projects.project_delivery  = '".$mysqli->real_escape_string($_POST['project_delivery'])."',

projects.project_orderdetails = '".$mysqli->real_escape_string($_POST['project_orderdetails'])."',

projects.project_owner = '".$mysqli->real_escape_string($_POST['project_owner'])."'

where projects.project_id = '".$mysqli->real_escape_string($_REQUEST['id'])."';

INSERT INTO hours

hours.userhours_id = '".$mysqli->real_escape_string($_POST['project_owner'])."',

hours.projecthours_id = '".$mysqli->real_escape_string($_REQUEST['id'])."',

hours.user_hours = '".$mysqli->real_escape_string($_POST['user_hours'])."'

where projects.project_id = '".$mysqli->real_escape_string($_REQUEST['id'])."';

COMMIT;

";
5
  • @Scuzzy I've done some joins also but since one is update and the other is insert will it still work? Commented May 27, 2018 at 7:56
  • Sorry, Deleted my comment as It wasn't accurate, stackoverflow.com/questions/39139831 looks interesting Commented May 27, 2018 at 7:57
  • No worries :) I've looked at that one before without success but will try to study it again and see what my error might be Commented May 27, 2018 at 7:58
  • got the table aliases or full table names in play? Commented May 27, 2018 at 7:59
  • Yeah, I've tried to run them one by one and both works perfect but not combined. Commented May 27, 2018 at 8:21

1 Answer 1

1

Mysqli has methods for working with transactions, for example http://php.net/manual/en/mysqli.begin-transaction.php.

Code can be:

$mysqli->begin_tansaction();
$mysqli->query('query1');
$mysqli->query('query2');
$mysqli->commit();
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, went for two querys and executed as you wrote with my statements. All good for now :)

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.