简体   繁体   中英

updating database field when a checkbox for an element is clicked

here is what i have so far :sql that gets data from the database, the data is passed through a loop and then displayed with the html code

$sql = "SELECT items_available.title, 
                items_available.item_number, 
                items_available.subtitle, 
                items_available.image_name, 
                users.username 
        FROM items_available
            INNER JOIN users ON items_available.owner_id = users.user_id
        WHERE items_available.status ='pending' 
        LIMIT $query_limit ;";

$query = mysql_query($sql);

while ($dbData = mysql_fetch_assoc($query)) {

    $item_id      = $dbData['item_number'];
    $sel_title    = $dbData ['title'];
    $sel_Image    = $dbData['image_name'];
    $sel_subtitle = $dbData['subtitle'];
    $sel_owner    = $dbData['username'];


    echo "<span style='display:inline-block;width:185px;margin:4px;'>
        <a href='#'>
            <img src='upload/$sel_Image' style='width:180px; height:160px;' />
                <h5 style='display:inline;'>$sel_title </h5><br>
                <h7 style='display:inline;'> $sel_subtitle</h7><br>
                <h6 style='display:inline;'>Posted by $sel_owner</h6> 
        </a>|
        <div style= \"display:inline-block;\"> 
            <input type=\"checkbox\" id=\"check\" name='item_ids[]' value='1' />
        </div>
    </span>";

}/

the checkbox below the block of codes should grab the ids of each element so an update to the database is possible.Hope my description is clear enough to be aided

you did nt give any specfic so it is like a guide for what u asking you need to create a form here with a hidden field

like

<div style= \"display:inline-block;\">
<form method=\"post\">
 <input type=\"hidden\" name=\"value\" =". $item_id.">
 <input type=\"checkbox\" name=\"update\" value = '1' />
</form>
</div>

php for that ll look like

if (isset($_REQUEST['value']))

{

//update after validation
}

PS $_REQUEST Deals with both for GET or POST method it actually depends what you want

You can make the VALUE of the checkbox into the id you want to get back in the $_POST['item_ids'] array.

<div style= \"display:inline-block;\"> ";

echo '<input type="checkbox" id="check" name="item_ids[]" value="' . $item_id . '" />';

echo "</div>

Now in your PHP code you can process them like this, remember checkboxes are only returned in the $_POST/$_GET array if they are actually checked.

I am assuming $_POST.

if ( isset($_POST['item_ids']) ) {
    foreach ( $_POST['item_ids'] as $item_id ) {

        // do whatever you want to with this information

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM