【问题标题】:radio button in while loopwhile循环中的单选按钮
【发布时间】:2014-10-23 18:14:43
【问题描述】:

我正在编写一个学生出勤页面,在页面中,所有学生的姓名都是使用 while 循环从数据库中动态检索的,在 while 循环内我使用了一个单选按钮。

我的问题是单选按钮工作正常,但我无法将单选按钮的值插入数据库。这是我的代码:

include'connection.php';
@$id = mysql_real_escape_string($_GET['id']);
$res=mysql_query("SELECT * FROM `std_register` AS p1 ,`sims-register-course` AS p2 WHERE p2.semester=p1.std_semester and p2.department=p1.std_department and p2.id = '".$id."'");
if ($res==FALSE) {
echo die(mysql_error());
                }
while ($row=mysql_fetch_assoc($res)) { ?>

    <input type="hidden" id="dept_0"            name="dept[]"           value="<?php echo $row["department"] ?>">
    <input type="hidden" id="course_name_1"     name="course_name[]"    value="<?php echo $row["course_name"]  ?>">
    <input type="hidden" id="std_semester_2"    name="std_semester[]"   value="<?php echo $row["semester"]  ?>">
    <input type="hidden" id="std_id_3"          name="std_id[]"         value="<?php echo $row["id"]  ?>">
    <input type="hidden" id="std_name_4"        name="std_name[]"       value="<?php echo $row["std_name"]  ?>">
    <tr>
    <td class =info><?php echo $row["std_name"];?></td>
    <td><input type="radio"             name="<?php  echo "status['".$row["std_name"]."']" ?>"  value="1"></td>
    <td><input type="radio"             name="<?php  echo "status['".$row["std_name"]."']" ?>"  value="2"></td>
     </tr>

    <?php   }?>
    </table><br>

    <div class="form-group">
    <div class="col-sm-offset-2 col-sm-3">
    <input type="submit" name="submit" value="Assign" class="btn btn-large btn-block btn-primary"></div></div>
    </form>

    <?php 
    for ($i=0; $i < 4; $i++) {
    if(isset($_POST["submit"])) {
    $stdid = mysql_real_escape_string($_POST['std_id'][$i]);
    $dptname = mysql_real_escape_string($_POST['dept'][$i]);
    $courseName = mysql_real_escape_string($_POST['course_name'][$i]);
    $stdSemester = mysql_real_escape_string($_POST['std_semester'][$i]);
    $std_name = mysql_real_escape_string($_POST['std_name'][$i]);
    $present = mysql_real_escape_string($_POST['status'][$i]);

    $totalClasses = $present;                                            
    $insrt = mysql_query("INSERT INTO `attendence_student` (department_name,courseName,semester,present,absent,total_classes) VALUES('$dptname','$courseName','$stdSemester','$present',0,0)") or die(mysql_error());

        }

    }

是否可以将此单选按钮中的值插入数据库的两列?

【问题讨论】:

  • 您不应该使用 sql,因为它已被弃用。查看 SQLI 或 PHP PDO 的前向兼容性和安全性。你得到什么错误?它连接到数据库好吗?
  • 是的,数据库完美连接。
  • 我在这里得到的错误是:未定义的偏移量
  • 尝试把“where stdid=$stdid”放在最后。您的查询
  • 您是否在 SQL 末尾尝试了 WHERE 子句?

标签: php html mysql


【解决方案1】:

我不确定,但尝试在单选输入中添加“已检查”属性

<input type="radio" name="<?php  echo "status[".$row["std_name"]."]" ?>"  value="1" checked></td>

或者
使用 Select 标签代替单选按钮

编辑
使用此代码插入当前列或不存在的列

$value = mysql_real_escape_string($_POST['status'][$i]);
$totalClasses = $value;
if($value == 1) //check if present
{
$sql="INSERT INTO `attendence_student` (department_name,courseName,semester,present,absent,total_classes) VALUES('$dptname','$courseName','$stdSemester','$value',0,0)";
}
else  //if absent
{
$sql="INSERT INTO `attendence_student` (department_name,courseName,semester,present,absent,total_classes) VALUES('$dptname','$courseName','$stdSemester',0,'$value',0)";
}

$insrt = mysql_query($sql) or die(mysql_error());

【讨论】:

  • 好的,我会试试的。谢谢你的好主意。!我试过后会告诉你的。
  • 删除双引号后出现语法错误。
  • 对不起兄弟..我是stackoverflow的新手,我希望你删除单引号..“status[".$row["std_name"]."]"
  • 嗯.. 你能告诉我 print_r($_POST); 的输出吗
  • 感谢兄弟的好主意(选择标签)。是否可以从一个选择标签将数据插入两列?
猜你喜欢
  • 2017-04-30
  • 1970-01-01
  • 1970-01-01
  • 2019-07-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-03
  • 2014-08-15
  • 1970-01-01
相关资源
最近更新 更多