1

I have 4 queries that run on a post. I've noticed that not all the queries are working if one of the first queries have no work to do.

Here are my queries

mysql_query("UPDATE invoice SET company='$company' WHERE company='$oldcompanyname'") or die(mysql_error());

mysql_query("UPDATE bill SET customer='$company' WHERE customer='$oldcompanyname'") or die(mysql_error());

mysql_query("UPDATE project SET customer='$company' WHERE customer='$oldcompanyname'") or die(mysql_error());

mysql_query("INSERT INTO activity_log (item, date) VALUES ('Customer Editted', NOW()) ") or die(mysql_error());

To give you an example, the first one runs ok. But the second one has no work to do because the data in the field does not exist. The third and the fourth should run but they do not.

I have always been accustomed to appending my queries with "or die(mysql_error());" but I'm thinking now that is not the best choice. But shouldn't the remaining queries run even if the one in the middle has no work to do?

If there is work to be done in all 4, then it works fine.

Tom
  • 363
  • 2
  • 11
  • 21
  • 1
    `die()` basically exits the script, you should know just from the name. So if the second one fails, the script will stop from executing, thus the 3rd and the 4th don't get executed ! – HamZa May 06 '13 at 23:37
  • 2
    The second one shouldn't get an error just because there's no matching row. – Barmar May 06 '13 at 23:38
  • Ah forgot to say that die is the equivalant of `exit()` and you can send a message to the client, and that's why you use mysql_error to show the error – HamZa May 06 '13 at 23:40
  • If the script is in fact exiting before finishing the other queries, what error are you getting? Try not using die() and echo the error if there is one. It's possible that the variables you think are empty are actually putting something in that makes the SQL invalid, rather than simply returning 0 rows. – BrianH May 06 '13 at 23:44
  • 2
    @BrianDHall `die()` echoes the error. – Barmar May 06 '13 at 23:45
  • @Barmar It certainly should! That's what seems odd, as OP didn't mention any error being received. – BrianH May 06 '13 at 23:47
  • Not receiving any errors, I have a header redirection after post and that works. – Tom May 07 '13 at 00:04

2 Answers2

4

@HamZa DzCyberDeV is right, if one of your first queries fails, the die() call will stop the rest of your script from executing just like exit(). You are much better off removing the die statement and using if/else in cases where you need to run a query only if another one completes.

$result = mysql_query("UPDATE invoice SET company='$company' WHERE company='$oldcompanyname'");
if ($result !== false) {
   // do something here or execute the next query
}

Also, don't use mysql_* functions in new code Why shouldn't I use mysql_* functions in PHP?. They are no longer maintained and are officially deprecated https://wiki.php.net/rfc/mysql_deprecation. Learn about Prepared statements https://en.wikipedia.org/wiki/Prepared_statement instead, and use PDO http://php.net/pdo or MySQLi http://php.net/mysqli.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Revent
  • 2,091
  • 2
  • 18
  • 33
  • 2
    But a query that doesn't have anything to do should not cause `mysql_query` to fail. It only fails if there's an error in the query. This is usually due to a bug in the program, such as not escaping input properly, and it's appropriate to die. – Barmar May 06 '13 at 23:44
  • Hmm. Not the header and seems to be working now don't know why. I closed and reopened the browser. Could it have anything to do with sessions? – Tom May 07 '13 at 00:08
  • @Barmar you are correct. It might also fail if it is expecting a certain data type, like trying to shove a string into an int field, etc., or exceeding his data type length. He is not checking posted data that we can see in his code. – Revent May 07 '13 at 00:45
1

This sounds like you should just normalize your data. If you had your customer table linked based off of an ID, you could just do:

UPDATE `company` SET name='$new_name' WHERE company_id=$id

And then in your invoice/bill/project tables you would have a foreign key for company_id, instead of basing it off of company_name.

dave
  • 62,300
  • 5
  • 72
  • 93
  • Thanks for all your responses. I think I might be creeping up on an answer. Is it possible after die that my header redirection could be kicking in? I have the following at the top of my script. if($_SERVER['REQUEST_METHOD'] == "POST") { header("Location: viewcustomers.php"); } – Tom May 07 '13 at 00:02
  • Maybe. Some browsers redirect as soon as they see the header redirect, some wait for the full response (AFAIK) – dave May 07 '13 at 00:36
  • Either way, if there's a redirect the content won't be displayed. You shouldn't send the header until after you've updated everything successfully. – Barmar May 07 '13 at 01:06