The forum working with database MySQL
Apache web - server is necessary for job of a forum with support PHP and database MySQL. The script forum.php3 is located in the catalogue/usr/local/apache/htdocs (without blanks). The working forum can be seen here <http://phpmysql.flashmaster.ru/forum.php>, and full listing here <http: // www.zk.ru/alextr/forumcode.html>.
The beginning
Before to begin consideration of practical application PHP, it is necessary enter in MySQL as the manager and to create a database in which the information will be stored{kept}.
We create a database forum.
mysqladmin-u root-p create forum;
use forum;
We create 3 tables in a database forum. In them the information will enter the name.
create table info (
id int (4) not null auto_increment primary key,
name varchar (15) not null,
password varchar (15) not null,
email varchar (40) not null,
posts int (4) default ' 0 ',
key (name), key (password));
create table topics (
top_id int (4) not null auto_increment primary key,
top_name varchar (255) not null,
name varchar (15) not null,
message text not null,
replies int (4) default ' 0 ',
post_date datetime default ' 0000-00-00 00:00:00 ',
last_reply datetime default ' 0000-00-00 00:00:00 ',
key (top_name), key (name));
create table replies (
name varchar (15) not null,
top_name varchar (255) not null,
reply text not null,
reply_date datetime default ' 0000-00-00 00:00:00 ',
key (name), key (top_name));
Registration of the user in a DB
Further will go pure{clean} PHP and HTML.
Let's generate the form for registration:
<h2 align=center> <font color = "ff0000"> Registration. </font> </h2>
<p> <font face = "serif" size=2> Please, fill in the form below.
<p> You may use only english chars
in your name, and name's length should be not less than 3 and
not more than 15 characters.
<br> Remember:all form fields are case sensitive. It means the names
<font face = "arial" size=2
color = "0000ff"> name </font> and
<font face = "arial" size=2 color = "0000ff"> Name </font> are different.
<p> Password's length should be not less than 6 characters. <br>
Don't forget to enter your e-mail address, you may need it if you'll forget your password.
<p> <FORM ACTION = "" METHOD = "POST" name = "reg">
<p align=right> <a href = ""> Home </a>
<center> <TABLE BGCOLOR = "bfbfbf">
<tr> <td colspan=2> </td> </tr>
<tr> <TD> <b> Login: </b> <TD>
<INPUT TYPE = "text" NAME = "login" SIZE = "20" maxlength = "15">
<tr> <TD> <b> Password: </b> <TD>
<INPUT TYPE = "password" NAME = "pass" SIZE = "20" onFocus = " is_ValidLogin (this.form.login.value) "; return true>
<tr> <TD> <b> Verify password: </b> <TD>
<INPUT TYPE = "password" NAME = "verpass" SIZE = "20" onFocus = " is_ValidPass (this.form.pass.value) "; return true>
<tr> <TD> <b> E-mail: </b> <TD>
<INPUT TYPE = "text" NAME = "email" SIZE = "20" onFocus = " verPasswd () "; return true>
<tr> <td colspan=2> <input type = "hidden" name = "action" value = "register">
<tr> <td colspan=2> <input type = "hidden" name = "verify" value = "ok">
<tr> <TD colspan=2> <p> <center>
<INPUT TYPE = "submit" name = "submit" VALUE = "Submit" onMouseOver = " true_Email (this.form.email.value) "; return true> </center>
<tr> <td colspan=2>
</table>
</form>
The functions used in the form, serve for check of the information entered by the user. Further, it is necessary to process the data. For connection with a DB function mysql_connect is used () .Poskol`ku to incorporate from a DB it is necessary not time, I have born{have taken out} all data in function connect ()
function connect () {
$hostname = "localhost:3306";
$user = "root";
$password = " your password ";
$db = "forum";
mysql_connect ($hostname, $user, $password);
$request=mysql_select_db ($db);
if (! mysql_connect) {
echo " Error: " .mysql_error ();
}
}
This function connects a database and checks presence of mistakes of connection. Now it is possible to receive and process the data of the user input.
if ($action == "register") {
connect ();
if ($verify) {
$query = " select id from info where name = ' $ login ' "; // Search to a DB.
$result=mysql_query ($query);
if (mysql_num_rows ($result)) {// It is checked, whether there is already such user.
print_header ();
echo " <p align=center> <b> Error! </b> ";
echo " <p> Name <b> ". $login. " </b> already exists. Please, go back and choose another name. ";
?>
<p> <center> <a href = "? action=register "> Go back </a> </center>
<? php
} else {
// If all OK it is inserted the data in the table info.
$query = " insert into info (name, password, email) values (' $login ',' $pass', ' $ email ') ";
$result=mysql_query ($query);
// We speak the user of thanks and we unpack{we print out} the information.
print_header ();
echo " <h3 align=center> Thank you, <font color ='ff0000 '> ". $login. "! </font> </h3> ";?>
<p> You are successfuly registered and your information added to our database.
<p> Please, remember your information. Only registered users may post messages and replies.
<p> <font face ='arial ' size=2 color = '0000ff'> Your login: </font>
<font face ='arial ' size=2 color ='ff0000 '> </font> <br>
<font face ='arial ' size=2 color = '0000ff'> Password: </font>
<font face ='arial ' size=2 color ='ff0000 '> </font> <br>
<font face ='arial ' size=2 color = '0000ff'> E-mail: </font>
<font face ='arial ' size=2 color ='ff0000 '> </font>
<p> Good luck!
<center> <a href = ""> | Go to topics </a> |
<a href = "? action=add_new_top "> Post new topic </a> | </center>
<? php
}
}
Function print_header () is heading of html-page. She too is used for reduction of quantity{amount} of a code.
function print_header () {
?>
<html> <head> <title> Forum version 1.1 </title>
</head> <style> A:link {font-family:arial; font-size:10pt; text-decoration:none; color: * 0000ff;}
A:hover {font-family:arial; font-size:10pt; text-decoration:none; color:red;} ";
A:visited {font-family:arial; font-size:10pt; text-decoration:underline; color: * 0000ff;}
BODY {background-color: * e6e8fa; font-family:arial; font-size:10pt; color: * 333300;}
TD {font-family:arial; font-size:10pt; color: * 333300;}
H2 {text-align:center; color:blue;}
</style> <body>
<? php
}
Well that's all. The user is registered and can place messages in a forum.
Creation new topika
For creation new topika it is used the following form:
<h3 align=center> <font color = "0000ff"> New topic </font> </h3>
<p align=right> | <a href = " <? php echo $script?> "> Go to topics </a>
| <a href = " <? php echo $script?>? action=register "> Register </a> |
<p align=right> <font color = "0000ff">
Only registered users may post messages in this forum. </font>
<p> <form action = " <? php echo $script?> " name = "newtop" method = "POST">
<table width=60 % align=center bgcolor = "aaaaaa">
<tr> <td> *nbsp;
<tr> <td> <b> Your nick: </b> <td> <input type = "text" name = "nick" size=20>
<tr> <td> <b> Your password: </b> <td> <input type = "password" name = "passwd" size=20>
<tr> <td> <b> Topic name: </b> <td> <input type = "text" name = "theme" size=20>
<tr> <td colspan=2> *nbsp; <b> Message: </b> <p> *nbsp; *nbsp; <textarea cols=40 rows=15
WRAP = "VIRTUAL" name = "message"> </textarea>
<tr> <td colspan=2> <input type = "hidden" name = "action" value = "add_new_top">
<tr> <td colspan=2> <input type = "hidden" name = "test" value = "ok">
<tr> <td colspan=2 align=center> *nbsp; <br>
<input type = "submit" name = "post" value = "Submit"> </table>
<? php
All fields are obligatory for filling. The user should enter the name under which he was registered in a DB and the password. The script checks their presence in a DB and if it's OK be located{place} the message in the table topics. If the name and the password are entered the mistake incorrect - is given out. Place messages the registered users can only. Here is how it looks:
elseif ($action == "add_new_top") {
connect (); // we Are connected to a DB.
if ($test) {
// We read the data from the form.
$date=date (" Y-m-d H:i:s ", mktime ());
$message=htmlspecialchars ($message);
$message=nl2br ($message);
$message=str_replace ("," ", $message);
$theme=str_replace ("," ", $theme);
$theme=str_replace (' "'", ", $theme);
$theme=htmlspecialchars ($theme);
// Here it is necessary to give explanatories about use
// Functions htmlspecialchars (), str_replace () and nl2br ().
// htmlspecialchars () it is used that
// To transform html-tegi. It is done{made} with a view of safety
And for protection against a different sort of jokers:)
// str_replace () replaces a symbol unary inverted commas with a blank,
// Since entering unary inverted commas in a DB
// It invalid - it a special service symbol. It will protect a DB from breaking.
// nl2br () will transform a symbol of the end of a line in html-teg <br>.
// It is used that the text entered into the form was saved constant.
$query = " select id from info where name = ' $ nick ' AND password = ' $ passwd ' ";
// Search to BD.Proverka a name and the password.
$result=mysql_query ($query);
// If all OK, we check, that the field of the form were not empty.
if (mysql_num_rows ($result)) {
if (strlen ($theme)! =0 ** strlen ($message)! =0) {
// If all OK, we insert the data in the table topics.
$query = " insert into topics (top_name, name, message, post_date, last_reply)
values (' $theme ',' $nick ',' $message ',' $date ',' $date ') ";
$result=mysql_query ($query);
// We update the table we info-increased quantity{amount} of posts for the given user on 1.
$query = " update info set posts=posts+1 where name = ' $ nick ' ";
$result=mysql_query ($query);
print_header ();
// We speak the user of thanks.
?>
<h3 align=center> Thank you, <font color = "ff0000"> <? php echo $nick?>! </font> </h3>
<p> Your topic has been submitted.
<p> <center> | <a href = " <? php echo $script?> "> Go to topics </a> |
<a href = " <? php echo $script?>? action=add_new_top "> Post new topic </a> | </center>
<? php
} else {
print_header ();
// If one of fields of the form appeared the mistake emptier - is given out.
?>
<h2 align=center> Error! </h2>
<p> You did not enter topic name or message. Please, go back and try again.
<p> <center>
| <a href = " <? php echo $script?>? action=add_new_top "> Go back </a>
| <a href = "? action=remind_pass "> Forgot password? </a> | </center>
<? php
}
} else {
print_header ();
// If an incorrect name or the password are entered, the mistake also is given out.
?>
<h2 align=center> Error! </h2>
<p> You entered incorrect name or password. Please, go back and try again.
<p> <center> | <a href = " <? php echo $script?>? action=add_new_top "> Go back </a>
| <a href = " <? php echo $script?>? action=remind_pass "> Forgot password? </a> | </center>
<? php
}
}
All these precautions not superfluous. First with a view of safety of the database. Second for protection of a forum against casual visitors and against breaking. New topik it is created also the information it is added in the table topics. Further we shall consider how to place the answer to the message.
Parcel{Sending} of the answer to the message
She differs nothing from creation new topika, only the answer is located in the table replies. We shall generate the form.
<h3 align=center> <font color = "0000ff"> Post reply. </font> </h3>
<p align=right> | <a href = " <? php echo $script?> "> Go to topics </a>
| <a href = " <? php echo $script?>? action=register "> Register </a> |
<p align=right> <font color = "0000ff">
Only registered users may post messages in this forum. </font>
<p> <form action = " <? php echo $script?> " name = "reply" method = "POST">
<table width=60 % align=center bgcolor = "aaaaaa">
<tr> <td> *nbsp;
<tr> <td> <b> Your nick: </b> <td> <input type = "text" name = "nick" size=20>
<tr> <td> <b> Your password: </b> <td>
<input type = "password" name = "passwd" size=20>
<tr> <td colspan=2> *nbsp; <b> Message: </b> <p> *nbsp; *nbsp;
<textarea cols=40 rows=15 WRAP = "VIRTUAL" name = "message"> </textarea>
<tr> <td colspan=2> <input type = "hidden" name = "action" value = "post_reply">
<tr> <td colspan=2> <input type = "hidden" name = "top_name" value = " <? php echo $top_name?> ">
<tr> <td colspan=2> <input type = "hidden" name = "validate" value = "ok">
<tr> <td colspan=2 align=center> *nbsp; <br>
<input type = "submit" value = "Submit"> </table>
And data processing:
elseif ($action == "post_reply") {
if ($validate) {
connect ();
$date=date (" Y-m-d H:i:s ", mktime ());
$message=htmlspecialchars ($message);
$message=nl2br ($message);
$message=str_replace ("," ", $message);
$query = " select id from info where name = ' $ nick ' AND password = ' $ passwd ' ";
$result=mysql_query ($query);
if (mysql_num_rows ($result)) {
if (strlen ($message)! =0) {
print_header ();
$query = " insert into replies (name, top_name, reply, reply_date)
values (' $nick ',' $top_name ',' $message ',' $date ') ";
$result=mysql_query ($query);
$query = " update topics set replies=replies+1, last_reply = ' $ date '
where top_name = ' $ top_name ' ";
$result=mysql_query ($query);
$query = " update info set posts=posts+1 where name = ' $ nick ' ";
$result=mysql_query ($query);
echo " <h3 align=center> Thank you,
<font color ='ff0000 '> ". $nick. "! </font> </h3> ";
echo " <p> Your reply has been submitted. ";
?>
<p> <center> | <a href = " <? php echo $script?> "> Go to topics </a>
| <a href = " <? php echo $script?>? action=add_new_top ">
Post new topic </a> | </center> <? php
} else {
print_header ();
?>
<h2 align=center> Error! </h2>
<p> You did not enter message. Please, go back and try again.
<p> <center> | <a href = " <? php echo $script?>
? action=post_reply*top_name =
<? php echo $top_name?> "> Go back </a>
| <a href = " <? php echo $script?>? action=remind_pass ">
Forgot password? </a> | </center>
<? php
}
}
With it, I think, all is understandable, as well as in the previous example.
Reading topika
It is rather volumetric part of a script, therein I shall allow explanatories.
elseif ($action == "read_topic") {
if ($top_name) {
connect ();
// In this case the data get out of 2 tables - topics and info.
$query = " select message, post_date, info.posts,
info.email from topics, info where topics.name =
info.name and topics.top_name = ' $ top_name ' and topics.name = ' $ name ' ";
$result=mysql_query ($query);
$row=mysql_fetch_array ($result);
$message = $ row ["message"];
$post_date = $ row ["post_date"];
$posts = $ row ["posts"];
$email = $ row ["email"];
// We cut out tegi <br> that they were not displayed on page.
$message=str_replace (" *lt; br*gt; ", " ", $message);
$char = $ message;
// This heap of transformations allows to place
// In messages smileys and to display them as figures.
$char=str_replace (":) ", " <img src = './images/smile.gif '> ", $char);
$char=str_replace (": (", " <img src = './images/frown.gif '> ", $char);
$char=str_replace (":o", " <img src = './images/redface.gif '> ", $char);
$char=str_replace (":D", " <img src = './images/biggrin.gif '> ", $char);
$char=str_replace (";) ", " <img src = './images/wink.gif '> ", $char);
$char=str_replace (":p", " <img src = './images/tongue.gif '> ", $char);
$char=str_replace (":cool: ", " <img src = './images/cool.gif '> ", $char);
$char=str_replace (":rolleyes: ", " <img src = './images/rolleyes.gif '> ", $char);
$char=str_replace (":mad: ", " <img src = './images/mad.gif '> ", $char);
$char=str_replace (":eek: ", " <img src = './images/eek.gif '> ", $char);
$char=str_replace (":confused: ", " <img src = './images/confused.gif '> ", $char);
print_header ();
?>
// Now all chosen data it is placed in the html-table and we display.
<p align=right> | <a href = " <? php echo $script?> "> Go to topics </a> |
<a href = " <? php echo $script?>? action=add_new_top "> Post new topic </a> |
<a href = " <? php echo $script?>? action=post_reply*top_name =
<? php echo $top_name?> "> Post reply </a> |
<p> <table cellspacing=0 cellpadding=5 border=1 width=100 %>
<tr> <td align=center width=150 bgcolor = "D5E6E1">
<font face = "Arial" size=2 color = "808080"> Author </font>
<td bgcolor = "D5E6E1"> <font face = "serif" size=2 color = "0000ff">
Topic: </font> <font face = "serif" size=2 color = "ff0000">
<? php echo $top_name?> </font> </td> </tr>
<tr> <td rowspan=2 bgcolor = "D5E6E1" align=center>
<font face = "serif" size=3 color = "ff0000">
<? php echo $name?> </font>
<br> <font face = "serif" size=2 color = "0000ff">
Posts: <? php echo $posts?> </font> </td>
<td bgcolor = "e6e8fa" height=30>
<font face = "serif" size=1 color = "000080"> Posted:
<? php echo $post_date?> </font>
*nbsp; *nbsp; *nbsp; *nbsp; *nbsp; *nbsp; <a href = " mailto:
<? php echo $email?> "> <img src = "email.gif" border=0> </a>
*nbsp; *nbsp; <font face = "serif" size=1 color = "000080"> E-mail </font>
<font face = "serif" size=1 color = "0000ff"> <? php echo $name?> </font>
*nbsp; *nbsp; *nbsp; *nbsp; *nbsp; *nbsp;
<a href = " <? php echo $script?>? action=edit*top_name =
<? php echo $top_name?> *nick =
<? php echo $name?> *type=message ">
<img src = "edit.gif" border=0> </a> *nbsp; *nbsp;
<font face = "serif" size=1 color = "000080">
Edit message </font> </td> </tr>
<tr> <td bgcolor = "f7f7f7"> <font face = "serif" size=2 color = "000080">
<? php echo $char?> </font> </td> </tr> </table>
<? php
// We limit quantity{amount} of recordings on page 20 to messages.
$lines=20;
$begin = $ page * $ lines;
if (empty ($page)) {
$page=0;
}
// We do{make} new search for a conclusion of answers on topik.
$query = " select replies.name, reply, reply_date, info.posts,
info.email from replies, info where replies.name =
info.name and replies.top_name =
' $top_name ' order by reply_date limit $begin, $lines ";
$result=mysql_query ($query);
// Further all the same as and in previous time.
while ($row=mysql_fetch_array ($result)) {
$nick = $ row ["name"];
$reply = $ row ["reply"];
$reply_date = $ row ["reply_date"];
$posts = $ row ["posts"];
$mail = $ row ["email"];
$reply=str_replace (" *lt; br*gt; ", " ", $reply);
$char = $ reply;
$char=str_replace (":) ", " <img src = './images/smile.gif '> ", $char);
$char=str_replace (": (", " <img src = './images/frown.gif '> ", $char);
$char=str_replace (":o", " <img src = './images/redface.gif '> ", $char);
$char=str_replace (":D", " <img src = './images/biggrin.gif '> ", $char);
$char=str_replace (";) ", " <img src = './images/wink.gif '> ", $char);
$char=str_replace (":p", " <img src = './images/tongue.gif '> ", $char);
$char=str_replace (":cool: ", " <img src = './images/cool.gif '> ", $char);
$char=str_replace (":rolleyes: ", " <img src = './images/rolleyes.gif '> ", $char);
$char=str_replace (":mad: ", " <img src = './images/mad.gif '> ", $char);
$char=str_replace (":eek: ", " <img src = './images/eek.gif '> ", $char);
$char=str_replace (":confused: ", " <img src = './images/confused.gif '> ", $char);
if (mysql_num_rows ($result)) {
?>
<table cellspacing=0 cellpadding=5 border=1 width=100 %>
<tr> <td rowspan=2 bgcolor = "e6e8fa" align=center width=150>
<font face = "serif" size=2 color = "0000ff">
<? php echo $nick?> </font> <br>
<font face = "serif" size=2 color = "333300">
Posts: <? php echo $posts?> </font> </td>
<td bgcolor = "e6e8fa" height=30>
<font face = "serif" size=1 color = "000080">
Posted: <? php echo $reply_date?> </font>
*nbsp; *nbsp; *nbsp; *nbsp; *nbsp; *nbsp;
<a href = " mailto: <? php echo $mail?> ">
<img src = "email.gif" border=0> </a>
*nbsp; *nbsp; </font> <font face = "serif" size=1 color = "000080">
E-mail </font> <font face = "serif" size=1 color = "0000ff">
<? php echo $nick?> </font>
*nbsp; *nbsp; *nbsp; *nbsp; *nbsp; *nbsp;
<a href = " <? php echo $script?>
? action=edit*top_name = <? php echo $top_name?>
*nick = <? php echo $nick?> *type=reply*reply_date =
<? php echo $reply_date?> ">
<img src = "edit.gif" border=0> </a> *nbsp; *nbsp;
<font face = "serif" size=1 color = "000080">
Edit message </font> </td> </tr>
<tr> <td bgcolor = "f7f7f7"> <font face = "serif" size=2>
<? php echo $char?> </td> </tr> </table>
<? php
}
}
// We form links for navigation on
// To pages in case the quantity{amount} of recordings is more 20.
// For this purpose we count up quantity{amount} of recordings
// In the table replies for given topika.
$query = " select COUNT (*) as count from replies where top_name = ' $ top_name ' ";
$result=mysql_query ($query);
$items=mysql_fetch_array ($result);
$count = $ items ["count"];
$pages=ceil ($count / $ lines);
// In case the quantity{amount} of answers is more 20,
// We form links with numbers{rooms} of pages.
if ($count> $lines) {
echo " <p align=right> ";
for ($i=0; $i <$pages; $i ++) {
?>
| <a href = " <? php echo $script?>? action=read_topic*top_name =
<? php echo $top_name?> *name =
<? php echo $name?> *page = <? php echo $i?> ">
<? php echo ($i+1)?> </a> <? php
}
}
}
}
That's all. Now you see, how it is simply possible to work from a DB with help PHP and as it facilitates a life. All this remains for the staff, and visitors of a forum see usual html-page. Further we shall consider, how it is possible to edit messages.
Editing of the message
Editing of the message can be necessary, if the user has made a mistake, or wants to change something in the text. To edit it is possible only the messages, it watched{kept up} with a script and BD.V dependences on that, the message topikom or the answer on topik is, 2 different forms are deduced.
connect ();
// If it topik, we find it in the table topics.
if ($type == "message") {
$query = " select message from topics where name = ' $ nick ' and top_name = ' $ top_name ' ";
$result=mysql_query ($query);
$row=mysql_fetch_array ($result);
$message = $ row ["message"];
$message=str_replace ("<br>", " ", $message);
print_header ();
// We place the message in the form for editing.
?>
<h3 align=center> <font color = "0000ff"> Edit message. </font> </h3>
<p align=right> | <a href = " <? php echo $script?> "> Go to topics </a>
| <a href = " <? php echo $script?>? action=register "> Register </a> |
<p align=right> <font color = "0000ff">
Only registered users may post messages in this forum. </font>
<p> <form action = " <? php echo $script?> "
name = "editmsg" method = "GET">
<table width=60 % align=center bgcolor = "aaaaaa">
<tr> <td> *nbsp;
<tr> <td> <b> Your nick: </b> <td>
<input type = "text" name = "name" size=20>
<tr> <td> <b> Your password: </b> <td>
<input type = "password" name = "passwd" size=20>
<tr> <td colspan=2> *nbsp; <b> Message: </b> <p> *nbsp; *nbsp;
<textarea cols=40 rows=15 WRAP 80 = "virtual" name = "post">
<? php echo $message?> <br> </textarea>
<tr> <td colspan=2>
<input type = "hidden" name = "action" value = "edit">
<tr> <td colspan=2>
<input type = "hidden" name = "submit" value = "1">
<tr> <td colspan=2>
<input type = "hidden" name = "top_name" value = " <? php echo $top_name?> ">
<tr> <td colspan=2>
<input type = "hidden" name = "nick" value = " <? php echo $nick?> ">
<tr> <td colspan=2>
<input type = "hidden" name = "type" value = " <? php echo $type?> ">
<tr> <td colspan=2>
<input type = "hidden" name = "reply_date" value = " <? php echo $reply_date?> ">
<tr> <td colspan=2 align=center> *nbsp;
<br> <input type = "submit" value = "Submit"> </table>
<? php
} elseif ($type == "reply") {
// If it is the answer on topik, we find it in
// To the table replies under the name topika.
$query = " select reply from replies where name
= ' $nick ' and top_name = ' $ top_name ' and reply_date = ' $ reply_date ' ";
$result=mysql_query ($query);
$row=mysql_fetch_array ($result);
$reply = $ row ["reply"];
$reply=str_replace ("<br>", " ", $reply);
print_header ();
?>
<h3 align=center> <font color = "0000ff"> Edit reply. </font> </h3>
<p align=right> | <a href = " <? php echo $script?> "> Go to topics </a> |
<a href = " <? php echo $script?>? action=register "> Register </a> |
<p align=right> <font color = "0000ff">
Only registered users may post messages in this forum. </font>
<p> <form action = " <? php echo $script?> "
name = "editmsg" method = "GET">
<table width=60 % align=center bgcolor = "aaaaaa">
<tr> <td> *nbsp;
<tr> <td> <b> Your nick: </b> <td>
<input type = "text" name = "name" size=20>
<tr> <td> <b> Your password: </b> <td>
<input type = "password" name = "passwd" size=20>
<tr> <td colspan=2> *nbsp; <b> Message: </b> <p> *nbsp; *nbsp;
<textarea cols=40 rows=15 WRAP 80 = "virtual" name = "post"> <? php echo $reply?> <br> </textarea>
<tr> <td colspan=2>
<input type = "hidden" name = "action" value = "edit">
<tr> <td colspan=2>
<input type = "hidden" name = "submit" value = "1">
<tr> <td colspan=2>
<input type = "hidden" name = "top_name" value = " <? php echo $top_name?> ">
<tr> <td colspan=2>
<input type = "hidden" name = "nick" value = " <? php echo $nick?> ">
<tr> <td colspan=2>
<input type = "hidden" name = "type" value = " <? php echo $type?> ">
<tr> <td colspan=2>
<input type = "hidden" name = "reply_date" value = " <? php echo $reply_date?> ">
<tr> <td colspan=2 align=center> *nbsp; <br>
<input type = "submit" value = "Submit"> </table>
<? php
}
}
Further, it is possible to count and process the data.
elseif ($action == "edit") {
if ($submit) {
connect ();
// We do{make} all as usually. A variable
// It is a line added at the end of the edited message.
$date=date (" Y-m-d H:i:s ", mktime ());
$end = "\n----------------------------------------------------------\n
This message was edited by $nick $date. ";
$post=str_replace ("," ", $post);
$post. = $ end;
$post=htmlspecialchars ($post);
$post=nl2br ($post);
// We check a name and the password.
if ($type == "message") {
$query = " select id from info where name = ' $ name ' and password = ' $ passwd ' ";
$result=mysql_query ($query);
// If all OK and it we topik-updated the table topics.
if (mysql_num_rows ($result) ** $name == $nick) {
$query = " update topics set message = ' $ post ' where top_name = ' $ top_name ' ";
$result=mysql_query ($query);
print_header ();
?>
// We speak the user of thanks.
<h3 align=center> Thank you, <font color ='ff0000 '> <? php echo $nick?> </font>! </h3>
<p> Your message was changed.
<p> <center> | <a href = " <? php echo $script?> "> Go to topics </a> |
<a href = " <? php echo $script?>? action=add_new_top ">
Post new topic </a> | </center>
<? php
} else {
print_header ();
?>
// If the name and the password are not found or the user
// Is not the author of the message the mistake is given out.
<h3 align=center> Error! </h3>
<p> This message cannot be edited. May
be you entered incorrect nick or password,
or you are not an author of this message.
<p> Please, go back and try again.
<p> <center> | <a href ='javascript:history.back () '> Go back </a> |
<a href = " <? php echo $script?> "> Go to topics </a> | </center>
<? php
}
} elseif ($type == "reply") {
// If it is the answer on topik, we change the table replies.
$query = " select id from info where name = ' $ name '
and password = ' $ passwd ' ";
$result=mysql_query ($query);
if (mysql_num_rows ($result) ** $name == $nick) {
$query = " update replies set reply = ' $ post ' where
top_name = ' $ top_name ' and name = ' $ nick '
and reply_date = ' $ reply_date ' ";
$result=mysql_query ($query);
print_header ();
?>
<h3 align=center> Thank you, <font color ='ff0000 '>
<? php echo $nick?> </font>! </h3>
<p> Your message was changed.
<p> <center> | <a href = " <? php echo $script?> "> Go to topics </a>
| <a href = " <? php echo $script?>? action=add_new_top ">
Post new topic </a> | </center>
<? php
} else {
print_header ();
?>
<h3 align=center> Error! </h3>
<p> This message cannot be edited. May be you entered incorrect nick or password, or you are not an author of this message.
<p> Please, go back and try again.
<p> <center> | <a href ='javascript:history.back () '> Go back </a> |
<a href = " <? php echo $script?> "> Go to topics </a> | </center>
<? php
}
}
}
As you can see, anything new. The same SQL-searches together with PHP-functions.
If the user has forgotten the password
If the user has forgotten the password, it is enough to him to enter of the e-mail which he has specified at registration, and the password will be sent to him on this e-mail. The form for input:
<p> <h4 align=center> Please, enter your e-mail address, you provided while registered. </h4>
<p> <FORM ACTION = " <? php echo $script?> " METHOD = "POST">
<p align=right> <a href = "forum.php3"> Home </a>
<center> <TABLE BGCOLOR = "bfbfbf">
<tr> <td colspan=2> *nbsp;
<TR> <td> <b> E-mail: </b> <TD> <INPUT TYPE = "text" NAME = "email" SIZE = "20"> *nbsp; *nbsp;
<tr> <td colspan=2> <input type = "hidden" name = "action" value = "remind_pass">
<TR> <TD colspan=3> <p> <center> <INPUT TYPE = "submit" VALUE = "Submit"> </center>
Data processing:
elseif ($action == "remind_pass") {
if ($email) {
connect ();
// We check, whether is such e-mail in a DB.
$query = " select name, password from info where email = ' $ email ' ";
$result=mysql_query ($query);
$row=mysql_fetch_array ($result);
$name = $ row ["name"];
$password = $ row ["password"];
// If is, we send the message with the password.
if (mysql_num_rows ($result)) {
$to = $ email;
$subject = " Password reminder ";
$mes = " Your password: ". $ password.".;
mail ("$to", "$subject", "$mes");
print_header ();
?>
<h3 align=center> Hello, <? php echo $name?>! </h3>
<p> You entered valid e-mail!
<p> Your password sent to your e-mail address.
<p> <center> | <a href = " <? php echo $script?> ">
Go to topics </a> | <a href = " <? php echo $script?>
? action=add_new_top "> Post new topic </a> | </center>
<? php
} else {
print_header ();
?>
// If e-mail it is not found, the mistake is deduced.
<h3 align=center> Sorry! </h3>
<p> This e-mail address has not been found in our database. <br>
Please, verify your e-mail address an try again,
or go to our registration page and register.
<p> <center> | <a href = " <? php echo $script?>
? action=remind_pass "> Go back </a> |
<a href = " <? php echo $script?>? action=register ">
Register </a> | </center>
<? php
}
}

|