3

我是新手,我正在尝试编写一个脚本,它将查询数据库并在下拉列表中返回所有用户名,然后复制所选名称,然后当您向他们注册 Dog 时,它将复制名称进入数据库,所以所有者可以有很多狗,但狗只能有 1 个所有者......我可以输入名称和品种,但不能输入下拉列表中的结果,我确实尝试让 java 脚本将所选项目拉入下拉到 $ownername 在“插入...”脚本中的位置

如果你能帮助我,你能给我发消息吗?我会链接你下载的文件

mysql_query("INSERT INTO `dog`(`id`, `dogname`, `breed`, `owner`)
 VALUES  ('', '$reg_dogname', '$breed', '$row');") or die (mysql_error());
     $userid = mysql_insert_id();

}}


$sql = mysql_query('SELECT `username` FROM  `users`');
$ownername = array();
while ($row = mysql_fetch_array($sql)){ 
    $ownername[] = $row;

 <select id="dropdown" name="dropdown" onchange="selectDropdown()">
  <?php
  foreach ($ownername as $ownername1) {
  ?> 
  <option value="<?php echo $ownername1['username']?>">
    <?php echo $ownername1['username']?>
    </option>
  <?php
  }
  ?>
  </select> 
4

1 回答 1

0

在您发布代码之前,很难给您一个特定于问题的答案,但我认为这对您来说效果很好:

- 为所有者、品种和狗创建关系表:

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");
?>
于 2012-10-20T20:48:21.693 回答