hi guys

really i don't know what im doing wrong... i am trying to process multiple radios with mysql. the first step has involved me echoing the results on the process page. each time i select any radio option it simply displays the first row result. this is that i see after trying to submit the form:

Notifications

Thank you. The notifications have been updated successfully.

statusid: 14 notc2: 1

Return

this is the code for the form:

<div style="padding: 15px;">

<span class="loginfail" style="font-size:24px; font-weight: bold">Notifications</span><p>

<?php include("progress_insertcomment.php"); ?>

 <?php 

// Make a MySQL Connection
mysql_select_db("speedycm_data") or die(mysql_error());

$query_comment = "select * from tbl_alert order by id desc limit 1";
$comment = mysql_query($query_comment, $speedycms) or die(mysql_error());
$row_comment = mysql_fetch_assoc($comment);
$totalRows_comment = mysql_num_rows($comment);

?>

<!--- add notification --->

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  <span id="sprytextarea1">

<textarea id='comment' name="comment" style="height: 75px; width:330px;"><?php echo $row_comment['comment']; ?></textarea> 
</span>
<p>
<button type="submit">Add</button>
               <input type="hidden" name="notc" value="1"/>
               </form>

               <!--- notification history --->

               <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

               <table border="0" cellspacing="2" cellpadding="2">
                  <?php

if ( $row_comment == 0 ) {

        echo "<span style='font-size: 11px;'>No current alerts.</span>";

    } else {

// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM tbl_alert ORDER BY id DESC") 
or die(mysql_error());  

while($rows=mysql_fetch_array($result)){ ?>
  <tr>
    <td>
     <?php
echo "<div class='bubble'><div class='pimped'>
        <blockquote>" . $rows['comment'] . "
        </blockquote></div>
        <cite><strong>" . $rows['user'] . "</strong> @ " . $rows['date'] . "</cite>
        <span style='font-size: 10px;'>
        <p>
    <a href='editalert.php?id=". $rows['id'] ."' class='form' >Edit</a>&nbsp;•&nbsp;<a href='deletealert.php?id=". $rows['id'] ."' class='form'>Delete</a>
    </span>
    </div>
    "; 
    ?> 

    </td>
    <td valign="top" align="center"><div style="padding-left: 30px;"><span style="font-size: 10px;">Completed?</span>
    <p class="field switch">

    <!--- determine status of notification --->

    <?php 
    $status = $rows['status'];
    $id = $rows['id'];
    ?>

    <input type="radio" name="selstatus[<?php echo $id; ?>]" value="no" <?php if($status == 'yes') {echo 'checked';} else {echo '';} ?>/>
    <input type="radio" name="selstatus[<?php echo $id; ?>]" value="yes" <?php if($status == 'yes') {echo 'checked';} else {echo '';} ?>/>
    <input type="hidden" name="statusid" value="<?php echo $id; ?>"/>
    <label for="radio1" class="cb-enable <?php if($status == 'yes') {echo 'selected';} else {echo '';} ?>"><span>Yes</span></label>
    <label for="radio2" class="cb-disable <?php if($status == 'no') {echo 'selected';} else {echo '';} ?>"><span>No</span></label>

    </p>
    </div></td>
  </tr>
  <tr>
    <td></td>
      <?php
    }
    }
    ?>
    <td align="center"><div style="padding-left: 30px;">
<button type="submit">Update</button>
        <input type="hidden" name="notc2" value="1"/>
    </div></td>
  </tr>
</table>
</form>
</div>

this is the code for the processing page:

<?php   

        // 6) update notifications

        if (array_key_exists('notc2',$_POST)) {

        $update = $_POST['selstatus']; 

echo "<p style='font-size: 12px;'>Thank you. The notifications have been updated successfully.<p>";

foreach($_POST as $key => $value){
echo $key . ': ' . $value . '<br>';
}

    echo "<p><span style='font-size: 12px;'>
                            <a onClick=\"history.go(-1)\" class='form'>Return</a></p>
                            <p></span>
                ";

            exit;

                };  
                ?>

how do i resolve this problem? please any help would be appreciated. i would like to display the id and value of the selected checkbox on the processing page before inserting it into the table. thanking you in advance.

GOD BLESS AND HELLO EVERYONE

Dani AI

Generated

A few concrete points to close the loop on this thread (references: original form and reply).

The radios are already being emitted as an array (names like selstatus[ID]) so the processing code must iterate that array directly. A single hidden field named statusid inside the loop will only submit one value (the last input with that name). Echoing entries from $_POST without walking the selstatus array will not show the ID=>value pairs (array values print as the word "Array"). The simplest, correct server-side pattern is to loop $_POST['selstatus'] and use the array key as the row id.

Example processing (display id + value, then update safely with PDO):

// assume $pdo is a configured PDO instance
if (!empty($_POST['selstatus']) && is_array($_POST['selstatus'])) {
    foreach ($_POST['selstatus'] as $id => $status) {
        $id = (int) $id;
        $status = ($status === 'yes') ? 'yes' : 'no';
        echo "statusid: $id — value: $status<br>";

        $stmt = $pdo->prepare('UPDATE tbl_alert SET status = :status WHERE id = :id');
        $stmt->execute([':status' => $status, ':id' => $id]);
    }
}

Front-end checklist: make both radios share the same name="selstatus[ID]" so only one value is posted for that ID; set the checked attribute based on the actual status (one radio should check for yes, the other for no); give each input a unique id so labels with for= work properly. For debugging, use print_r($_POST) or var_export($_POST, true) rather than echoing raw values.

Note: the old mysql_* extension is removed in modern PHP. Migration to PDO or mysqli is strongly recommended; see PHP prepared statements for safe updates (PDO prepared statements).

In second code line number 11 foreach loop is wrong:

$selstatus = $_POST['selstatus'];
foreach($selstatus as $key => $value)
{
    echo $key . ': ' . $value . '<br>';
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.