【问题标题】:Codeigniter set_valueCodeigniter 设置值
【发布时间】:2018-08-19 06:50:59
【问题描述】:

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

 
<? 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() ?>
<? } ?>

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();
	}
}
		

我知道我必须使用 set_value(),但我不知道如何使用它才能做到两者兼得。

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

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

    // $data is from your database
    set_value('name', $data['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('... ... ...');
    

    通过这样做,您始终可以获取最新数据

    【讨论】:

      【解决方案2】:

      您的代码看起来很可靠。

      我建议尝试添加一个

      $this->db->update(.......)
      

      在函数结束时。

      然后数据库将更新为最新的信息。

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 2011-09-01
        • 1970-01-01
        • 2011-04-06
        • 2011-10-21
        • 2013-05-26
        • 2020-01-18
        • 1970-01-01
        • 2014-05-12
        • 1970-01-01
        相关资源
        最近更新 更多