简体   繁体   English

如何在动态下拉列表中将值存储在数据库中?

[英]How do I store value in my database in a dynamic dropdown list?

This is a dynamic dropdown in PHP/mySQL. 这是PHP / mySQL中的动态下拉列表。 I want to store the name in the database server but the tag outputs the integer value. 我想将名称存储在数据库服务器中,但是标签输出整数值。

If i change the code from <option value="<?php echo $row["id"]; ?>"> to <option value="<?php echo $row["name"]; ?>"> It shows my_sqli_fetch_array expects parameter 1 error. 如果我将代码从<option value="<?php echo $row["id"]; ?>">更改为<option value="<?php echo $row["name"]; ?>">显示my_sqli_fetch_array预期参数1错误。

My objective being to store the corresponding $row["name"] that is being displayed on the dropdown instead of $row["id"]. 我的目标是存储在下拉列表中显示的相应$ row [“ name”]而不是$ row [“ id”]。

<?php

$link = mysqli_connect("localhost","root", "");
mysqli_select_db($link,"loginsystem");

?>

<form name="form1" action="" method="post">
<table>
<tr>
<td>Select Assembly Line</td>
<td><select id ="assemblylinedd" onChange="change_assemblyline()">
<option>Select</option>

<?php
$i=1;
$res=mysqli_query($link, "SELECT * FROM assemblyline");
$count=mysqli_num_rows($res);
if ($count >0){
while($row=mysqli_fetch_array($res))
{
?>
<option value="<?php echo $row["id"]; ?>"><?php echo $row["name"]; ?></option>
}

<?php $i++;} }else{
echo "No record Found !";
} ?>

</select></td>
</tr>

Scripting code : 脚本代码:

<script type="text/javascript">
function change_assemblyline()
{
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open("GET","ajax.php?assemblyline="+document.getElementById("assemblylinedd").value, false);
    xmlhttp.send(null);
    alert(xmlhttp.responseText);
    document.getElementById("devices").innerHTML=xmlhttp.responseText;

}

This is my ajax.php 这是我的ajax.php

$link = mysqli_connect("localhost","root", "");
mysqli_select_db($link,"loginsystem");

$assemblyline = isset($_GET['assemblyline']) ? $_GET['assemblyline'] : '';
$devices = isset($_GET['devices']) ? $_GET['devices'] : '';


if($assemblyline!="")
{   

    $res=mysqli_query($link, "SELECT * FROM devices WHERE devices_id=$assemblyline");
    echo "<select id='devicesdd' onchange='change_devices()'>";
    while($row=mysqli_fetch_array($res))

    {

    echo "<option value='$row[id]'>";echo $row["name"]; echo "</option>";

    }
    echo "</select>";

}

Please do ignore onchange_devices() as it follows the same for next consecutive dropdown. 请不要忽略onchange_devices(),因为在下一个连续的下拉列表中,它也一样。

Though, its your requirement to save device name in DB, it is advised to save numeric id. 虽然,这是您在DB中保存设备名称的要求,但建议保存数字ID。

Reason: Name may change, but, id will persist. 原因:名称可能会更改,但是ID将保留。

If say your device id:name is 99 : iPhone 6 and you save in DB: iPhone 6 , later the name gets changed to iPhone6 . 如果说您的设备id:名称为99 : iPhone 6并保存在DB: iPhone 6 ,则名称将更改为iPhone6

In this scenario if you search records with name iPhone6 , clearly, your above record will not show. 在这种情况下,如果您搜索名称为iPhone6记录,显然,上面的记录将不会显示。

If you save numeric id, it will show irrespective of name change. 如果保存数字ID,则无论名称更改如何,它都会显示。

Coming back to your question: 回到您的问题:

I cannot write code here. 我不能在这里写代码。 But a pseudo code logic will help (hope so): 但是伪代码逻辑会有所帮助(希望如此):

  1. Take a hidden field device_name . 采取隐藏字段device_name

  2. On change of drop down, with jQuery , assign value to hidden field. 更改下拉列表时,使用jQuery将值分配给隐藏字段。

  3. $("#assemblylinedd option:selected").text();

  4. Now, after submit, you will get device_name in hidden field. 现在,提交后,您将在隐藏字段中获得device_name

  5. $devices = isset($_GET['device_name']) ? $_GET['device_name'] : '';

Save this to DB. 将此保存到数据库。

$link = mysqli_connect("localhost","root", "");
mysqli_select_db($link, "loginsystem");

$assemblyline = isset($_GET['assemblyline']) ? $_GET['assemblyline'] : '';
$devices = isset($_GET['devices']) ? $_GET['devices'] : '';


if(!empty(trim($assemblyline)))
{   
    $res = mysqli_query($link, "SELECT * FROM devices WHERE devices_id = '$assemblyline'");

    echo "<select id='devicesdd' onchange='change_devices()'>";

    while($row = mysqli_fetch_array($res))
    {

        echo "<option value='" . $row["id"] . "'>" . $row["name"] . "</option>";

    }

    echo "</select>";
}
  • I've added a proper empty check instead of your != "" , which didn't previously prevent a single space from being passed. 我添加了一个正确的空支票,而不是您的!= "" ,该支票以前并未阻止传递单个空格。
  • I've quoted your query value, I would definitely use prepared statements instead of passing values directly. 我已经引用了您的查询值,我肯定会使用准备好的语句,而不是直接传递值。
  • I've quoted your $row[id] . 我已经引用了您的$row[id]
  • I've concatenated your string correctly. 我已经正确连接了您的字符串。

Note: It would be preferable to return a JSON array object with the IDs and the names instead of outputting HTML via the AJAX, it would make your code-base much cleaner and adaptable in the future. 注意:最好返回一个带有ID和名称的JSON数组对象,而不是通过AJAX输出HTML,这将使您的代码库在将来更加整洁和适应。

Reading Material 阅读材料

empty

trim

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM