作为我项目的一部分,我有两个文件:
在文件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);
?>
希望我足够清楚。
答案 0 :(得分:1)
所以你要做的是将select options值设置为id,所以当你提交表单时,所选的id会被提交给php。但是你必须稍微改变你的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);
?>