如何将数据从下拉选项值保存到laravel控制器

时间:2015-05-19 16:09:24

标签: php laravel-4 mysqli eloquent laravel-5

//表单

<form method="post">
<div>
<label>Result:</label>
<div class="select">
<select name="result" id="result" class="result">
<option value="1">First Division</option>
<option value="2">Second Division</option>
<option value="3">Third Division</option>
<option value="4">Grade</option>
</select>
</div>
Marks/CGPA.
</div>
<div class="marks">
<label>Marks(%):</label>
<input name="marks" class="form-control marksText" type="text" />
</div>
<div class="cgpa">
<div class="col-md-5">
<label>CGPA:</label>
<input name="cgpa" id="cgpa" />
</div>
<div class="col-md-5">
<label>Scale:</label>
<input name="scale" id="scale" class="scale" />
</div>
</div>
<div class="save">
<button type="button">Close</button>
 <input type="submit" name="save_education" value="Save changes" />
 </div>
</form>

// controller

if(isset($_POST['save_education])){
    $qualification = Qualification::create(array
        (   
         'level_of_education' => $input['level_education'],
         'exam_title' =>  $input['exam_title'],
         'major' = $input['major'],

          if(isset($_POST([$input['result]))){
               $r = $input['result'];
               if($r == 1){
                  $result => 'First';
                  $marks = $input['marks'];
                  }
              elseif ($r == 2){
                  $result => 'Second';
                  $marks = $input['marks'];
                  }
               elseif ($r == 3){
                  $result => 'Third';
                  $marks = $input['marks'];
              }
              else {
                  if($r == 4)
                     $result = $input['cgpa'];
                     $marks = $input['scale'];
                   }
          }
         .....
        )
    );

    $qualification->save();
}

我正在使用laravel4.2,当我尝试保存结果时显示错误(如果在isset结果行中出现意外),我有什么问题。检查选项值后如何将数据保存到数据库中。作为laravel的新手帮助我..它整天都在毁了我......请帮助......

2 个答案:

答案 0 :(得分:1)

如果这是您的实时代码,那么您在第一行和第八行中缺少一些引号:

if(isset($_POST['save_education])){ //old
if(isset($_POST['save_education'])){ //new

if(isset($_POST([$input['result]))){ //old
if(isset($_POST([$input['result']))){ //new

另外,使用laravel特定函数而不是普通PHP是个好主意。将$_POST['blub']命令替换为\Input::get('blub')。您可以使用\Input::has('blub')检查var是否可用。

为了进一步的安全性,您还应该进行一些验证。一个例子是:

$validator = \Validator::make(
    \Input::all(),
    [
         'result' => 'required|between:1,4',
         'marks' => 'do-further-stuff',
         [...]
    ]
);
if($validator->fails()) {
    // error handling, go back or something
} else {
    // success, do what you want
}

对于验证规则,请查看the docs

答案 1 :(得分:0)

IMO似乎:: create数组令基础混乱。另外,我不清楚你在使用$ result和$ marks做什么 - 我假设它们是你的资格表上的列。

我建议做这样的事情:

$r = $input::get('result');

 if($r){
    $q = new Qualification;
    $q->level_of_education = Input::get('level_education');
     // do the same thing with the rest of your fields

    if($r == 1) {
       $q->result = 'First';
       $q->marks = Input::get('marks');
      }
    // rest of your elseif statements would follow 

   $q->save();
}

这个网站有一个很棒的CRUD教程(大概是1/2通过):https://scotch.io/tutorials/simple-laravel-crud-with-resource-controllers

祝你好运