简体   繁体   English

将ID存储到数据库表中时会在该处抛出名称

[英]Storing id into database table throw there name

I'm working on small project for data entry. 我正在为数据输入的小型项目。 let assume a table named material fields 让我们假设一个名为material字段的表
id , name , visible , position

and other table named received_material fields 和其他名为received_material字段的表
id , material_id , quantity , supplier

and one simple html form for receiving materials, structure here 和一种用于接收材料的简单html表单,在此处进行结构化

<form method="post" action="<?php echo SERVER['PHP_SELF'] ?>">
    <label for="date">date</label>
    <input type="text" name="date" />

    <label for="name">Material Name</label>
    <input type="text" id="name" name="name" />

    <label for="quantity">Weight</label>
    <input type="text" name="quantity" />

    <label for="supplier">Supplier</label>    
    <input type="text" name="supplier" />

    <input type="submil" value="submit"/>
</form>

<script>
    $('#name').autocomplete({source : 'supplier_chk.php'});
</script>

the supplier_check.php Script here... provider_check.php脚本在这里...

<?php include 'includes/session.php'; ?>
<?php include 'includes/functions.php'; ?>

<?php if(!logged_in()) {
    redirect_to('login.php');
}
?>
<?php if ( logged_in_role('administrator') || logged_in_role('store') ) { ?>
<?php echo msg(); ?>

<?php 

$search = escape_string($_GET['term']);
$sql = "SELECT id, supplier_name FROM `supplier`
        WHERE `supplier_name` LIKE '%$search%'";
$result = mysqli_query($connection,$sql);
confirm_query($sql);

$json = [];
    while ( $row = mysqli_fetch_assoc($result) )
    {
        $json[] = $row['supplier_name'];
    }
    echo json_encode($json);

}

?>

I'm using JQuery UI For Name Autocomplete. 我正在使用“名称自动完成”的JQuery UI。 fetching material name throw ajax method. 获取材料名称时抛出ajax方法。 it'll easily autocomplete material name but i've to store material id instead material name.. please help how to do this action using jquery/javascript or php. 它会轻松自动完成材料名称,但我必须存储材料ID而不是材料名称..请帮助如何使用jquery / javascript或php执行此操作。

add hidden field before name input give id to get element.. 在名称输入给id以获取元素之前添加隐藏字段。

here html example 这里的HTML示例

<form method="post" action="<?php echo SERVER['PHP_SELF'] ?>">
    <label for="date">date</label>
    <input type="text" name="date" />

    <label for="name">Material Name</label>
    <input type="hidden" id="id" name="id" />
    <input type="text" id="name" name="name" />

    <label for="quantity">Weight</label>
    <input type="text" name="quantity" />

    <label for="supplier">Supplier</label>    
    <input type="text" name="supplier" />

    <input type="submil" value="submit"/>
</form>


<script>
$('#name').autocomplete({
    minLength: 0,
    source : 'material_chk.php',
    focus: function( event, ui ) {
    $( "#name" ).val( ui.item.label );
    return false;
    },
    select: function( event, ui ) {
    $( "#name" ).val( ui.item.label );
    $( "#id" ).val( ui.item.id );

    return false;
    }
});
</script>

in material_chk.php 在material_chk.php中

<?php 
$search = escape_string($_GET['term']);
$size = isset($_GET['size'])? escape_string($_GET['size']): null;
$sql = "SELECT * FROM `materials`
        WHERE `name` LIKE '%$search%'";
$result = mysqli_query($connection,$sql);
confirm_query($sql);
$json = [];
    while ( $row = mysqli_fetch_assoc($result) )
    {
        $json[] = array("id"=>$row['id']),"label"=>$row['item_name']);
    }
    echo json_encode($json);
}

I'm note sure what you're asking, but I do have a tip. 我注意到您在问什么,但我确实有个建议。 This is a general tip, not just restricted to suppliers, but I will use them as an example. 这是一个一般性提示,不仅限于供应商,我将以它们为例。

I would use a better solution: a dropdown list with all the supplier names. 我会使用更好的解决方案:一个包含所有供应商名称的下拉列表。 The value of each list item is the supplier ID. 每个清单项目的值是供应商ID。 So you directly submit the supplier ID. 因此,您直接提交供应商ID。

<select id="suppliers" name="supplierSelector">
  <option value="1">Philips</option>
  <option value="2">Sony</option>
  <option value="3">Samsung</option>
  <option value="4">Akai</option>
</select>

You could additionally put a search text input below the dropdown list. 您还可以在下拉列表下方放置搜索文本输入。 Anything typed into that search box will restrict the dropdown list. 在该搜索框中键入的任何内容都会限制下拉列表。 This can be done in Javascript. 这可以用Javascript完成。

But basically my solution is to let the user select the correct supplier ID using their names. 但基本上,我的解决方案是让用户使用其名称选择正确的供应商ID。 in PHP that would be: 在PHP中将是:

$sql    = 'SELECT id, supplier_name FROM supplier';
$result = mysqli_query($connection,$sql);

echo '<select id="suppliers" name="supplierSelector">'.PHP_EOL;

while ($supplier = mysqli_fetch_assoc($result))
{ 
  extract($supplier);
  echo '<option value="'.$id.'">'.$supplier_name.'</option>'.PHP_EOL;
}

echo '</select>'.PHP_EOL;

The added advantage of this method is that the form cannot be submitted without a valid supplier. 这种方法的另一个优点是,没有有效的供应商就无法提交表单。 In general you should try to restrict the input to what you know is allowed. 通常,您应该尝试将输入限制为您所允许的范围。

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

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