I need a form to tie MySQL Records together
I need a HTML-based form to tie database records together. The user scenario that I envision is somthing like this:
A super-user does a search of the database and is delivered a table of records based on that search; each record is numbered with the database-record id.
There would also be two text fields next to each record. These text fields would allow:
- the user to make a reference from one record to another. For example, a user could enter into record id #457 the integer 242 to indicate that there is a correlation with record ID #242.
- The user would also describe the type of relationship it is. This could be accomplished with a simple integer in the second field that indicates the type of relationship between the two records.
When the super-user hits the submit button, all of these relationships are saved in a mySqljoin table.
One option is to give me advice on how to implement and code this myself in PHP. However, before I reinvent the wheel, another option is to lead me to a free script that does something sim开发者_高级运维ilar.
If you're just looking for a pseudo-code framework, I would do it something like this for the body of the form:
<form id="my_form" action="" method="post">
<table id="my_table"><tr><th>Result ID</th><th>Related ID</th><th>Relation Type</th></tr>
<?php
$result = mysql_query("SELECT blah blah");
while ($row = [your chosen method of fetching results]) {
$resource_id = $row['id'];
echo "<tr><td>".$resource_id."</td>";
echo "<td><input type=\"text\" name=\"related[".$resource_id."]\" /></td>";
echo "<td><input type=\"text\" name=\"type[".$resource_id."]\" /></td></tr>";
}
?>
</table>
<input type="submit" value="Submit Related Resources" />
</form>
And then when the user submits the form, process it with another loop that goes through each value in the related and type arrays and submits a mysql_query("INSERT blah blah"). For example:
$related_resources = $_POST['related'];
$related_types = $_POST['type'];
foreach ($related_resources as $original_resource -> $related_resource) {
$type = $related_types[$original_resource];
mysql_query("INSERT INTO [table name] ('resource', 'related', 'type') VALUES ('".$original_resource."', '".$related_resource."', '".$type."'");
}
Of course you should also add an "if in_array" etc. before getting the type to make sure the user entered one and store a default if not, or check the form for completeness before processing it if you want to require them to fill in both fields. You should also run your input through a safety function (escaping quotes etc.) before letting any users input data into your tables.
This sounds way too specific for you to be able to easily repurpose a script from somewhere else. In addition, the user/superuser combination makes it more complicated to implement.
If access control is not an issue you could use phpMyAdmin or something similar. Otherwise you'll need a custom implementation. It's tricky to advice you on how to do that based on your question though. More details would help.
精彩评论