mysql error, i need help pls

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jade756
Forum Newbie
Posts: 4
Joined: Fri May 22, 2009 5:21 am

mysql error, i need help pls

Post by jade756 »

hello everyone..
well, im making a simple chatroom..

Code: Select all

<?php
    require '../db/db_connect.php';
    require '../db/session.php';
    $id=$_SESSION['id'];
    $message=$_POST['message'];
    if($message==null)
        header("Location: ../messagearea.php");
    
    else
        {
          $time = date('Y').date('m').date('d').date('h').date('i').date('s');
          $insert1 = "INSERT INTO mainchat
                    (mainID, chatID, maintime, message) VALUES
                    ('', '$id', '$time', '$message')";
                    
        if(!mysql_query($insert1))
        { die(mysql_error()); }
    
        else
        { header("Location: ../messagearea.php"); }
?>
 
here my problem:
if $message contains a " ' " (ex. I'm here).. this generate an error in the insert statement..
when $message doesnt contain " ' ", its works fine..

btw, the $message is a LONGTEXT.. does it matter.. but I changed it to MEDIUMTEXT and TEXT but it still have that error when $message contains " ' "
thanks in advance
Last edited by Benjamin on Fri May 22, 2009 9:47 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: mysql error, i need help pls

Post by susrisha »

google out for mysql_real_escape_string function

Code: Select all

 
mysql_real_escape_string();
 
anand
Forum Commoner
Posts: 80
Joined: Fri May 22, 2009 11:07 am
Location: India
Contact:

Re: mysql error, i need help pls

Post by anand »

susrisha wrote:google out for mysql_real_escape_string function

Code: Select all

 
mysql_real_escape_string();
 
+1.

You should try this

Code: Select all

mysql_real_escape_string($message);

This will escape all \n , \r , ' and so on.
jade756
Forum Newbie
Posts: 4
Joined: Fri May 22, 2009 5:21 am

Re: mysql error, i need help pls

Post by jade756 »

thank u very much for your reply.. :mrgreen:
Post Reply