0

我有这个从 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

我想使用表单更新中选中和未选中的复选框来更新表格

这怎么可能?

谢谢

4

3 回答 3

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);
?>
于 2013-10-21T16:03:17.860 回答
0

如果您的意思是要根据用户更新 db 上的 $property_img["imagexx"] val,请单击您必须使用 ajax 的复选框。

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

Jquery Ajax 功能可以帮助您完成这项任务。

大号

于 2013-10-21T15:31:24.310 回答
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);
?>
于 2013-10-21T15:32:02.673 回答