Php 如何使用MySQL处理多个单选按钮?
我正在尝试用MySQL处理多个收音机。第一步是我在流程页面上回显结果。每次我选择任何单选选项时,它只显示第一行结果。这是我尝试提交表单后看到的:Php 如何使用MySQL处理多个单选按钮?,php,mysql,Php,Mysql,我正在尝试用MySQL处理多个收音机。第一步是我在流程页面上回显结果。每次我选择任何单选选项时,它只显示第一行结果。这是我尝试提交表单后看到的: <div style="padding: 15px;"> <span class="loginfail" style="font-size:24px; font-weight: bold">Notifications</span><p> <?php include("progress_inser
<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>
通知
多谢各位。通知已成功更新
状态ID:14非C2:1
返回
这是表单的代码:
<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>
通知
添加
好吧,这里有一些东西要用你的脚本修复:
1) 您可以执行查询以检查是否有任何内容要发布,但可以通过获取所有匹配的行,然后丢弃结果来执行此操作
即使您对初始查询有一个“limit 1”,您仍然会强制MySQL使用“order by”指令处理整个结果集。它必须首先找到所有匹配的行,按降序排序,然后只能提取第一行
稍后,您将执行完全相同的查询,但没有'limit'子句
当一个查询可以运行时,没有理由运行两个查询。在伪代码中:
$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) 在表单中,生成一个名为“statusid”的隐藏字段。但您将为每个生成的同名行输出该值。这意味着在提交表单时,只提交最后生成的值。也许这只是为了跟踪您所在的行,但是您可以从其他字段推断,因为您已经在字段名中嵌入了$id
值,所以我认为这是一个错误
3) 您正在单选按钮上动态生成“选中”值。这很好,但是您对“selstatus”收音机上的“no”和“yes”按钮具有相同的检查/条件,因此如果$status为“yes”,则两个按钮都会检查checked属性。另外,您正在使用else子句来回显一个空字符串。这只会导致可读性稍差的代码。尝试用以下内容替换这些内容:
<input ... value="yes" <?php echo ($status == 'yes') ? 'checked' : '' ?> />
<input ... value="no" <?php echo ($status == 'no') ? 'checked' : '' ?> />
<?php
var_dump($_POST);
?>
这将输出整个POST数据,并添加一些格式和变量类型/长度信息。如果表单生成正确,您应该看到表中每行单选按钮的一个部分。如果你得到的只是一行数据,那么你就必须调试你的表单,因为这就是问题所在。嗨,你能把你的帖子缩短一点,只给我们真正相关的部分吗?这需要通读很多内容。为什么要使用单选按钮来选择是/否?我试图集成以下是/否:请正确对齐代码。另外,给我们一节生成的XHTML/HTML。事实上,您正在使用XHTML或HTML吗?-1。请用适当的标题回答你的问题。