使用Unique Key更新sql字段

时间:2015-04-28 13:01:52

标签: php mysql

我正在使用字段名称',但是当我使用编辑文件通过管理员更新记录时,它给出了重复输入的错误。我无法移除唯一的密钥,这是必需的..

<!--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>

任何解决方案,无需从字段中删除唯一键&#39; name&#39; ?

1 个答案:

答案 0 :(得分:0)

您的更新查询需要更正如下 -

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