无法按ID过滤更新查询

时间:2015-08-06 09:15:08

标签: php mysql mysqli

我有一个网站后端部分的代码,可以快速链接'并允许在面板中编辑它们。当我没有添加' WHERE'它更新了所有快速链接的SQL子句,但是当我按ID过滤时,它只是不起作用。



<?php

    $query = "SELECT id,title,picture,link,alt,button,active FROM quicklinks" or die("Error!:" . mysqli_error($link)); 
    $result = $link->query($query); 
    $title = $_REQUEST["title"];
    $picture = $_REQUEST["picture"];
    $id = $_REQUEST["id"];
?>
<?php 


    while($row = mysqli_fetch_array($result)) 
    { 
        if (isset($_REQUEST['update']))     
        {
        mysqli_query($link,"UPDATE quicklinks SET title='$title', picture='$picture' WHERE ID='$id' ");
        }
  echo "<form method='post'><div id='ql" . $row["id"] . "' class='modal fade' role='dialog'> <div class='modal-dialog'><div class='modal-content'><div class='modal-header'><button type='button' class='close' data-dismiss='modal'>&times;</button><h4 class='modal-title'>Editing " . $row["title"] . "</h4></div><div class='modal-body'> 
<div class='input-group'>
            <span class='input-group-addon' id='basic-addon1'>Quick Link ID is: </span>
  <input type='text' name='id' class='form-control' value='" . $row["id"] . "' disabled='disabled'></div></br>
<div class='input-group'>
<span class='input-group-addon' id='basic-addon1'>Title: </span>
<input name='title' type='text' id='inputHelpBlock' class='form-control' value='" . $row["title"] . "'> </div></br>
<div class='input-group'>
<span class='input-group-addon' id='basic-addon1'>Link: </span>
<input name='link' type='text' id='inputHelpBlock' class='form-control' value='" . $row["link"] . "'> </div></br>
<div class='input-group'>
<span class='input-group-addon' id='basic-addon1'>Image URL: </span>
<input name='picture' type='text' id='inputHelpBlock' class='form-control' value='" . $row["picture"] . "'> </div></br>
<div class='input-group'>
<span class='input-group-addon' id='basic-addon1'>Image Alt Text: </span>
<input name='alt' type='text' id='inputHelpBlock' class='form-control' value='" . $row["alt"] . "'> </div></br>
<div class='input-group'>
<span class='input-group-addon' id='basic-addon1'>Button Text: </span>
<input name='button' type='text' id='inputHelpBlock' class='form-control' value='" . $row["button"] . "'> </div></br></div>
<div class='modal-footer'><button type='button' class='btn btn-default' data-dismiss='modal'>Close</button><input name='update' type='submit' class='btn btn-success' value='Apply'></div></div></div></div></form>"; 
} ?>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

如果id是数据库中的小写字母,那么您应该将其写在查询中。如果它是整数,也删除引号。

mysqli_query($link,"UPDATE quicklinks SET title='$title', picture='$picture' WHERE id=$id ");

修改

我发现你的代码实际上搞砸了。这是修改后的代码

<?php

    $query = "SELECT id,title,picture,link,alt,button,active FROM quicklinks" or die("Error!:" . mysqli_error($link)); 
    $result = $link->query($query); 
    $title = $_REQUEST["title"];
    $picture = $_REQUEST["picture"];
    $id = $_REQUEST["id"];

    if ($_GET['do'] == "update") {
        mysqli_query($link,"UPDATE quicklinks SET title='$title', picture='$picture' WHERE id=$id ");
    }
?>
<?php 


    while($row = mysqli_fetch_array($result)) 
    { 

  echo "<form method='post' action='?do=update' ><div id='ql" . $row["id"] . "' class='modal fade' role='dialog'> <div class='modal-dialog'><div class='modal-content'><div class='modal-header'><button type='button' class='close' data-dismiss='modal'>&times;</button><h4 class='modal-title'>Editing " . $row["title"] . "</h4></div><div class='modal-body'> 
<div class='input-group'>
            <span class='input-group-addon' id='basic-addon1'>Quick Link ID is: </span>
  <input type='text' name='id' class='form-control' value='" . $row["id"] . "' disabled='disabled'></div></br>
<div class='input-group'>
<span class='input-group-addon' id='basic-addon1'>Title: </span>
<input name='title' type='text' id='inputHelpBlock' class='form-control' value='" . $row["title"] . "'> </div></br>
<div class='input-group'>
<span class='input-group-addon' id='basic-addon1'>Link: </span>
<input name='link' type='text' id='inputHelpBlock' class='form-control' value='" . $row["link"] . "'> </div></br>
<div class='input-group'>
<span class='input-group-addon' id='basic-addon1'>Image URL: </span>
<input name='picture' type='text' id='inputHelpBlock' class='form-control' value='" . $row["picture"] . "'> </div></br>
<div class='input-group'>
<span class='input-group-addon' id='basic-addon1'>Image Alt Text: </span>
<input name='alt' type='text' id='inputHelpBlock' class='form-control' value='" . $row["alt"] . "'> </div></br>
<div class='input-group'>
<span class='input-group-addon' id='basic-addon1'>Button Text: </span>
<input name='button' type='text' id='inputHelpBlock' class='form-control' value='" . $row["button"] . "'> </div></br></div>
<div class='modal-footer'><button type='button' class='btn btn-default' data-dismiss='modal'>Close</button><input name='update' type='submit' class='btn btn-success' value='Apply'></div></div></div></div></form>"; 
}
?>