douxun1407 2015-03-26 17:44
浏览 34
已采纳

更新和删除特定记录

I have written code to retrieve all the images from database for a specific city, and I want to be able to delete a specific image or to change the caption.

The problem is:

the code always work on the last image only! I hope you guys will be able to help me with this problem.

Retrieve code:

<?php
$City_name=$_REQUEST['id'];
$Image_query = "SELECT * FROM image where City_name ='".$City_name."' ";
$Image_result = mysqli_query($dbcon,$Image_query); 

echo "<table>";
while ($row = mysqli_fetch_array($Image_result))
{
    $image_id = $row['Image_id'];
    $image = $row['Image_url'];
    $Caption = $row['Caption']; 

    echo "<tr style='float:right;'>";
    echo "<td>"; ?> <img  src="<?php echo $image ; ?>"/>  <br> 
        <input type="text" name="caption" value="<?php echo $Caption ;?>" /> 
        <br> <input name="delete" type="submit" value="Delete picture" />
        <br> <input name="Update_caption" type="submit" value="change caption" />
    <?php echo "</td>"; 
    echo "<td>"; ?> <input class="input-image" type="hidden" name="id" value="<?php echo $image_id ;?>" /> 
    <?php echo "</td>"; 
} /* End of while loop */
echo "</tr>"; 
echo"</table>";     
?>

Update code :

if (isset($_POST['Update_caption'])) {
    $ImageID = $_POST['id'];
    $ImageCaption = $_POST['caption'];
    $sql = mysqli_query ($dbcon,"UPDATE `image` SET `Caption`='".$ImageCaption."' WHERE `Image_id`='".$ImageID."' ");
    if ($sql) {
        echo "done";
    } else { echo "error"; } 
}

Delete code :

if (isset($_POST['delete'])) {
    $ImageID = $_POST['id'];
    $sql = mysqli_query ($dbcon,"DELETE FROM `image` where `Image_id` = '".$ImageID."' ");
    if ($sql) {
        echo "done";
    } else { echo "error"; } 
}
  • 写回答

1条回答 默认 最新

  • dongrao1862 2015-03-26 17:47
    关注

    $_POST['id'] (sql injection alert!) contains the contents of the input element that has a name attribute id.

    You are echoing out your input elements in a loop, all with the same name so the last one will overwrite all the previous ones.

    You should use an array like for example:

    <input class="input-image" type="hidden" name="id[<?php echo $image_id ;?>]" value="<?php echo $image_id ;?>" />
    

    So that your $_POST['id'] is an array containing all elements.

    The same applies to other input elements like the caption.

    An alternative, especially for your delete option, would be to wrap every image in its own form. But keep in mind that you will need valid html for that to work, you can't have a form that opens in a row element and spans different columns.

    And note that you should really use a prepared statement to close the sql injection hole you have now.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。