Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have this PHP/HTML Code that is selecting data from a MySQL Database:

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

each row, has its own image with the columns image1 - image13

i want to update the table with the checkboxes that are checked and unchecked on the form update

how is this possible?

Thanks

share|improve this question
 
Sorry but your question is not clear my friend –  Sajuna Fernando Oct 21 at 15:26
 
Ok - i will update. hold on... –  charlie Oct 21 at 15:27
 
does it make sense now? –  charlie Oct 21 at 15:29
 
Ill post an answer soon. I think Bibear answer is correct –  Sajuna Fernando Oct 21 at 15:31
add comment

3 Answers

If you mean that you want to update the $property_img["imagexx"] val on db depending on the user click on the checkbox you must use ajax.

Fire an event on triggering each checkbox and send the value to a php page that update the php.

Jquery Ajax function can help you on this task.

L

share|improve this answer
 
would it be easier to use a for loop in php? –  charlie Oct 21 at 15:31
 
It more or less the same. For me the Ajax approach is lighter since it use only those items that have been modified and not all of them. –  Lelio Faieta Oct 21 at 15:37
add comment

name all your checkboxes images[]

<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" />

You can then update with 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);
?>
share|improve this answer
1  
13 queries just to update 1 row? that sounds nasty xD –  aleation Oct 21 at 15:37
 
Yeah, i have update my solution ;) –  Bibear Oct 21 at 15:39
 
works great, but its updating ALL rows with the same for example. update property_images set image1 = 'Y' where property_seq = '1' - there are multiple rows with the same property_seq, sequence is the unique column. how can i do this? –  charlie Oct 21 at 15:58
 
You are updating all rows on table at the same time, also, you are not setting imagex='N' on the unchecked ones! –  aleation Oct 21 at 16:04
 
i dont need to set imageX = 'N' at all. it can just be blank. how can i make it update where sequence = '1' and not property_seq = '1' –  charlie Oct 21 at 16:17
add comment

First of all, mysql_ functions are deprecated, please google mysqli_ or PDO, mysql_ won't be supported on future versions, and is unsafe, etc

Your html output could be much simpler with a loop, also have an eye on putting the sequence number on a hidden field or something first:

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

Then this is the next page where the update is done, have a good look at the comments:

<?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);
?>
share|improve this answer
 
Yikes. It's great that you're advocating the use of PDO, but in your code here you have $_POST data put directly in a query. That's radioactively bad code. –  tadman Oct 21 at 16:33
 
I was at work, and rushing that piece of code, If you read the commented line before that line, you will see that I'm telling him to sanitize the values of the query first xD. You people never read 100% of the things :P. I was guiding him, don´t need to do everything perfect, also I didn´t sanitize and validate there because it´s in plain mysql, it´s pain in the ass to do it, that´s one of the reasons I´m recommending PDO, I would have used prepared statements XD –  aleation Oct 21 at 19:04
 
Some people use the same excuses at work, then later when their site is completely cracked open they realize the mistake they've made. Just saying this is extremely dangerous without mysql_real_escape_string. –  tadman Oct 22 at 18:03
 
Again, I told to sanitize the query in the comments, if you can't read is not my fault, I wasn't going to do all the small details about security here because it wasn't the point at all, just warning him about it was more than enough, If not I would just had told him to switch to PDO and not trying to help with the code. The user is just learning the very basics, let him learn step by step. And thanks about concerning about my work, the 100% of the queries are PDO prepared statements, I'm using MVC so every query comes from the model, no loose plain queries ;D –  aleation Oct 23 at 7:21
 
Why do people in the PHP world insist on coming up with excuses rather than fixing the problem? It took me literally five seconds to put that escape call in there. Step 1 in learning how to use MySQL in PHP is proper escaping, it's not optional, it's not "advanced". –  tadman Oct 23 at 15:12
show 7 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.