How to process multiple radio buttons with MySQL?
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 what 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> • <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 sty开发者_运维百科le='font-size: 12px;'>
<a onClick=\"history.go(-1)\" class='form'>Return</a></p>
<p></span>
";
exit;
};
?>
I would like to display the id and value of the selected checkbox on the processing page before inserting it into the table.
Well, here's some things to fix with your script:
1) You do a query to check if there's anything to post, but do it by fetching all the matching rows, THEN THROWING AWAY THE RESULTS.
Even though you have a 'limit 1' on the initial query, you still force MySQL to process the entire result set with the 'order by' directive. It has to find all the matching rows first, sort them descending, THEN it can pull off only the first row.
Later on, you then perform the exact same query, but without the 'limit' clause.
There's no reason you should run two queries when one would do. In pseudo-code:
$results = mysql_query("SELECT * FROM tbl_alert ORDER BY id DESC;") or die(mysql_error());
if (mysql_num_rows() == 0) {
echo "No current alerts."
} else {
... start form here ...
while($row = mysql_fetch_array($results)) {
... generate table rows here ...
}
... finish off form here ...
}
2) In your form you generate a hidden field with name="statusid". But you're outputting this for EVERY generated row with the same name. This means when the form's submitted, only the LAST generated value will be submitted. Maybe this is just to track which row you're on, but you can infer that from the other fields since you're embedding the $id
value in their field names already, so I'm thinking this is a bug.
3) You're generating the 'checked' values on the radio buttons dynamically. This is fine, but you've got the same check/condition for both the 'no' and 'yes' buttons on the 'selstatus' radios, so if $status is 'yes', BOTH buttons check the checked attribute. As well, you're using the else clause to echo out an empty string. This just makes for slightly less readable code. Try replacing those with the following:
<input ... value="yes" <?php echo ($status == 'yes') ? 'checked' : '' ?> />
<input ... value="no" <?php echo ($status == 'no') ? 'checked' : '' ?> />
Using the tertiary operator for stuff like this makes for slightly more compact code, and is somewhat easier to read than an in-line if()
.
4) Ditto for the 'selected' class attributes you're adding to the label tags. The "else output blank string" portions just add to useless code bloat.
5) Within the while() loop that spits out the table rows, you use a multi-line echo statement with multiple "breakouts" to insert PHP variables. You should investigate the use of PHP's heredoc syntax which is specifically intended for multi-line text generation of this sort.
Now, having said all of that, to see exactly what's being submitted to the server, try replacing the entirety or your "processing page" script with the following:
<?php
var_dump($_POST);
?>
This will output the entirety of the POST data, with some added formatting and variable type/length information. If your form is generated properly, you should see a section for every table row of radio buttons. If all you get is one row's worth of data, then you'll have to debug your form, because that's where the problem is.
精彩评论