Results 1 to 3 of 3

Thread: Chat Script Error

  1. #1
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Chat Script Error

    I created a little script for live chat, but get an error whenever there are more than 10 entries. Here is my code:
    PHP Code:
    <?php

    function printMessage() {
        
    $show mysql_query("SELECT * FROM `chat` ORDER BY time") or die ('Error Getting Messages! <br />' .mysql_error());
            while (
    $msg mysql_fetch_array($show)) {
                echo 
    ''.date('h:m'$msg['time']).' '.$msg['user'].' >'.$msg['message'].' <br />
                '
    ;
            }
    }


    function 
    addMessage($user$message) {
        
    $user mysql_escape_string(strip_tags($_POST['user']));
        
    $message mysql_escape_string(strip_tags($_POST['message'], '<a><b><i><u>'));
        
    mysql_query("INSERT INTO `chat` (id, time, user, message) VALUES ('10', 'time()', '$user', '$message')") or die ('Error Posting! <br />' .mysql_error());
        
    mysql_query("UPDATE `chat` SET id = id-1") or die (mysql_error());
        
    mysql_query("DELETE FROM `chat` WHERE id < 1") or die (mysql_error());
    }


    if (
    $_POST['send']) {
        
    addMessage($user$_POST['message']);
        
    printMessage();
    }


        echo 
    '
    <!-- Chat Form -->
        <form method="post" action="" name="chat">'
    ;
    if (!
    $_COOKIE['chat']) {
        if (!
    $_POST['send']) {
            echo 
    '
            Username: <input type="text" name="user" />
                <br />'
    ;
        } else {
            @
    setcookie('chat'strip_tags($user), time() + 3600);
            echo 
    '
            <input type="hidden" name="user" value="'
    .$_POST['user'].'" />';
        }
    }
        
    ?>
            Message: <input name="message" type="text" size="50" />
            <input type="submit" name="send" value="Send" />
        </form>
    <!-- /Chat Form -->
    After it gets up to about 10 messages, it gives the error "Duplicate entry '9' for key 1". I know that it is something wrong with the addMessage function in the first and second sql queries, but can't figure out another way to do it. Here is my sql table in case you need it:
    Code:
    CREATE TABLE `chat` (
    	`id` int(4) NOT NULL auto_increment,
    	`time` varchar(10) NOT NULL,
    	`user` varchar(32) NOT NULL,
    	`message` varchar(255) NOT NULL,
    	PRIMARY KEY (`id`)
    ) TYPE=HEAP AUTO_INCREMENT=1;
    I want it to only keep the latest 10 messages in the database. Any ideas? Thanks
    Thanks DD, you saved me countless times

  2. #2
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Anybody have an idea of what I need to change?
    Thanks DD, you saved me countless times

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I'm guessing that you've got a non-unique ID in there somewhere, which isn't allowed since it's a primary key. Rather than using a VARCHAR(10) for the time field, you might want to use a DATETIME and the MySQL NOW() function.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •