douza9835 2015-04-28 13:01
浏览 85
已采纳

使用Unique Key更新sql字段

I am using Unique Key for the field 'name', but when I update the record using edit file through admin, it gives me an error for duplicate entry. I can't remove unique key, it's required..

<!--update process-->
<?php
if(isset($_POST['submit']) and !empty($_POST['token'])){
print_r($_POST);
$name = $_POST['name'];
$size = $_POST['size'];
$linkt = $_POST['link'];
$seeds = $_POST['seeds'];
$leechs = $_POST['leechs'];
$active = $_POST['active'];

$query = "UPDATE tplus_torrentlist SET name = '$name', size = '$size', link = '$linkt', seeds = '$seeds', leechs = '$leechs', active = '$active'";

$result = mysqli_query($link, $query) or die(mysqli_error($link));

$error = mysqli_error($link);
if(empty($error)){
    $_SESSION['flash'] = '<blockquote style="background: green; color: #fff">One record updated</blockquote>';
    header('location:dashbord.php');
}
else{
    $_SESSION['flash'] = '<blockquote style="background: green; color: #fff">Sorry cant updated this record</blockquote>';
    header('location:dashbord.php');
}
}
?>
<!--fetch values from database according to id-->

<?php
$sql = "SELECT * FROM tplus_torrentlist WHERE id = $id limit 1";
$result =  mysqli_query($link ,$sql) or die(mysqli_error($link));
$row = $result->fetch_array();
?>

<div class="container">
<div class="row clearfix">

    <div class="col-md-8 col-md-offset-2">
        <?php if(!empty($response)){echo $response;} ?>
        <h3>Edit this torrent</h3>
        <hr/>
        <form role="form" class="form" action="edit.php?id=<?php echo $_GET['id']?>" method="POST">

            <label>Name</label>
            <input type="text" name="name" value="<?php echo $row['name']?>" class="form-control input-sm">

            <label>Size</label>
            <input type="text" name="size" value="<?php echo $row['size']?>" class="form-control input-sm">

            <label>Link</label>
            <input type="text" name="link" value="<?php echo $row['link']?>" class="form-control input-sm">

            <label>Seeds</label>
            <input type="text" name="seeds" value="<?php echo $row['seeds']?>" class="form-control input-sm">

            <label>Leechs</label>
            <input type="text" name="leechs" value="<?php echo $row['leechs']?>" class="form-control input-sm">

            <label>Active</label>
                <select name="active" class="form-control input-sm">
                <?php if($row['active'] ==0){?>
                <option value="0">Active</option>
                <option value="1">InActive</option>
                    <?php }else{ ?>
                <option value="1">InActive</option>
                <option value="0">Active</option>
                        <?php } ?> 
            </select>

            <br/>
            <input type="hidden" name="token" value="<?php echo rand(100, 100000)?>">
            <input type="submit" name="submit" value="update torrent" class="btn btn-info">
        </form>
    </div>

</div>
</div>

Any solution without removing unique key from the field 'name' ?

  • 写回答

1条回答 默认 最新

  • dpge74512 2015-04-28 13:12
    关注

    Your update query need to correct as following -

    $query = "UPDATE tplus_torrentlist SET name = '$name', size = '$size', link = '$linkt', seeds = '$seeds', leechs = '$leechs', active = '$active' WHERE id = $id limit 1";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥15 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)