在您发布代码之前,很难给您一个特定于问题的答案,但我认为这对您来说效果很好:
- 为所有者、品种和狗创建关系表:
Table: owners
Fields:
id_key: integer, auto number, primary key - table index
owner: varchar, 16 character, unique index - account records (unique prevents duplicate account records)
password: varchar, 32 character (assuming you are using md5 encryption, you will always get a 32 character result. Additional security concerns are beyond the scope of this question)
any other account related fields (but not dogs, give them their own table)
Table: breeds
Fields:
id_key: integer, auto number, primary key - table index
breed: varchar, 16 character, unique - available dog breeds
Table: dogs
Fields:
id_key: integer, auto number, primary key - table index
dog: varchar, 16 character, index - dog owned by account
breed: varchar, 32 character, index, foreign key= breeds.breed on delete=restrict on update=cascade (prevents duplicate breed entries during data entry)
owner: varchar, 16 character, index, foreign key= owners.owner on delete=cascade on update=cascade (this constricts the dog owner to existing accounts)
然后制作一个像这样的表格:
<?php
$formdata=array(
owners => array(),
breeds => array()
);
require_once 'dbconnection.php';
echo '<form id="dog_input" name="dog_input" method="post">';
$result=mysql_query("SELECT `owner` FROM owners;");
while ($row = mysql_fetch_row($result)) {
array_push($formdata[owners], $row[0]);
}
$result=mysql_query("SELECT `breed` FROM breeds;");
while ($row = mysql_fetch_row($result)) {
array_push($formdata[breeds], $row[0]);
}
?>
<FORM name="dog_input" method="post" action="dog_data.php">
<ul>
<li>
<label for="owner">Dog Owner</label>
<SELECT name="owner" id="owner">
<?php
foreach ($formdata[owners] as $i) {
echo '<OPTION>'.$i.'</OPTION>';
>
?>
</SELECT>
</li>
<li>
<label for="breed">Dog Breed</label>
<SELECT name="breed" id="breed">
<?php
foreach ($formdata[breeds] as $i) {
echo '<OPTION>'.$i.'</OPTION>';
>
?>
</SELECT>
</li>
<li>
<label for="dog">Dog Name</label>
<INPUT type="text" name="dog" />
</li>
<li><INPUT type="submit" value="Submit /></li>
</ul>
</FORM>
然后您需要创建验证脚本来检查您的结果并将安全数据输入数据库:
<?php
/*
* dog_data.php - checks data and inserts it into the database.
*/
require_once 'dbconnection.php';
$owner = $_POST['owner'];
$breed = $_POST['breed'];
$dog = mysql_real_escape_string($_POST['owner']);
//perform any additional data validation here, use an if statement to check validation before insert query if you do so
mysql_query("INSERT INTO `dogs` (`id_key`, `owner`, `breed`, `dog`)
VALUES(DEFAULT, '".$owner."', '".$breed."', '".$dog."');");
mysql_close($con);
header(" Location: http://www.example-redirection-page.com");
?>