BUG的缔造者 2018-04-13 12:20 采纳率: 60%
浏览 916
已采纳

PHP通过浏览器写不进数据库

做了最新实验,我的代码发给朋友,没有改动,数据库可以正常添加代码,但是我这边就不行,是数据库问题么?
图片说明

 <!DOCTYPE html>
 <html lang="en">
 <head>
     <meta charset="UTF-8">
     <link href="css.css" rel="stylesheet" type="text/css">
     <title>Title</title>
     <?php include ("add.php")?>

 </head><body>

 <hr size=1>
 <form action="add.php" method="post" >
     用户:<input type="text" size="10" name="user"/><br>
     标题:<input type="text" name="title" /><br>
     内容:<textarea name="content"></textarea><br>
     <input type="submit" name="submit" value="发布留言" />
 </form>
 </body>
 </html>

图片说明

 <?php
header("Content-type: text/html; charset=utf-8"); 
include ("conn.php");
$id=$_POST['id'];
$user=$_POST['user'];
$title=$_POST['title'];
$content=$_POST['content'];
if ($_POST['submit']){
  $sql="insert into message(id,user,title,content,lastdate)values('','$user','$title','$content',now())";
    mysql_query($sql);
    echo "<script>alert('提交成功!返回首页。');location.href='add.html';</script>";
}
?>

图片说明

数据库一点变化没有
图片说明

其他代码也放一下

 <?php
 $conn =mysql_connect("localhost", "root", "root") or die("数据库链接错误");
 mysql_select_db("test", $conn);
 mysql_query("set names 'utf8'"); //使用utf-8中文编码; 
 ?>

 <!DOCTYPE html>
 <html lang="utf-8">
 <Meta http-equiv="Content-Type" Content="text/html; Charset=utf-8">
 <head>

    <?php
 include ("conn.php");
 ?> 
  <link href="css.css" rel="stylesheet" type="text/css">
 </head>


 <table width=500 border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#add3ef" >
     <?php

     $sql="select * from message order by id desc";
     $query=mysql_query($sql);
     while($row=mysql_fetch_array($query)){  ?>

         <tr bgcolor="#eff3ff">
             <td>标题: <?php echo $row['title'];?> <font color="red">用户: <?php echo $row['user'];?> </td>
         </tr>
         <tr bgColor="#ffffff">
             <td>发表内容:<?php echo $row['content'];?></td>
         </tr>
         <tr bgColor="#ffffff">
             <td><div align="right">时间:<?php echo $row['lastdate'];?></td>
         </tr>
     <?php } ?>
     <tr bgcolor="#f0fff0">
         <td><div align="right"><a href="add.html">返回留言</a> </td>
     </tr>
 </table>
 </html>

图片说明

  • 写回答

8条回答 默认 最新

  • Go 旅城通票 2018-04-13 14:00
    关注

    ID是主键,不能插入空值啊。。如果是自动增长的列,去掉id,改下面的试试

      $sql="insert into message(user,title,content,lastdate)values('$user','$title','$content',now())";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)