php - 修改从选择字段中选择的字段值

标签 php mysql

作为我的项目的一部分,我有两个文件:

  1. modify_brand.php(PHP 表单)
  2. update_brand.php(更新数据库的脚本)

在文件 1 中,品牌列在选择字段中,并显示在允许修改品牌名称的单独字段中。 文件2.包含更新数据库的查询。

该问题包括将所选品牌的 ID 传递给 update_brand.php 中存储的 UPDATE 查询,以便识别要更新的记录。

可能我以错误的方式处理这个问题,但如果不是,你知道如何将 ID 传递给查询吗?

modify_brand.php

<form role="form" action='../php/update_brand.php' method='post'>
<label>BRANDS LIST</label>

<select name='brands-list'>

<?php 
while ($listabrand=mysqli_fetch_array($brands)){
echo '<option value="'.$listabrand['1'].'">'.$listabrand['0'].' - '.$listabrand['1'].'</option>';
}?>
</select>

<label>BRAND'S NAME TO MODIFY</label>
<input type="text" name='brand-name'>  

<button type="submit" name='modify-btn' class="btn btn-default">Modifica</button>
</form>
.....   
<script>
$('select[name="brands-list"]').change(function(){  
var selectedBrand = $(this).val();
$('input[name="brand-name"]').val(selectedBrand);
});
</script>

update_brand.php

<?php
require '../sys/conn.php';
$mod_brand=$_POST['brand-name']

if (isset($_POST['modify-btn'])){
$brand=mysqli_real_escape_string($conn, $mod_brand] );

if ($mod_brand !=''){
    $update = mysqli_query($conn,"
    UPDATE mg_terms SET name= $brand 
    WHERE term_id=......... // THIS IS WHERE THE ID OF SELECTED BRAND SHOULD BE PLACED
    ");
     header('Location: ../pages/success.html');}
     else{header('Location: ../pages/error.html');}}
       mysqli_close($conn);
    ?>

希望我说得足够清楚。

最佳答案

所以你想要做的是将选择选项值设置为id,这样当你提交表单时,所选的id就会提交给php.ini。但你必须稍微改变你的Javascript。它应该将输入的值设置为选项文本,而不是选项值:

<form role="form" action='../php/update_brand.php' method='post'>
<label>BRANDS LIST</label>

<select name='brands-list'>

<?php 
while ($listabrand=mysqli_fetch_array($brands)){
echo '<option value="'.$listabrand['0'].'">'.$listabrand['0'].' - '.$listabrand['1'].'</option>';
}?>
</select>

<label>BRAND'S NAME TO MODIFY</label>
<input type="text" name='brand-name'>  

<button type="submit" name='modify-btn' class="btn btn-default">Modifica</button>
</form>
.....   
<script>
$('select[name="brands-list"]').change(function(){  
$('input[name="brand-name"]').val($('select[name="brands-list"] option:selected').text().split(' - ')[1]);
});
</script>

在 php 方面:

<?php
require '../sys/conn.php';
$mod_brand=$_POST['brand-name'];
$mod_id=$_POST['brands-list'];

if (isset($_POST['modify-btn'])){
$brand=mysqli_real_escape_string($conn, $mod_brand );
$brand_id=intval($mod_id );

if ($mod_brand !=''){
    $update = mysqli_query($conn,"
    UPDATE mg_terms SET name= '$brand' 
    WHERE term_id=$brand_id
    ");
     header('Location: ../pages/success.html');}
     else{header('Location: ../pages/error.html');}}
       mysqli_close($conn);
    ?>

关于php - 修改从选择字段中选择的字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45740925/

相关文章:

php - 如何生成与数据库记录匹配的唯一 URL 变量?

mysql - 了解 SQL 中的元组语法

mysql - 加入不同的表并用mysql在一行上显示匹配记录

php - 搜索数组并在找到匹配时返回所有键和值

php - 教程网站的数据库结构

php - 获取 php 中最后执行的 mysqli 查询的结果,并在另一个查询中使用结果

php - Laravel 5.3 LoginController - 标题可能不包含多个标题,检测到新行

mysql - 将表 2 中的数据插入表 1,新 ID 以表 1 的最大值开头

mysql - 在 MYSQL CONSOLE 中触发

php - 在 HTML 表格单元格内创建进度条