Jump to content

[SOLVED] need help with a php coding im making (come on smart guys)


mybluehair

Recommended Posts

ok, so im making a chat room using only php and mysql. here is my full code (this is one file called allinone.php):

 

 

$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++;

}

 

so hopfully u can imagnine what that looks like, and what it does. but for some reason, half the time, the code that displays the messages from my DB will order it wong. so lets yes you type in "hello" and click enter. the "hello" will appear on the bottom of the list of everything else sayed, intead of appearing of the top. can some one help me? how do i correct this?

Link to comment
Share on other sites

This needs to use the ORDER BY function

$query="SELECT * FROM contacts";

To

$query="SELECT * FROM `contacts` ORDER BY created_date DESC";

I'm don't know the schema of your database so I can tell you exactly what to order by. But you have the timestamp of when the message was created, that would be a good field to use.

Link to comment
Share on other sites

This needs to use the ORDER BY function

$query="SELECT * FROM contacts";

To

$query="SELECT * FROM `contacts` ORDER BY created_date DESC";

I'm don't know the schema of your database so I can tell you exactly what to order by. But you have the timestamp of when the message was created, that would be a good field to use.

 

ok. um...how would i creat a field like that? (timestamp) how exactly would i get it to know what time a message was created?

 

also when i enter your code i get error:

 

Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /home/gotdrake/public_html/tutorial/allinone.php on line 40

 

line 40 is:

 

$num=mysql_numrows($result);

 

 

Link to comment
Share on other sites

ok. um...how would i creat a field like that? (timestamp) how exactly would i get it to know what time a message was created?

 

ill post how i did it...

MySQL: somerow DATETIME NOT NULL

and from your script you need something like that:

$query = "INSERT INTO sometable (somerow) VALUES (NOW())";

 

Link to comment
Share on other sites

ok. um...how would i creat a field like that? (timestamp) how exactly would i get it to know what time a message was created?

 

ill post how i did it...

MySQL: somerow DATETIME NOT NULL

and from your script you need something like that:

$query = "INSERT INTO sometable (somerow) VALUES (NOW())";

 

 

 

ok. thank you. im almost there. here is my little code:

 

$query = "INSERT INTO contacts VALUES ('', '$message', NOW())";

 

as you can imagine, i have created a new field in contacts called "datetime" and i used the little "now" code thing you gave me. so i tested it out. when i enter in a message, and submit it, i go to myphpadmin and in browse i see my message i entered, and along with it, in the datetime feild, i see the time it was created. but only problem now, is when i change:

$query="SELECT * FROM `contacts` ";

to:

$query="SELECT * FROM `contacts` ORDER BY created_date DESC";

 

all i get is a mysql error. maybe you could help me out. here is the bit in my file that works as showing entered messges:

 

$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++;

}

how would i change this, so it could use the datetime to order them correctly?

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.