PHP如何插入数据库
<?php$ostype=$_POST['ostype']; $uuid=$_POST['uuid'];$nowtime=time();$username='XXXX';$userpass='XXXX';$dbhost='localhost';$dbdatabase='XXX';//生成一个连接$db_connect=mysql_connect($dbhost,$username,$userpass) or die("Unable to connect to the MySQL!");$ret_json;if(!$db_connect) { $ret_json=array('code'=>1001, 'message'=>'链接数据库失败');} else { mysql_select_db($dbdatabase,$db_connect); $result = mysql_query("INSERT INTO t_dblocal_userinformation (ID, OSTYPE, UUID, LASTDATE) VALUES (NULL, $ostype, $uuid, $nowtime)"); if ($result) { $ret_json=array('code'=>1000, 'message'=>'插入数据库成功'); } else { $ret_json=array('code'=>1002, 'message'=>'插入数据库失败'); }}$jobj=new stdclass();foreach($ret_json as $key=>$value){$jobj->$key=$value;}echo ''.json_encode($jobj);?>
为嘛插入数据库失败呢??
ID是自增的主键,LASTDATE是DATE类型
回复讨论(解决方案)
报错提示什么?
如果LASTDATE是DATE类型 $nowtime=date(‘Y-m-d’);
如果LASTDATE是DATETIME类型$nowtime=date(‘Y-m-d H:i:s');
echo mysql_error(); 报什么错误没有
少了引号了。
$result = mysql_query("INSERT INTO t_dblocal_userinformation (ID, OSTYPE, UUID, LASTDATE) VALUES (NULL, '$ostype ', '$uuid ', '$nowtime ')");
少了引号了。
$result = mysql_query("INSERT INTO t_dblocal_userinformation (ID, OSTYPE, UUID, LASTDATE) VALUES (NULL, '$ostype ', '$uuid ', '$nowtime ')");
把你拼写的sql 放到数据库的(orcale 放到plsqldev sqlserver 发到企业管理器) 执行下 看看能不能插入成功
第一个 想楼上那样说的 引号的问题
第二个 你的time()是返回的时间戳,和date类型对应不上吧 要转化处理一下才行
报错提示什么?
如果LASTDATE是DATE类型 $nowtime=date(‘Y-m-d’);
如果LASTDATE是DATETIME类型$nowtime=date(‘Y-m-d H:i:s');
哇,版主
echo mysql_error(); 报什么错误没有
报错:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''t_dblocal_userinformation'('ID', 'UUID', 'OSTYPE', 'LASTDATE') VALUES (NULL, 'E' at line 1
我知道了..原来PHP 的表名和字段名要用 ` 的.....我用的都是 ' ....见笑见笑...多谢各位..过来穿了个门...

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.
