Codeigniter set_value

时间:2018-03-10 23:34:02

标签: php codeigniter

如何制作它以便在我的帖子表单上进行错误验证时,它不会仅从数据库发回默认值,但如果我要编辑值并点击提交,它会保存我试图更新的数据。我被困在我只能做其中一个的地方。



 
<? if (isset($update)) { ?>
<?php echo set_value ('fname'); ?>
<?= form_open("Phonebook/updateentry/" . $entry['id']) ?>
<?= form_fieldset("Update Entry"); ?>
<?= form_label('First Name: ', 'fname'); ?> <br>
<?= form_input(array('name' => 'fname', 'id' => 'fname', 'value' =>  $entry['fname'])); ?> <br> 

<?= form_label('Last Name: ', 'lname'); ?> <br>
<?= form_input(array('name' => 'lname', 'id' => 'lname', 'value' => $entry['lname'])); ?> <br>
<?= form_label('Phone Number: ', 'phone'); ?> <br>
<?= form_input(array('name' => 'phone', 'id' => 'phone', 'value' => $entry['phone'])); ?> <br>
<?= form_label('Email: ', 'email'); ?> <br>
<?= form_input(array('name' => 'email', 'id'=>'email', 'value' => $entry['email'])); ?> <br>
<?= form_submit('phonebooksubmit', 'Submit'); ?>
<?= form_fieldset_close(); ?>
<?= form_close() ?>
<? } ?>
&#13;
&#13;
&#13;

&#13;
&#13;
private function display()
	{
		$query = $this->db->query("SELECT * FROM phonebook ORDER BY id ASC;");
		$this->TPL['listing'] = $query->result_array();
		
		$this->load->view('phonebook_view', $this->TPL);
	}
  
  	public function updateentry($id)
	{

		$this->form_validation->set_rules('fname', 'First Name', 'required|alpha|min_length[5]|max_length[15]',
		array(
			'required' => 'You have not provided the %s. ',
			'alpha' => '%s must only compose of alphanumeric letters.',
			'max_length' => 'You exceeded max char of 20 . ',
			'min_length' => 'The min length must be 5 . '

		));
		$this->form_validation->set_rules('lname', 'Last Name', 'required|alpha|min_length[5]|max_length[20]',
		array(
			'required' => 'You have not provided %s .',
			'alpha' => '%s must only compose of alphanumeric letters.',

			'max_length' => 'You exceeded max char of 20 . ',
			'min_length' => 'The min length must be 5 . '
		));
		$this->form_validation->set_rules('phone', 'Phone Number', 'required|regex_match[/^([0-9]{3})[-. ]([0-9]{4})$/]',
		array(
			'required' => 'You have not provided %s .',
			'regex_match' => 'Please use the correct phone format.'
		));
		$this->form_validation->set_rules('email', 'Email', 'required|valid_email',
		array(
			'required' => 'You have not provided %s .',
			'valid_email' => 'Please provide valid email. '
		));
		$this->form_validation->set_error_delimiters('<p class = "errorlist">' , '</p>');
		if ($this->form_validation->run() == FALSE) {
			
			$query = $this->db->query("SELECT * FROM phonebook where id = '$id';");
			$this->TPL['entry'] = $query->result_array()[0];

			
			$this->TPL['update'] = true;
			$this->TPL['memory'] = true;
	
			$this->display();
		}
		else
		{	
		
		$fname = $this->input->post("fname");
   			$lname = $this->input->post("lname");
    			$phone = $this->input->post("phone");
    			$email = $this->input->post("email");      
    			$query = $this->db->query("UPDATE phonebook " .
                              "SET fname = '$fname'," .
                              "    lname = '$lname'," .
                              "    phone = '$phone'," .
                              "    email = '$email'" .
                              " WHERE id = '$id';");		
		$this->display();
	}
}
		
&#13;
&#13;
&#13;

我知道我必须使用set_value(),但我不确定如何制作它以便它可以同时执行这两项操作。

2 个答案:

答案 0 :(得分:0)

set_value有第二个参数,这是默认值。如果你提取记录并编辑它们,它看起来像这样:

// $data is from your database
set_value('name', $data['name'])

我使用“name”作为示例,但这可能是您记录中的任何字段。

但回到你的控制器中,你需要这样的东西:

if( $this->input->method == 'POST' )
     // Do your validation and updating to the record

// Then pull in the record for your view
$view_data['data'] = $this->db->query('... ... ...');

通过这样做,您总是会提取新数据

答案 1 :(得分:-1)

您的代码看起来非常可靠。

我建议尝试添加

<div style="white-space: nowrap">
    <form class="form-inline" editable-form name="rowForm" onaftersave="save()" ng-show="rowForm.$visible" shown="shown">
        <button type="submit" class="btn btn-primary" ng-disabled="rowForm.$waiting">Save</button>
        <button type="button" class="btn btn-link" ng-disabled="rowForm.$waiting" ng-click="shown ? remove() : rowForm.$cancel()">Cancel</button>
    </form>

    <!-- Buttons to edit and remove -->
    <div ng-hide="rowForm.$visible">
        <a href="" ng-click="rowForm.$show()" ng-hide="showDeleteButton"><span class="glyphicon glyphicon-edit" style="margin-right: 5px;"></span>Edit</a>
        <a href="" ng-click="showDeleteButton = true" ng-hide="showDeleteButton"><span class="glyphicon glyphicon-trash" style="margin-left: 10px; margin-right: 5px;"></span>Remove</a>
        <button class="btn btn-danger" ng-click="remove()" ng-show="showDeleteButton">Remove</button>
        <button class="btn btn-link" ng-click="showDeleteButton = false" ng-show="showDeleteButton">Cancel</button>
    </div>
</div>

在你的功能结束时。

然后使用最新信息更新数据库。

希望这有帮助。