gpt4 book ai didi

php - 无法从下拉列表中选择结果

转载 作者:搜寻专家 更新时间:2023-10-31 22:11:02 24 4
gpt4 key购买 nike

我是新手,我正在尝试编写一个脚本,它将在其中查询数据库并返回下拉列表中的所有用户名,然后复制出选定的名称,然后当您向它们注册狗时将名称复制到数据库中,所以 Owners 可以有很多只狗,但 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>

最佳答案

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

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

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");
?>

关于php - 无法从下拉列表中选择结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12991309/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com