i need a little help on a code im working on (please help me smart guys!)

Discussion in 'PHP' started by mybluehair, Nov 10, 2007.

  1. #1
    (you can skim threw this, and it will still make sence)

    i was bored, so i decided to start making a chat room runing only on php and mysql. and its all one one .php file (the form for users to enter messages, the part were mysql inserts that info that they typed into the DB, and the part were all the messages entered display on the page). the only problem is that having it run on all the same page creates a few problems. in that one page, i have to put the code:

    $query = "INSERT INTO contacts VALUES ('', '$message')";
    PHP:
    so that if the user entered any thing into the chat box, it will place that info in my mysql DB. now the problem with that is if a person is just coming into the chat room for the first time(and ofcourse not having entered any previous chat message), that code above will still play out, and will enter a empty row into my mysql. now stay with me here. it wouldnt be such a big deal for me, but i have to use this code to display the info:

    $query="SELECT * FROM chat";
    $result=mysql_query($query);
    
    
    $num=mysql_numrows($result);
    
    mysql_close();
    
    echo "<b><center>chats:</center></b><br><br><br>";
    
    $i=0;
    while ($i < $num) {
    
    $message=mysql_result($result,$i,"message");
    
    
    echo "<b>$message</b><br><hr><br>";
    
    $i++;
    }
    PHP:

    ok, so that means, if the first row in the messages feild in my DB is empty, the php loop code starts ordering all the messages backward. so lets say u enter hi, and click submit, it would display that info u entered on the bottom of the list insted of the top. (unless the first row is not empty)

    now, can anyone help me? i was weondering if maybe theres a better way of writing out a display code. or maybe having a code that sayed "if the text box was empty on submittion, dont instert anything into the DB". so come on guys. got any ideas?
     
    mybluehair, Nov 10, 2007 IP
  2. joshm

    joshm Peon

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi. Try something like this:

    if ($message == '') {
    $message = false;
    } else {
    $query = "INSERT INTO contacts VALUES ('', '$message')";
    }
    PHP:
    This will only insert $message into the DB if it is not empty.
     
    joshm, Nov 10, 2007 IP
  3. mybluehair

    mybluehair Peon

    Messages:
    1,374
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #3
    ^ WOW, i didnt think of that. thats so basic, but i didnt think of it. thank you
     
    mybluehair, Nov 10, 2007 IP
  4. mybluehair

    mybluehair Peon

    Messages:
    1,374
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #4
    but for some reason its still displaying it backwards. (ur code worked) but still. does anyone have any more ideas?
     
    mybluehair, Nov 10, 2007 IP
  5. mybluehair

    mybluehair Peon

    Messages:
    1,374
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #5
    here is my hole code:


    $database = "**********";
    $username = "******";
    $password = "******";
    
    $message=$_POST['message'];
    
    
    echo '
    <center><a href="logintoap.html">Admin panel</a></center><br>
    
    <form action="allinone.php" method="post"> <br>
    chat message:<br> <textarea rows="12" cols="65" name="message" id="message"></textarea> <br><br>
    <input type="submit"><br>';
    
    
    
    mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die ( "unable to connect");
    if ($message == '') {
    $message = false;
    } else {
    $query = "INSERT INTO contacts VALUES ('', '$message')";
    }
    
    
    mysql_query($query);
    
    mysql_close();
    
    echo '<hr>';
    
    
    mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die ( "unable to connect");
    $query="SELECT * FROM contacts";
    $result=mysql_query($query);
    
    
    $num=mysql_numrows($result);
    
    mysql_close();
    
    echo "<b><center>chats:</center></b><br><br><br>";
    
    $i=0;
    while ($i < $num) {
    
    $message=mysql_result($result,$i,"message");
    
    
    echo "<b>$message</b><br><hr><br>";
    
    $i++;
    }
    PHP:
     
    mybluehair, Nov 10, 2007 IP
  6. bobb1589

    bobb1589 Peon

    Messages:
    289
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #6
    SELECT * FROM contacts ORDER by 'id' DESC

    im not sure if your using an id but whatever your unique identifier is for the field then replace that with id
     
    bobb1589, Nov 10, 2007 IP