Jump to content

checking multiple tables have entry not empty


zimmo

Recommended Posts

I have a system which is working well, but  I need to do one final check. I have 5 tables with different information in. Each table should have some information entered from a form, now I need to check that all tables have had data entered to return true, if any return false then it does not allow them to publish. Just like a check to make sure all information is filled in.

 

I have never done this so dont know the correct way.

 

$sql = "SELECT * FROM table_b_details, table_c_info, table_d_tickets, table_e_bank, table_f_publish WHERE fishery_id = '$_SESSION[fishery_id]' AND table_b_details.name = 'NOT NULL' etc.. "; 

 

I know this is wrong, well I think it is, not sure of the correct way to query different data in each table?

Link to comment
Share on other sites

I have tried the following but it is giving me an error. If anyone can help as never done this before.

 

$sql = "SELECT * FROM table_b_details, table_c_info, table_d_tickets, table_e_bank, table_f_publish WHERE fishery_id = '$_SESSION[fishery_id]' AND complete = 'YES' "; 
$sql_result = mysql_query($sql); 
if (mysql_num_rows($sql_result) ==0)

Link to comment
Share on other sites

This means your query failed somehow.

 

Change

$sql_result = mysql_query($sql); 

 

to

$sql_result = mysql_query($sql) or die(mysql_error().": $sql"); 

 

and post what error message it shows then.

Link to comment
Share on other sites

Oh.... you probably want this:

(SELECT complete FROM table_b_details WHERE fishery_id = '1' AND complete = 'YES')
UNION
(SELECT complete FROM table_c_info WHERE fishery_id = '1' AND complete = 'YES')
UNION
(SELECT complete FROM table_d_tickets WHERE fishery_id = '1' AND complete = 'YES')
UNION
(SELECT complete FROM table_e_bank WHERE fishery_id = '1' AND complete = 'YES')
UNION
(SELECT complete FROM table_f_publish WHERE fishery_id = '1' AND complete = 'YES')

Link to comment
Share on other sites

I have slightly changed things, and better explanation at the end. I have created a new table for the terms section so now I want it to query all 5 tables to look for the fishery_id and complete ='YES" it needs to return that all are true, in other words that each of these tables has YES in the complete field. It is not doing that.

 

Example in 4 of the tables they all have complete='YES" the last table does not as it has had no information entered, so it should return as not valid as their is no row in that table, the same can apply for any of the others, there is no particular order that the tables are filled in. It is a form system and they can switch between different tables. hope it makes sense.

 

So here is my new query:

$sql = "
(SELECT complete FROM table_b_details WHERE fishery_id = '$_SESSION[fishery_id]' AND complete = 'YES')
UNION
(SELECT complete FROM table_c_info WHERE fishery_id = '$_SESSION[fishery_id]' AND complete = 'YES')
UNION
(SELECT complete FROM table_d_tickets WHERE fishery_id = '$_SESSION[fishery_id]' AND complete = 'YES')
UNION
(SELECT complete FROM table_e_bank WHERE fishery_id = '$_SESSION[fishery_id]' AND complete = 'YES')
UNION
(SELECT complete FROM table_f_terms WHERE fishery_id = '$_SESSION[fishery_id]' AND complete = 'YES')
";
$sql_result = mysql_query($sql) or die(mysql_error().": $sql");
$count = mysql_num_rows($sql_result);
{
  echo ($count);
}

 

Based on the above query it is returning the result as 1 but if one of the selects does not have complete ='yes' it still returns as valid??

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.