This topic is locked
[SOLVED]

 How to check if datefield is empty in AFTER RECOD UPDATED event ?

5/7/2019 12:22:28 PM
PHPRunner General questions
R
roeland author

Good afternoon!
I hope someone can help me. I try to update fields in a table, it works perfect, but when the date is empty, I have issues:
I have 2 tables: ORDER and ORDERINHOUD.

When order.orderbevestiging is updated, I need to update also the records orderinhoud.orderbevestiging when they meet some criteria as you can see below.
It works fine for other queries, but with an empty datefield in ORDER, it still updates the records in ORDERINHOUD with value 00-00-0000 And the idea is that when the value in order is empty, it does not update those records in ORDERINHOUD.

As you can see below, I tried already different things, but none of those have the result that the other table is not updated.
Hope someone can help me with this!
$sql = "UPDATE orderinhoud, order

SET orderinhoud.orderbevestiging = '".$values['orderbevestiging']."'
WHERE (orderinhoud.orderbevestiging IS NULL OR orderinhoud.orderbevestiging = '') AND order.orderbevestiging IS NOT NULL AND order.orderbevestiging != 0 AND order.orderbevestiging <>'' AND order.orderbevestiging <>'0-0-0000'AND orderinhoud.orderid='".$values['id']."'";
db_exec($sql);

W
WilliamBDevClub member 5/7/2019

Have you tried



`order`.orderbevestiging!='0-0-0000'
R
roeland author 5/7/2019



Have you tried



`order`.orderbevestiging!='0-0-0000'



Thanks for your quick reply. I didtry it now. Unfortunately, also now the query still updates the other table with the 00-00-0000 value
"

$sql = "UPDATE orderinhoud, order

SET orderinhoud.orderbevestiging = '".$values['orderbevestiging']."'
WHERE (orderinhoud.orderbevestiging IS NULL OR orderinhoud.orderbevestiging = '') AND order.orderbevestiging!='0-0-0000'AND order.orderbevestiging IS NOT NULL AND order.orderbevestiging != 0 AND order.orderbevestiging <>'' AND order.orderbevestiging <>'0-0-0000' AND orderinhoud.orderid='".$values['id']."'";
db_exec($sql);

"

R
roeland author 5/7/2019

In the meantime I tried many different things. But always the same result:
$sql = "UPDATE orderinhoud, order

SET orderinhoud.orderbevestiging = '".$values['orderbevestiging']."'
WHERE (orderinhoud.orderbevestiging IS NULL OR orderinhoud.orderbevestiging = '')

AND '".$values['orderbevestiging']."' IS NOT NULL

AND order.orderbevestiging != 0

AND order.orderbevestiging !='0000-0-0'

AND order.orderbevestiging !='0000-00-00'

AND order.orderbevestiging !='0000-00-00 00:00:00'

AND order.orderbevestiging !='00-00-0000'

AND order.orderbevestiging !='0-0-0000'

AND order.orderbevestiging IS NOT NULL

AND order.orderbevestiging != 0

AND order.orderbevestiging <>''

AND order.orderbevestiging <>'0-0-0000'
AND orderinhoud.orderid='".$values['id']."'";
db_exec($sql);

