0

i have searched over the net for this query and I could find some on this platform but they are not well explanatory to me. I have a table which holds message and the table also contains username column to track the messenger using login session but every time I tried to insert a message i received Field 'username' doesn't have a default value. here is my code:

<?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();


?>

the sql table syntax:

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;
Shadow
  • 33,525
  • 10
  • 51
  • 64
Fagbemi Ayodele
  • 321
  • 5
  • 15
  • 4
    **WARNING**: If you're just learning PHP, please, do not use the [`mysql_query`](http://php.net/manual/en/function.mysql-query.php) interface. It’s so awful and dangerous that it was removed in PHP 7. A replacement like [PDO is not hard to learn](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/) and a guide like [PHP The Right Way](http://www.phptherightway.com/) explains best practices. Your user data is **not** [properly escaped](http://bobby-tables.com/php.html) and there are [SQL injection bugs](http://bobby-tables.com/) and can be exploited. – tadman May 18 '17 at 00:07
  • That table definition is full of problems. Use InnoDB. Use `utf8` encoding. Use `VARCHAR(255)` as a default size unless you need something longer. Use `INT(11) PRIMARY KEY AUTO_INCREMENT`. Use `INSERT INTO x (a,b,c) VALUES (?,?,?)` instead of the non-standard `SET`. – tadman May 18 '17 at 00:08
  • @tadman, thanks for the advise but what do you think might caused such an issue? – Fagbemi Ayodele May 18 '17 at 00:10
  • There's so many things here that are just plain confusing that I'm not sure what the exact problem is. One thing to remember on Stack Overflow is adding the precise error message you get, not just some approximation of it, is very helpful not just in terms of communicating what went wrong, but in helping others with the same problem find this question. – tadman May 18 '17 at 00:11
  • the error gotten is what I used as the header which is Field 'username' doesn't have a default value. What could be so confusing about that? – Fagbemi Ayodele May 18 '17 at 00:14
  • 1
    What's confusing about that is that query you're showing can't generate that error: `username=''` is specified. – tadman May 18 '17 at 00:16
  • Are you sure that the error message complains about the username field and not the post_id field? Post_id is not declared as auto increment, but is not null, yet you do not provide any value for it. – Shadow May 18 '17 at 00:17
  • @Shadow, No it wasn't complaining about post_id but username. – Fagbemi Ayodele May 18 '17 at 00:26
  • Is the server yours? As in, do you have root access to it? – icecub May 18 '17 at 00:30
  • I'm sorry, but I cannot reproduce the behaviour. For me mysql complains about no default value provided for post_id field. – Shadow May 18 '17 at 00:30
  • @Shadow The problem is most likely caused as stated in the +130 answer here: http://stackoverflow.com/questions/15438840/mysql-error-1364-field-doesnt-have-a-default-values – icecub May 18 '17 at 00:32
  • @icecub no, that's a different scenario. – Shadow May 18 '17 at 00:48
  • @Shadow Ah ok. It seemed a bit simular to me since the code above simply should not generate that error. – icecub May 18 '17 at 00:51
  • @icecube I agree, the above code does not generate the error message specified. This is why I voted to close it down as cannot be reproduced. – Shadow May 18 '17 at 01:05

0 Answers0