为什么插入数据,但是mysql依然为空?

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>



慕勒3428872
浏览 560回答 1
1回答

拉风的咖菲猫

把你的new表删除,然后运行下面的sql语句CREATE&nbsp;TABLE&nbsp;IF&nbsp;NOT&nbsp;EXISTS&nbsp;`news`&nbsp;(&nbsp;&nbsp;`id`&nbsp;int(11)&nbsp;NOT&nbsp;NULL,&nbsp;&nbsp;`title`&nbsp;varchar(255)&nbsp;CHARACTER&nbsp;SET&nbsp;utf8&nbsp;COLLATE&nbsp;utf8_unicode_ci&nbsp;NOT&nbsp;NULL,&nbsp;&nbsp;`dates`&nbsp;timestamp&nbsp;NOT&nbsp;NULL&nbsp;DEFAULT&nbsp;CURRENT_TIMESTAMP,&nbsp;&nbsp;`contents`&nbsp;text&nbsp;CHARACTER&nbsp;SET&nbsp;utf8&nbsp;COLLATE&nbsp;utf8_unicode_ci&nbsp;NOT&nbsp;NULL)&nbsp;ENGINE=InnoDB&nbsp;DEFAULT&nbsp;CHARSET=utf8;&nbsp;ALTER&nbsp;TABLE&nbsp;`news`&nbsp;&nbsp;ADD&nbsp;PRIMARY&nbsp;KEY&nbsp;(`id`);&nbsp;ALTER&nbsp;TABLE&nbsp;`news`&nbsp;&nbsp;MODIFY&nbsp;`id`&nbsp;int(11)&nbsp;NOT&nbsp;NULL&nbsp;AUTO_INCREMENT;运行后,数据效果图如下:然后,conn.php代码<?php&nbsp;&nbsp;$Conn&nbsp;=&nbsp;mysql_connect('localhost',&nbsp;'root',&nbsp;'')&nbsp;or&nbsp;die(&nbsp;'数据库服务器连接失败:'&nbsp;.mysql_error()&nbsp;);&nbsp;&nbsp;mysql_query('set&nbsp;names&nbsp;utf8');&nbsp;mysql_select_db('php100')&nbsp;or&nbsp;die('数据库连接失败'&nbsp;.&nbsp;mysql_error());add.php&nbsp;<?php&nbsp;if&nbsp;(&nbsp;!empty(&nbsp;$_POST['sub']&nbsp;)&nbsp;)&nbsp;{&nbsp;&nbsp;&nbsp;$title&nbsp;=&nbsp;$_POST['title'];&nbsp;&nbsp;&nbsp;$con&nbsp;&nbsp;&nbsp;=&nbsp;$_POST['con'];&nbsp;&nbsp;&nbsp;echo&nbsp;msyql_query("insert&nbsp;into&nbsp;new&nbsp;(title,&nbsp;contents)&nbsp;values&nbsp;('{$title}',&nbsp;'{$con}')")&nbsp;?&nbsp;'插入成功'&nbsp;:&nbsp;'插入失败:'&nbsp;&nbsp;.mysql_error();&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;?>//下面的表单自己写
打开App,查看更多内容
随时随地看视频慕课网APP