tb1
-----------------
id | codetb1
-----------------
1 | code1,code2
2 | code2,code3
tb2
-----------------
codetb2 | cost
-----------------
code1 | $20
code2 | $40
code3 | $60
save.php -> This is how I insert checkbox values to the database.
$codetb1 = $_POST['codetb1'];
var_dump($codetb1);
// Setting up a blank variable to be used in the coming loop.
$allStyles = "";
// For every checkbox value sent to the form.
foreach ($typeocodetb1fvisit as $style) {
// Append the string with the current array element,
// and then add a comma and a space at the end.
$allStyles .= $style . ", ";
}
// Delete the last two characters from the string.
$allStyles = subs
tr($allStyles, 0, -2);
Result.php -> this is my connection and how I can get my result
$query="SELECT * FROM tb1, tb2 WHERE
tb1.codetbl1 = tb2.codetbl2 AND
tb1.id='$id'";
Hi all — your help is greatly appreciated. I have 2 databases (table 1 and table 2). I would like to have the 2 databases linked with one another. I am having trouble accomplishing this. For example, I tried explode but I couldnt make it work.
So How can I get rid of that comma from codetb1 and find a way to match with codetb2. So finally I can get cost. Any ideas?
Normalized Schema
In my opinion, the only "real" answer to this question is to model the domain with a normalized schema. Specifically, we should never have one column storing more than exactly one piece of data. The term we use to describe your domain is that there is a many-to-many relationship, where one row in tb1 is related to many rows in tb2, and one row in tb2 can be related to many rows in tb1. The destination schema will look like this:
tb1
tb2
tb1_tb2
Then, the way you select them depends on what you want. Here are some examples:
The highest and lowest "cost" for each row in tbl1:
The sum of costs for rows in tb1:
The idea is to model each "thing" completely and distinctly; and the trick is to think of each relationship between a tb1 and a tb2 as a distinct "thing" (i.e. each one has a single row in tb1_tb2; the relationship is a full-class citizen in a normalized schema)
Inserting is fairly straightforward once you think about the relationship as a distinct entity. If you have a tb1 and a tb2 you want to relate, simply add a tow to tb1_tb2 with their ids.