我是新手,我正在尝试编写一个脚本,它将查询数据库并返回下拉列表中的所有用户名,然后复制出所选名称,然后当您向他们注册一个狗时将名称复制到DB中, 所以业主可以拥有很多狗,但狗只能拥有1只狗... 我可以输入名称和品种,但不能输入下拉列表中的结果, 我确实尝试让java脚本将下拉列表中的所选项目拉到'insert into ...'脚本中$ 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>
答案 0 :(得分: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");
?>