下面的编码可能有逻辑错误,我无法识别,因为我的逻辑不太好。
// wrap IDisposable into using
using (OracleCommand cmd = new OracleCommand()) {
cmd.Connection = koneksidb.con;
// Make SQL readable
cmd.CommandText =
@"SELECT Notes
FROM Keterangan";
// wrap IDisposable into using; do not close manually - dr.Close()
using (OracleDataReader dr = cmd.ExecuteReader()) {
// do not append string in loop - use SringBuilder
StringBuilder sb = new StringBuilder();
while (dr.Read()) {
if (sb.Length > 0) // <- do not put delimiter before the very first item
sb.Append(", "); // <- do you want a delimiter? Say, comma?
// ".ToString()" provides debug info, use Convert.ToString() instead
sb.Append(Convert.ToString(dr.GetValue(0)));
}
// Assign data once in order to prevent re-painting (and blinking)
textBox1.Text = sb.ToString();
}
}
流程是,当这个控制器从POST(复选框和一些texfields)接收时,它将转到foreach并检查复选框== 1(表示是否已勾选)它是否会插入数据库。如果没有,仍然插入数据库但值为0。
7个复选框中的一个我没有打勾。因此,在数据库6中,attendance_check将保持值1和1 attendance_check保持值为0.
我得到了什么?都保持价值1。
错误是什么?
感谢。
- 更新22/3/2016 -
表单视图(名为index.php)
public function actionSave()
{
//initiate connection
$request = Yii::$app->request;
if (Yii::$app->request->post())
{
//receive from POST, insert into $student
$student=Yii::$app->request->post('sudent');
//receive from POST, insert into $selection
$selection=Yii::$app->request->post('selection');
//looping foreach and set $student as $key
foreach ($student as $key => $value)
{
if($key==0 )
{
$check=$selection[$key];
}
elseif($key)
{
$check=empty($selection[$key])?'':$selection[$key];
}
else{
//don't know what to do here
}
$model=new Attendance();
if($check || $check==0)
{
$model->student_id=$value;
$model->attendance_check=1;
$model->attendance_date=time();
// $model->attendance_reason=$value;
$model->save();
}
else
{
$model->student_id=$value;
$model->attendance_check=0;
$model->attendance_date=time();
// $model->attendance_reason=$value;
$model->save();
}
}
}
else
{
//don't know what to do here
}
}
&#13;
- 2016年3月23日更新 -
答案 0 :(得分:0)
Select已经被数据提供者的ID索引,因此不需要sudent数组。
以下情况应该有效。
public function actionSave()
{
//initiate connection
$request = Yii::$app->request;
if (Yii::$app->request->post())
{
//receive from POST, insert into $selection
$selection=Yii::$app->request->post('selection');
//looping foreach and set $student as key
foreach ($selection as $value => $check)
{
$model=new Attendance();
$model->student_id=$value;
$model->attendance_check=$check ? 1 : 0;
$model->attendance_date=time();
// $model->attendance_reason=$value;
$model->save();
}
}
else
{
//don't know what to do here
}
}
还需要考虑的另一件事是您正在创建新的出勤记录,但可能已经记录了响应,您想先加载。
$model = Attendance::findOne(['student_id'=>$value]);
if(!$model) $model = new Attendance();