开发者

PHP/MySQL Update checkbox selection to database 2

开发者 https://www.devze.com 2023-02-11 09:20 出处:网络
Following on from a previous post, I\'m trying to update a database field with a 1 or 0, depending on whether a checkbox is checked or 开发者_如何学Cnot.

Following on from a previous post, I'm trying to update a database field with a 1 or 0, depending on whether a checkbox is checked or 开发者_如何学Cnot.

It seems to work when the checkbox is checked but not when empty.

Any ideas how to solve this? Code below. Many Thanks, S.

Here's the code within my form:

$query  = "SELECT * FROM istable WHERE assocProp = '".$_POST['id']."'";
    $result = mysql_query($query);              
    while($row = mysql_fetch_array($result, MYSQL_ASSOC))
        {                                           
        echo '<li>                          
                <label for="changePP'.$row['id'].'"><input id="changePP'.$row['id'].'" type="checkbox" name="showPP_ids[]" value="'.$row['id'].'"'.($row['showPP']=='1' ? ' checked="checked"':NULL).' /> Display</label>
                </li>'. PHP_EOL;                                        
        } 

And the php process code (updated):

$newQuery=mysql_query("select * from istable where assocProp = '".$_POST['id']."'");
    $newResult=mysql_fetch_array($newQuery);    

    foreach ($newResult as $showPP_ids) {
      $val = (int) isset($_POST['showPP_ids']); 
        mysql_query("UPDATE istable SET showPP = $val WHERE id = ".mysql_real_escape_string($showPP_ids));
    }


$val = (int) isset($_POST['showPP_ids']);  

Is an Array. It should be

foreach ($newResult as $showPP_ids) {  
$val = (int) isset($_POST['showPP_ids'][$showPP_ids]);  

I guess


I don't think the code can work this way for you... the thing is that if you don't tick a checkbox on page, its value is not being submitted with the form data and therefore you are unable to set showPP to 0 in your database

I have 2 possible solutions in mind:

if all records other than the ones ticked on page should be set to 0, you can do it this way:

$values = mysql_real_escape_string(implode(',', $showPP_ids));
mysql_query("UPDATE istable SET showPP = 1 WHERE id IN('$values')");
mysql_query("UPDATE istable SET showPP = 0 WHERE id NOT IN('$values')");

another solution would be to store all the IDs in hidden field(s) on HTML page with that form and check which of them you're getting in $showPP_ids array - those will be set to 1 - and which of them are missing from that variable - those will be set to 0

$query  = "SELECT * FROM istable WHERE assocProp = '".$_POST['id']."'";
$result = mysql_query($query);              
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    echo '<li>                          
            <label for="changePP'.$row['id'].'">
                <input id="changePP'.$row['id'].'" type="checkbox" name="showPP_ids[]" value="'.$row['id'].'"'.($row['showPP']=='1' ? ' checked="checked"':NULL).' /> Display
            </label>
            <input type="hidden" name="original_ids[]" value="'.$row['id'].'" />
          </li>'. PHP_EOL;
}

... and in the PHP file (no MySQL SELECT is neccessary here at all):

foreach ($_POST['original_ids'] as $id) {
    if (isset($_POST['showPP_ids']) && in_array($id, $_POST['showPP_ids'])) {
        $val = 1;
    } else {
        $val = 0;
    }
    mysql_query("UPDATE istable SET showPP = $val WHERE id = ".mysql_real_escape_string($id));
}


When a checkBox is not checked, the value isn't submitted to the server.Pls

Please use isset($_POST['id']) to check if the value exists.

http://php.net/manual/en/function.isset.php


You are testing whether the array $_POST['showPP_ids'] is set, which it always will be.

I think your test should be:

$val = (int) isset($_POST['showPP_ids'][$showPP_ids]);
0

精彩评论

暂无评论...
验证码 换一张
取 消