【问题标题】:Posting multiple radio button values to MySQL using "foreach"使用“foreach”将多个单选按钮值发布到 MySQL
【发布时间】:2010-06-09 02:05:33
【问题描述】:

我已经稍微调整了我的代码,但我仍然难以将其发布到表格中。有人可以给我一个foreach数组的例子吗?

表单页面

  <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;&#8226;&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']; ?>

    <input type="radio" name="selstatus" value="no" <?php if($status == 'yes') {echo 'checked';} else {echo '';} ?>/>
    <input type="radio" name="selstatus" value="yes" <?php if($status == 'yes') {echo 'checked';} else {echo '';} ?>/>
    <input type="hidden" name="statusid" value="<?php echo $rows['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>
</body>

处理

           <?php   

        // 6) update notifications

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

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

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

            exit;

                };  
                ?>

在做了一些研究后,我了解到将多个单选值插入 MySQL 表的唯一方法是使用数组。在这个站点的某个地方提出了一个类似的问题:PHP multiple radio buttons,建议使用 foreach 循环。

使用 foreach 循环

<?php
foreach ( $_POST as $key => $val )
    echo "$key -> $val\n";
?>

$key 将是所选选项的名称,$val 是值。

这将如何适用于我的情况? - 我正在努力在互联网上寻找任何帮助。我知道我会在处理页面上使用它来从上一页中提取任何单选值,然后在进程中循环 INSERT MySQL 代码,直到全部完成。

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    据我所知,这些是多行。要让 PHP 解析 POST 的方式会大大节省时间,我建议您阅读以下内容:http://nl2.php.net/manual/en/language.variables.external.php

    如果您的表tbl_alert 有某种id,您可以使用向单选按钮添加名称,如下所示:

     <input type="radio" name="selstatus[<?phpecho $row['id'];?>]" ....
    

    ... 这将在 $_POST['selstatus'] 中为您提供一个不错的可循环数组,其中警报的 id 作为键,单选按钮的值作为值。试试print_rvar_dump $_POST 看看有什么不同。对于每个条目,您可以根据需要运行 UPDATE 或 INSERT 查询。

    【讨论】:

      猜你喜欢
      • 2014-03-12
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 2013-03-29
      • 2015-04-27
      • 1970-01-01
      • 2012-11-30
      • 2021-10-31
      相关资源
      最近更新 更多