gpt4 book ai didi

php - 如何用表单数据更新数据库

转载 作者:行者123 更新时间:2023-11-29 00:40:29 24 4
gpt4 key购买 nike

我是 Code Igniter 的新手,我正在尝试更新表单中的用户数据。插入、显示和删除数据工作正常,但我无法使更新功能工作。

数据保存在两个表中:usersusers_info

我的模型:

function getuser($id) {
$this->db->join('users_info','users.user_id = users_info.user');

$query = $this->db->get_where('users', array('user_id'=> $id));

return $query->result_array();
}

function edituser($id) {

$update_user = array(
'username' => $this->input->post('username'),
'password' => md5($this->input->post('password')),
'email' => $this->input->post('email'),
'level' => $this->input->post('role')
);

$this->db->where('user_id', $id);
$this->db->join('users_info','users.user_id = users_info.user' );
$this->db->update('users', $update_user);

$affected_rows1 = $this->db->affected_rows() ? TRUE : FALSE;

$update_info = array(
'name' => $this->input->post('first_name'),
'surname' => $this->input->post('surname'),
'address' => $this->input->post('address'),
'city' => $this->input->post('city'),
'phone' => $this->input->post('phone'),
'mobile' => $this->input->post('mobile'),
'am' => $this->input->post('am'),
'vat' => $this->input->post('vat')
);

$this->db->where('user', $id);
$this->db->update('users_info', $update_info);
$affected_rows2 = $this->db->affected_rows() ? TRUE : FALSE;

return ($affected_rows1 && $affected_rows2) ? TRUE : FALSE;

}

我的 Controller :

function edituser() {
if ($this->session->userdata('is_logged_in')) {
$this->load->helper('form');
$this->load->helper('html');
$this->load->model('users_model');
$id=$this->uri->segment(3);
$this->users_model->edituser($id);

if ((int)$id > 0) {

$query = $this->users_model->getuser($id);

$data['fid']['value'] = $id;
$data['fusername']['value'] = $query['username'];
$data['fpassword']['value'] = $query['password'];
$data['femail']['value'] = $query['email'];
$data['frole']['value'] = $query['role'];
$data['ffirst_name']['value'] = $query['first_name'];
$data['fsurname']['value'] = $query['surname'];
$data['faddress']['value'] = $query['address'];
$data['fcity']['value'] = $query['city'];
$data['fphone']['value'] = $query['phone'];
$data['fmobile']['value'] = $query['mobile'];
$data['fam']['value'] = $query['am'];
$data['fvat']['value'] = $query['vat'];
}

$data['main_content'] = 'pages/edit_user';
$this->load->view('templates/template', $data);

} else {
redirect('login/index');
}
}

我的看法

<div class="form-grid">

<?php
$attributes= array('class' => 'leftLabel', 'id' => 'form1');
echo form_open('users/edituser', $attributes);
?>
<? echo form_hidden('id',$fid['value']); ?>

<ul>
<li>
<label class="fldTitle">Name<abbr title="Required Field" class="require">*</abbr></label>

<div class ="fieldwrap">
<span class="fldcol left">
<input name="ffirst_name" type="text" tabindex="13" value="<?php echo set_value('ffirst_name',$ffirst_name); ?>"class="full">
<label class="fldLabel">First Name</label>
<?php echo form_error('first_name'); ?>
</span><span class="fldcol right">
<input name="fsurname" type="text" tabindex="14" value="<?php echo set_value('fsurname'); ?>" class="full">
<label class="fldLabel">Surname</label>
<?php echo form_error('surname'); ?>
</span>
</div>
</li>
<li>
<label class="fldTitle">Login info<abbr title="Required Field" class="require">*</abbr></label>
<div class ="fieldwrap">

<span class="fldcol left">
<input name="username" type="text" tabindex="13" class="full">
<label class="fldLabel">Username</label>
<?php echo form_error('username'); ?>
</span>

<span class="fldcol right">

