我在网上搜索了这个查询,我可以在这个平台上找到一些,但它们对我来说并不是很好解释。我有一个保存消息的表,该表还包含用户名列,用于使用登录会话跟踪信使但每次我尝试插入消息时都收到了字段'用户名'没有默认值。这是我的代码:
<?php
session_start();
if(!isset($_SESSION['user_name']) || (trim($_SESSION['user_name']) == '')) {
header("location: ../login");
exit();
}else{
$user= $_SESSION['user_name'];
if(!empty($_POST['message'])) {
include_once 'includes/config.php';
include_once 'includes/security.php';
include_once 'includes/smileys.php';
$message = clean(mysql_real_escape_string($_POST['message']));
$message = special_chars($message);
$time = time();
//getting image link
if(!empty($_POST['pic_url'])) {
$image = strip_tags($_POST['pic_url']);
} else {
$image = '';
}
$git = '';
//getting video link
if(!empty($_POST['y_link'])) {
$video = fix_url(strip_tags($_POST['y_link']));
} else {
$video = '';
}
//insert into wall table
$query = mysql_query("INSERT INTO posts SET
post_desc = '$message',
image_url = '$image',
vid_url = '$video',
username = '',
git = '$user',
post_date = '$time')") or die(mysql_error());
$ins_id = mysql_insert_id();
?>
sql表语法:
CREATE TABLE `posts` (
`post_id` int(9) NOT NULL,
`post_desc` mediumtext NOT NULL,
`image_url` varchar(100) NOT NULL,
`vid_url` varchar(100) NOT NULL,
`username` varchar(100) NOT NULL,
`git` varchar(100) DEFAULT NULL,
`post_date` varchar(20) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;