gpt4 book ai didi

php - 无法使用codeigniter在Mysql中插入数据

转载 作者:搜寻专家 更新时间:2023-10-30 23:41:07 24 4
gpt4 key购买 nike

由于数据库错误 1048,我无法将数据插入 mysql。我已经搜索了将近 1 周,但找不到任何解决方案。请帮我。这是所有的东西......

Controller users.php:

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Users extends CI_Controller
{
function __construct()
{
parent::__construct();
#$this->load->helper('url');
$this->load->model('users_model');
}

public function index()
{
$data['user_list'] = $this->users_model->get_all_users();
$this->load->view('show_users', $data);
}

public function add_form()
{
$this->load->view('insert');
}

public function insert_new_user()
{
$udata['Username'] = $this->input->post('name');
$udata['Email-Id'] = $this->input->post('email');
$udata['Address'] = $this->input->post('address');
$udata['Mobile'] = $this->input->post('mobile');
$res = $this->users_model->insert_users_to_db($udata);

if($res)
{
header("location: http://localhost/crudcode/index.php/users_model/insert_users_to_db");
}
else
{
echo "Hello";
}
}

}

模型 users_model.php:

<?php

class Users_model extends CI_Model
{
function __construct()
{
parent::__construct();
$this->load->database();
}

public function get_all_users()
{
$query = $this->db->get('users');
return $query->result();
}

public function insert_users_to_db($udata)
{
return $this->db->insert('users', $udata);
}

public function getById($id)
{
$query = $this->db->get_where('users',array('id'=>$id));
return $query->row_array();
}

}

?>

查看insert.php:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CI Insert Form</title>
</head>
<body>
<form method="post" action="localhost/crudcode/index.php/users/insert_new_user">
<table width="400" border="0" cellpadding="5">

<tr>
<th width="213" align="right" scope="row">Enter your username</th>
<td width="161"><input type="text" name="name" size="20" /></td>
</tr>

<tr>
<th align="right" scope="row">Enter your email</th>
<td><input type="text" name="email" size="20" /></td>
</tr>

<tr>
<th align="right" scope="row">Enter your Mobile</th>
<td><input type="text" name="mobile" size="20" /></td>
</tr>

<tr>
<th align="right" scope="row">Enter Your Address</th>
<td><textarea name="address" rows="5" cols="20"></textarea></td>
</tr>

<tr>
<th align="right" scope="row">&nbsp;</th>
<td><input type="submit" name="submit" value="Send" /></td>
</tr>

</table>
</form>
</body>
</html>

Here is the error

最佳答案

您是否检查过表单中的数据是否已提交?喜欢

public function insert_new_user()
{
print_r($this->input->post());
}

并检查您传递给用户模型的数组以及最后一个查询。

public function insert_users_to_db($udata)
{
$this->db->insert('users', $udata);
print_r($udata);
echo $this->db->last_query();
}

关于php - 无法使用codeigniter在Mysql中插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35082319/

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