Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP在MySQL中更新多行_Php_Mysql - Fatal编程技术网

PHP在MySQL中更新多行

PHP在MySQL中更新多行,php,mysql,Php,Mysql,我有以下PHP/HTML代码,用于从MySQL数据库中选择数据: <?php $sql3="SELECT * from property_images where property_seq = '".$property["sequence"]."' "; $rs3=mysql_query($sql3,$conn); while($property_img=mysql_fetch_array($rs3)) { ?><tr>

我有以下PHP/HTML代码,用于从MySQL数据库中选择数据:

<?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
    }
    ?>

如果您的意思是要根据用户的不同更新数据库上的$property_img[“imagexx”]val,请单击必须使用ajax的复选框

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

jqueryajax函数可以帮助您完成这项任务


L

将所有复选框命名为图像[]

<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);
?>

首先,mysql_uu函数已被弃用,请使用google mysqli_uu或PDO,mysql_uu在未来版本中将不受支持,并且不安全,等等

通过循环,您的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
    }
?>
“/>

对不起,你的问题不清楚,我的朋友好了-我会更新的。等一下…现在有意义吗?我很快会给出答案。我想Bibear的答案是正确的。在php中使用for循环会更容易吗?大致相同。对我来说,Ajax方法更轻,因为它只使用那些已经修改的项,而不是所有的项。13st更新1行?这听起来不错,但它会用相同的行更新所有行。例如,更新属性\u images set image1='Y'其中属性\u seq='1'-有多行具有相同的属性\u seq,序列是唯一的列。我如何才能做到这一点?您正在同时更新表上的所有行,而且,您没有设置我不需要设置imagex='N',它可以是空的。我怎样才能让它在sequence='1'而不是property_seq='1'的地方更新呢?你提倡使用PDO真是太好了,但是在你的代码中,你有
$\u POST
数据直接放在查询中。这是放射性的坏代码。我当时在w如果你读了这行之前的注释行,你会看到我告诉他先清理查询的值xD。你们从来没有读过100%的东西:P。我在指导他,不需要把每件事都做得完美,我也没有清理和验证,因为它是在普通的mysql中,它是pa我推荐PDO的原因之一是,我会使用事先准备好的声明,有些人在工作中使用同样的借口,然后当他们的网站被完全打开时,他们意识到他们犯了错误。如果没有mysql\u real\u escape\u string
,仅仅说这是非常危险的。我再次告诉他们为了清理注释中的查询,如果你不能阅读不是我的错,我不会在这里做所有关于安全性的小细节,因为这根本不是重点,只是警告他这已经足够了,如果不是的话,我会告诉他切换到PDO,而不是试图帮助代码。用户只是在学习最基本的s、 让他一步一步地学习。感谢大家对我工作的关注,100%的查询都是PDO准备的语句,我使用MVC,所以每个查询都来自模型,没有松散的简单查询;为什么PHP世界的人们坚持提出借口而不是解决问题?我花了五秒钟才把esca放出来学习如何在PHP中使用MySQL的第一步是正确的转义,它不是可选的,不是“高级的”。
<?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);
?>