PHP在MySQL中更新多行

时间:2013-10-21 15:24:24

标签: php mysql

我有这个从MySQL数据库中选择数据的PHP / HTML代码:

<?php
    $sql3="SELECT * from property_images where property_seq = '".$property["sequence"]."' ";
    $rs3=mysql_query($sql3,$conn);
    while($property_img=mysql_fetch_array($rs3))
    {
        ?><tr>
          <td colspan="2"><img src="http://domain.co.uk/img/property-images/<?php echo $property_img["image"]; ?>" width="80px" height="80px" /></td>
          <td colspan="2"><input type="checkbox" name="image1" value="Y" <?php if($property_img["image1"] == 'Y') { echo 'checked="checked"'; } ?> />
          <input type="checkbox" name="image2" value="Y" <?php if($property_img["image2"] == 'Y') { echo 'checked="checked"'; } ?> />
          <input type="checkbox" name="image3" value="Y" <?php if($property_img["image3"] == 'Y') { echo 'checked="checked"'; } ?> />
          <input type="checkbox" name="image4" value="Y" <?php if($property_img["image4"] == 'Y') { echo 'checked="checked"'; } ?> />
          <input type="checkbox" name="image5" value="Y" <?php if($property_img["image5"] == 'Y') { echo 'checked="checked"'; } ?> />
          <input type="checkbox" name="image6" value="Y" <?php if($property_img["image6"] == 'Y') { echo 'checked="checked"'; } ?> />
          <input type="checkbox" name="image7" value="Y" <?php if($property_img["image7"] == 'Y') { echo 'checked="checked"'; } ?> />
          <input type="checkbox" name="image8" value="Y" <?php if($property_img["image8"] == 'Y') { echo 'checked="checked"'; } ?> />
          <input type="checkbox" name="image9" value="Y" <?php if($property_img["image9"] == 'Y') { echo 'checked="checked"'; } ?> />
          <input type="checkbox" name="image10" value="Y" <?php if($property_img["image10"] == 'Y') { echo 'checked="checked"'; } ?> />
          <input type="checkbox" name="image11" value="Y" <?php if($property_img["image11"] == 'Y') { echo 'checked="checked"'; } ?> />
          <input type="checkbox" name="image12" value="Y" <?php if($property_img["image12"] == 'Y') { echo 'checked="checked"'; } ?> />
          <input type="checkbox" name="image13" value="Y" <?php if($property_img["image13"] == 'Y') { echo 'checked="checked"'; } ?> /></td>
        </tr><?php
    }
    ?>

每一行都有自己的图像,其中列为 image1 - image13

我想使用在表单更新

上选中和取消选中的复选框来更新表

这怎么可能?

由于

3 个答案:

答案 0 :(得分:0)

如果您想要更新db上的$ property_img [“imagexx”] val,具体取决于用户点击复选框,则必须使用ajax。

触发每个复选框时触发事件并将值发送到更新php的php页面。

Jquery Ajax函数可以帮助您完成此任务。

答案 1 :(得分:0)

将所有复选框命名为[]

<input type="checkbox" name="images[]" value="1" />
<input type="checkbox" name="images[]" value="2" />
<input type="checkbox" name="images[]" value="3" />
<input type="checkbox" name="images[]" value="4" />

然后您可以使用PHP进行更新:

<?php
$q = "UPDATE property_images SET";
foreach($_POST["images"] as $image) {
     $q .= "  image" . $image ." = 'Y', ";
}
$q .= " property_seq = '".$property["sequence"]."' WHERE property_seq = '".$property["sequence"]."'";
mysql_query($q);
?>

答案 2 :(得分:0)

首先,不推荐使用mysql_函数,请google mysqli_或PDO,将来版本不支持mysql_,不安全等等

你的html输出可以通过循环更简单,也可以着眼于将序列号放在隐藏字段或其他东西上:

<?php
    $sql3="SELECT * from property_images where property_seq = '".$property["sequence"]."' ";
    $rs3=mysql_query($sql3,$conn);
    while($property_img=mysql_fetch_array($rs3)){
?>
        <tr>
            <td colspan="2"><img src="http://domain.co.uk/img/property-images/<?php echo $property_img["image"]; ?>" width="80px" height="80px" /></td>

            <!-- THIS IS VERY IMPORTANT: send the sequence or ID through a hidden field, to know which row you are gonna update later-->
            <input type="hidden" name="sequence" value="<?php echo $property_img['sequence']; ?>"/>

            <td colspan="2">
                <?php for($i = 1; $i <= 13; $i++): ?>
                    <input type="checkbox" name="images[]>" value="<?php echo $i; ?>" <?php if($property_img["image$i"] == 'Y') { echo 'checked="checked"'; } ?> />
                <?php endfor ?>
            </td>
        </tr>
<?php
    }
?>

然后这是更新完成的下一页,仔细查看评论:

<?php
    //Handle as you want the situation when there are no images selected instead of using an exit(), I used it here just for the quickness;
    if(count($_POST['images'] < 1) exit('no images where selected to update');

    $images = array();
    for($i = 1; $i <= 13; $i++){
        $string = "image$i = ";
        if(in_array($i, $_POST['images'])){
            $string .= "'Y'"; //notice the double quoting here, it's important
        } else {
            //This updates the table so if it was checked when loaded, but unchecked by the user, this makes the change!
            $string .= "'N'";
        }
    }

    //This creates a string like: image1 = 'Y', images2 = 'N', etc...
    $images = implode(', ', $images );

    //try to sanitize the query first ... I won't cos am just showing you your question xD
    $sql = "UPDATE property_images SET $images WHERE property_seq = " . mysql_real_escape_string($_POST[sequence]) . ";
    mysql_query($sql,$conn);
?>