/*
nothing works. I hope someone has an idea

A
acpan 5/7/2019

Assumming you are working on another table and after updating it, you want to update another 2 tables: order and orderinhoud in the background.
Your query statement itself that you update 2 tables at one go is quite dangerious practice in my view and hard to troubleshoort when there is problem (like in this case).
For readability and ease of troubleshooting, you maybe need to break the update query into 2 parts, so in after update event of the form, try this:
$orderbevestiging = $values['orderbevestiging'];

$id = $values['id'];
// do your checking for empty or null field here, then update accordingly

if ($values['orderbevestiging'] == "" OR $values['orderbevestiging'] == NULL)

{

$sql = "UPDATE order SET orderbevestiging = '$orderbevestiging' WHERE orderid= $id ";

db_exec($sql);

}

else

{

$sql = "UPDATE order SET orderbevestiging = '$orderbevestiging' WHERE orderid= $id ";

db_exec($sql);

$sql = "UPDATE orderinhoud SET orderbevestiging = '$orderbevestiging' WHERE orderid= $id ";

db_exec($sql);

}
Hope it helps

ACP

R
roeland author 5/8/2019



Assumming you are working on another table and after updating it, you want to update another 2 tables: order and orderinhoud in the background.
Your query statement itself that you update 2 tables at one go is quite dangerious practice in my view and hard to troubleshoort when there is problem (like in this case).
For readability and ease of troubleshooting, you maybe need to break the update query into 2 parts, so in after update event of the form, try this:
$orderbevestiging = $values['orderbevestiging'];

$id = $values['id'];
// do your checking for empty or null field here, then update accordingly

if ($values['orderbevestiging'] == "" OR $values['orderbevestiging'] == NULL)

{

$sql = "UPDATE order SET orderbevestiging = '$orderbevestiging' WHERE orderid= $id ";

db_exec($sql);

}

else

{

$sql = "UPDATE order SET orderbevestiging = '$orderbevestiging' WHERE orderid= $id ";

db_exec($sql);

$sql = "UPDATE orderinhoud SET orderbevestiging = '$orderbevestiging' WHERE orderid= $id ";

db_exec($sql);

}
Hope it helps

ACP


Thanks a lot ACP!!
This did the trick. specially, taking the values out with the first 2 lines. Somehow that works for dates better then in the SQL query. I am not completely sure where the first statement is for:
$sql = "UPDATE order SET orderbevestiging = '$orderbevestiging' WHERE orderid= $id ";
in this case you update the table order with its own values, but that is already done in phprunner right? Or is there another reason.
Anyway, unless the above has a special reason, I have a working code now as follows:
$orderbevestiging = $values['orderbevestiging'];

$id = $values['id'];
// do your checking for empty or null field here, then update accordingly

if ($values['orderbevestiging'] != "" OR $values['orderbevestiging'] != NULL)

{

$sql = "UPDATE orderinhoud SET orderbevestiging = '$orderbevestiging' WHERE (orderinhoud.orderbevestiging IS NULL OR orderinhoud.orderbevestiging = '') AND orderid= $id ";

db_exec($sql);

}
Thanks a lot! Really appreciated! If you are a PHPrunner expert, and looking for some commercial work, let me know as I am looking for someone to help me out many times :-) :-)

A
acpan 5/8/2019

Hi,
"I am not completely sure where the first statement is for:

$sql = "UPDATE order SET orderbevestiging = '$orderbevestiging' WHERE orderid= $id ";

in this case you update the table order with its own values, but that is already done in phprunner right? Or is there another reason."
ACP: This is due to misundertood your question. I mentioned: "Assumming you are working on another table and after updating it, you want to update another 2 tables: order and orderinhoud in the background." Because you did not state you are actually working on order table itself and your codes looked like you are working on a 3rd table that will update order and orderinhoud in the background.
For the code you paste, please look at my comment below:



$orderbevestiging = $values['orderbevestiging'];

$id = $values['id'];
// do your checking for empty or null field here, then update accordingly

if ($values['orderbevestiging'] != "" OR $values['orderbevestiging'] != NULL)

{
// ACP: you don't need to use SQL to check again, as you already check the null or empty values in above if statement.

// ACP: you can put more strigent check in the PHP if statement itself.

// example to put more checks: if ( $values['orderbevestiging'] != "" OR $values['orderbevestiging'] != NULL OR strlen($values['orderbevestiging']) >= 8 )
// So, your SQL can be more simple:
// $sql = "UPDATE `orderinhoud` SET orderbevestiging = '$orderbevestiging' WHERE (`orderinhoud`.orderbevestiging IS NULL OR `orderinhoud`.orderbevestiging = '') AND orderid= $id ";
// Should be:
$sql = "UPDATE `orderinhoud` SET orderbevestiging = '$orderbevestiging' WHERE orderid= $id ";

db_exec($sql);

}


Thanks a lot! Really appreciated! If you are a PHPrunner expert, and looking for some commercial work, let me know as I am looking for someone to help me out many times :-) :-)

[/quote]
ACP: No, not expert but creating app using phpr is fun for me sometimes <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=87747&image=1&table=forumreplies' class='bbc_emoticon' alt=':)' />
Glad it helped.
ACP.