<select name="role" tabindex="12" id="combobox" value="<?php echo set_value('role'); ?>">
<option value="first"<?php echo set_select('role', 'first', TRUE); ?>>First</option>
<option value="second"<?php echo set_select('role', 'second'); ?>>Second</option>
<option value="third"<?php echo set_select('role', 'third'); ?>>Third</option>
</select>

<label class="fldLabel">Choose user role</label>

<?php echo form_error('role'); ?>

</span><span class="fldcol left">

<input name="password" type="password" tabindex="13" class="full">
<label class="fldLabel">Password</label>
<?php echo form_error('password'); ?>

</span>

<span class="fldcol right">
<input name="password2" type="password" tabindex="13" class="full">
<label class="fldLabel">Comfirm Password</label>
<?php echo form_error('password2'); ?>
</span>
</div>
</li>
<li class="cmplxFld error">
<label class="fldTitle">User info<abbr title="Required Field" class="require">*</abbr></label>
<div class ="fieldwrap">
<span class="full">
<input name="address" type="text" tabindex="15" value="<?php echo set_value('address'); ?>" class="full">
<label class="fldLabel">Street Address</label>
<?php echo form_error('address'); ?>
</span>
<span class="fldcol left">
<input name="city" type="text" tabindex="17" value="<?php echo set_value('city'); ?>" class="full">
<label class="fldLabel">City</label>
<?php echo form_error('city'); ?>
</span>
<span class="fldcol right">
<input name="email" type="text" tabindex="18" value="<?php echo set_value('email'); ?>" class="full">
<label class="fldLabel">email</label>
<?php echo form_error('email'); ?>
</span>
</span>
<span class="fldcol left">
<input name="phone" type="text" tabindex="17" value="<?php echo set_value('phone'); ?>" class="full">
<label class="fldLabel">Phone</label>
<?php echo form_error('phone'); ?>
</span>
<span class="fldcol right">
<input name="mobile" type="text" tabindex="18" value="<?php echo set_value('mobile'); ?>" class="full">
<label class="fldLabel">Mobile</label>
<?php echo form_error('mobile'); ?>
</span>
</span>
<span class="fldcol left">
<input name="am" type="text" tabindex="17" value="<?php echo set_value('am'); ?>" class="full">
<label class="fldLabel">AM</label>
<?php echo form_error('am'); ?>
</span>
<span class="fldcol right">
<input name="vat" type="text" tabindex="19" value="<?php echo set_value('vat'); ?>" class="full">
<label class="fldLabel">VAT</label>
<?php echo form_error('vat'); ?>
</span>
</div>
</li>
<li class="buttons bottom-round noboder">
<div class ="fieldwrap">
<input name="submit" type="submit" value="Submit" class="submit-button">
</div>
</li>
</ul>

</form>
</div>

有人能告诉我我的代码有什么问题吗?

最佳答案

你需要做的是传递第三个参数

$this->db->update('users', $update_user);

所以它看起来像这样

$this->db->update('users', $update_user,array('field_name'=>$id));

第三个参数采用字符串数组作为查询的 where 部分。它生成此查询:

UPDATE users SET (...) WHERE field_name = content_of_$id_variable

编辑: 我重新阅读了您的问题并注意到在您的第一次更新中您添加了一个连接子句。此代码应该有效。

型号:

function edituser($id) {

$this->db->trans_start();

$update_user = array(
'username' => $this->input->post('username'),
'password' => md5($this->input->post('password')),
'email' => $this->input->post('email'),
'level' => $this->input->post('role')
);

$this->db->update('users', $update_user,array('user_id'=>$id));

$update_info = array(
'name' => $this->input->post('first_name'),
'surname' => $this->input->post('surname'),
'address' => $this->input->post('address'),
'city' => $this->input->post('city'),
'phone' => $this->input->post('phone'),
'mobile' => $this->input->post('mobile'),
'am' => $this->input->post('am'),
'vat' => $this->input->post('vat')
);

$this->db->update('users_info', $update_info,array('user',$id));

$status = $this->db->trans_status();

$this->db->trans_complete();

return $status;
}

告诉我进展如何。

关于php - 如何用表单数据更新数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12483518/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com