Jump to content

A simpler way to match data across 2 tables


swatisonee

Recommended Posts

hi,
This issue crops up only for exisiting data. For new records, i've taken care in the coding.

I have a table  A with multiple fields, one of them being Name that occurs multiple times. In another table  B I have 2 fields, id(autoinc,int 11) and Family . Table A also has a field called Family

Now John Doe and Jane Doe are both part of the Smith Family. I want to be able to list all the Does in table A and match it to the Smith entry in Table B .

My current way of doing it is to select all the Does, then on another page select Smith and then process both on a third page by doing an UPDATE Table A SET `Family` = $family WHERE `Name` = $name

I think there must be a simpler way to do this. Ideally i'd like to list out two columns , Name - one listing all the Does , and Family listing Smith, Barnaby etc. That way, I can check multiple boxes in the Name column and match it to a single checkbox on the Family column.


Any ideas how I  can proceed ?

Thanks.
Swati



Link to comment
Share on other sites

An example...

[code]<?php

if ($_POST) {
foreach ($_POST['names'] as $name) {
$query = "INSERT INTO tablec (name, family_name) VALUES ('" . mysql_real_escape_string($name) . "', '" . mysql_real_escape_string($_POST['family_name']) . "')";
mysql_query($query) or die(mysql_error());

//you may want to execute a query that will remove that name from tableA to prevent it from showing in the future

}
}

$query = "SELECT * FROM tableA";
$query = "SELECT * FROM tableB";

$names = mysql_query($query);
$families = mysql_query($query);

while ($row = mysql_fetch_array($names, MYSQL_ASSOC)) {
$n .= '
<input type="checkbox" name="names[]" value="' . $row['name'] . '"> ' . $row['name'] . '<br />';
}

while ($row = mysql_fetch_array($families, MYSQL_ASSOC)) {
$f .= '
<input type="radio" name="family" value="' . $row['family_name'] . '"> ' . $row['family_name']. '<br />';
}

echo '
<form method="post">
<table>
<tr>
<th>Names</th>
<th>Families</th>
</tr>
<tr>
<td colspan="2" style="text-align: center;">Choose the names to associate with a family:</td>
</tr>
<tr>
<td>' . $n . '</td>
<td>' . $f . '</td>
</tr>
<tr>
<td colspan="2" style="text-align: center;"><input type="submit" name="submit" value="Submit"></td>
</tr>
</table>
</form>';[/code]

Obviously you would have to have database connection code and you would have to modify the field names and table names, but you get the idea.
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.