thinkphp 用add方法添加数据到数据库不成功
我用add方法提交数据之后,数据库总是插不进去我要插入的数据。
html
<code> <form action="__APP__/Room/add" method="post"> 房间编号<input type="text" name="room_id"><br> 房间类型<input type="text" name="room_type"><br> 房间状态<input type="text" name="room_status"><br> 房间面积<input type="text" name="room_area"><br> 房间价格<input type="text" name="room_cost"><br> 描述<input type="text" name="room_description"><br> <input type="submit" name="add" value="添加"> <input type="submit" name="reset" value="取消"> </form></code>
post方法提交的数据可以在下面方法中获取到,就是add不进去
add
<code> public function add(){ header("Content-Type:text/html; charset=utf-8"); if(isset($_POST["add"])){ $room=M('room'); $roominfo['room_id']=$_POST["room_id"]; $roominfo['room_type']=$_POST["room_type"]; $roominfo['room_status']=$_POST["room_status"]; $roominfo['room_area']=$_POST["room_area"]; $roominfo['room_cost']=$_POST["room_cost"]; $roominfo['room_description']=$_POST["room_description"]; $room->add($roominfo); echo "<script>alert('添加成功')</script>"; // $this->display("add"); //这里在里面写数据是因为没有展示该方法名称为主的页面 } } </code>
请问我是哪里写错了吗
?
回复内容:
我用add方法提交数据之后,数据库总是插不进去我要插入的数据。
html
<code> <form action="__APP__/Room/add" method="post"> 房间编号<input type="text" name="room_id"><br> 房间类型<input type="text" name="room_type"><br> 房间状态<input type="text" name="room_status"><br> 房间面积<input type="text" name="room_area"><br> 房间价格<input type="text" name="room_cost"><br> 描述<input type="text" name="room_description"><br> <input type="submit" name="add" value="添加"> <input type="submit" name="reset" value="取消"> </form></code>
post方法提交的数据可以在下面方法中获取到,就是add不进去
add
<code> public function add(){ header("Content-Type:text/html; charset=utf-8"); if(isset($_POST["add"])){ $room=M('room'); $roominfo['room_id']=$_POST["room_id"]; $roominfo['room_type']=$_POST["room_type"]; $roominfo['room_status']=$_POST["room_status"]; $roominfo['room_area']=$_POST["room_area"]; $roominfo['room_cost']=$_POST["room_cost"]; $roominfo['room_description']=$_POST["room_description"]; $room->add($roominfo); echo "<script>alert('添加成功')</script>"; // $this->display("add"); //这里在里面写数据是因为没有展示该方法名称为主的页面 } } </code>
请问我是哪里写错了吗
?
你打印出生成的sql语句或者入库的错误信息就知道了
检测是否开启了数据库字段缓存:
将Runtime/Data/_fields/下面的字段缓存删除,或者直接删除整个Runtime文件
tp有自带的add()函数,不知道会不会和它冲突了?
还有如果开启调试模式,如果是数据库的问题是有提示的
var_dump下$_POST,看看有没有正确接收数据,在add方法里echo 一个字符串,看看有没有进入该方法,然后dump下$room,看看模型有木有实例化成功,在配置文件开启页面调试show_page_trace => true,查看生成的sql语句
先用var_dump看看$_POST是否传递成功,在用var_dump看看$model-〉add($room_info)返回数据库表主键没有,没有就是添加失败,在用getlastsql()查看上一条数据库语句。检查下字段名有没有写错了

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
