我用连接数据库成功了,php程序运行也是对的,但是为什么数据没有导入到数据库中?
conn.php<?php$conn=mysql_connect("localhost","root","")ordie("mysql连接失败");mysql_selec...
conn.php
<?php
$conn=mysql_connect("localhost","root","")or die("mysql连接失败");
mysql_select_db("php",$conn)or die("db连接失败");
mysql_query("set names 'utf8'");
?>
add.php
<?php
include("conn.php"); //引入连接数据库
if(!empty($_POST['sub'])){
$title=$_POST['title'];
$con=$_POST['con'];
$sql="insert into 'news'('id','title','dates','contents') values(null,'$title',now(),'$con')";
mysql_query($sql);
echo "插入成功";
}
?>
<form action = "add.php" method = "post">
标题<input type = "text" name = "title"><br>
内容<textarea rows="5" cols="50" name="con"></textarea><br>
<input type="submit" name="sub" value="发表" />
</form> 展开
<?php
$conn=mysql_connect("localhost","root","")or die("mysql连接失败");
mysql_select_db("php",$conn)or die("db连接失败");
mysql_query("set names 'utf8'");
?>
add.php
<?php
include("conn.php"); //引入连接数据库
if(!empty($_POST['sub'])){
$title=$_POST['title'];
$con=$_POST['con'];
$sql="insert into 'news'('id','title','dates','contents') values(null,'$title',now(),'$con')";
mysql_query($sql);
echo "插入成功";
}
?>
<form action = "add.php" method = "post">
标题<input type = "text" name = "title"><br>
内容<textarea rows="5" cols="50" name="con"></textarea><br>
<input type="submit" name="sub" value="发表" />
</form> 展开
1个回答
展开全部
$sql="insert into 'news'('id','title','dates','contents') values(null,'$title',now(),'$con')";
改成
$time= time();
$sql="insert into news('title','dates','contents') values('$title','$time','$con')";
追答
改成这样
<?php
include_once("conn.php"); //引入连接数据库
if($_POST['dopost']=='ok'){
$title=$_POST['title'];
$con=$_POST['con'];
$time= time();
$sql = "insert into news (title,dates,contents) values ('".$title."','".$time."','".$con."')";
mysql_query($sql);
}
?>
<form action = "add.php" method = "post">
<input type="hidden" name="dopost" value="ok">
标题<input type = "text" name = "title"><br>
内容<textarea rows="5" cols="50" name="con"></textarea><br>
<input type="submit" name="sub" value="发表" />
</form>
数据库字段 dates 类型为int 长度11
conn.php 可以改成
<?php
$host="localhost";
$db_user="root";
$db_pass="";
$db_name="php";
$timezone="Asia/Shanghai";
$link=mysql_connect($host,$db_user,$db_pass);
mysql_select_db($db_name,$link);
mysql_query("SET names UTF8");
header("Content-Type: text/html; charset=utf-8");
date_default_timezone_set($timezone); //北京时间
?>
用你自己的也可以的
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询