conn.php代码<?php @mysql_connect("localhost:3306","root","")or die("mysql连接失败"); @mysql_select_db("php100")or die("db连接失败"); mysql_query("set names 'gbk'");?>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 回答
拉风的咖菲猫
TA贡献1995条经验 获得超2个赞
把你的new表删除,然后运行下面的sql语句
CREATE TABLE IF NOT EXISTS `news` ( `id` int (11) NOT NULL , `title` varchar (255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL , `dates` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP , `contents` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ALTER TABLE `news` ADD PRIMARY KEY (`id`); ALTER TABLE `news` MODIFY `id` int (11) NOT NULL AUTO_INCREMENT; |
运行后,数据效果图如下:
然后,conn.php代码
<?php $Conn = mysql_connect( 'localhost' , 'root' , '' ) or die ( '数据库服务器连接失败:' .mysql_error() ); mysql_query( 'set names utf8' ); mysql_select_db( 'php100' ) or die ( '数据库连接失败' . mysql_error()); |
add.php
<?php if ( ! empty ( $_POST [ 'sub' ] ) ) { $title = $_POST [ 'title' ]; $con = $_POST [ 'con' ]; echo msyql_query( "insert into new (title, contents) values ('{$title}', '{$con}')" ) ? '插入成功' : '插入失败:' .mysql_error(); } ?> //下面的表单自己写 |
- 1 回答
- 0 关注
- 559 浏览
添加回答
举报
0/150
提交
取消