[PHP] How to make a DJ Says
I'm not sure if there is also a tutorial for this on the forum, but I'll show you how I do it, and probably the most original way of doing it.
To start of, you will need a database. Once you have made yourself a database, run the following query:
PHP Code:
CREATE TABLE `message` (
`id` int(12) NOT NULL auto_increment,
`message` varchar(250) NOT NULL default '',
`ip` varchar(250) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=60 ;
Inside your database, you should now have a table named message, and inside the table there should be three fields. ID, Message and IP.
Your database is now setup, and the message that the DJ submits will now be put into the database you just made. We will learn how to make the page where the DJ submits the message later, but first we need to connect to the database.
Make a page named dbconnect.php
Inside dbconnect.php, insert the following code, editing the obvious.
PHP Code:
<?php
$connect = mysql_connect("localhost","DATABASE USERNAME","DATABASE PASSWORD");
mysql_select_db(DATABASE NAME) or die(mysql_error());
?>
Now visit this page you just made. If the page is blank, then the connection was successful. If not then you haven't setup your database name/username/password up right.
Now we need to make it so that your DJs can submit their message as the DJ Says.
Make a page named edit.php
To start of we need to include the database connection. To start of your page with the following code.
PHP Code:
<?php
include('dbconnect.php');
This is including the code which allows the inputs your going to make to submit values to the database.
PHP Code:
$_POST = array_map('strip_tags',$_POST);
$_POST = array_map('htmlspecialchars',$_POST);
if (!get_magic_quotes_gpc()) { $_POST = array_map('addslashes',$_POST); }
This protects the message from SQL attacks. It will hopefully never come in usefull, but you should add it incase a hacker ever gets into the page. To stop anyone else getting into the page you need to protect it. You will learn how to do this later.
PHP Code:
if ($_POST[submit]) {
$message = $_POST[message];
$ip = $_POST[ip];
if($message==NULL) {
echo("You cannot leave your message blank.");
}
The top three lines are telling itself what do submit upon submission. The fourth line tells itself that if the message was left null/blank/empty, then it needs to tell them that their submission was unsuccessful.
PHP Code:
else{
$message = htmlspecialchars($message);
$ip = htmlspecialchars($ip);
$query = mysql_query("INSERT INTO message (message, ip) VALUES('$message', '$ip')");
}
echo("Your DJ Message was successfully added and should now appear on the homepage.");
}
else {
echo"
<font size=\"4\">DJ Message/Says</font><br>To create your DJ Message, fill in the TEXT you would like below. HTML, BB Code and PHP have all been protected so you can not use any of those coding characters.<br><br><b>Your IP will be recorded for security reasons.</b><br><br>
<center>
<form method=\"POST\">
<input name=\"ip\" type=\"hidden\" value=\"$_SERVER[REMOTE_ADDR]\">
<textarea name=\"message\" cols=\"55\" rows=\"10\"></textarea><br>
<input type=\"submit\" value=\"Submit\" name=\"submit\">
</form>
</center>
";
}
This is the final part of the page, and the most important part too. The HTML inside the following tags:
PHP Code:
echo" HTML here ";
Is what is displayed on the page. All the DJ will see is the text explaining what the DJ Says can be used for and the input to enter their message.
Finally, we end of the page.
If there are any errors please let me know. I haven't tested it and I'm geussing it works fine.
Hope it helps!
Edited by opensourcehost (forum moderator): Thread closed because it was bumped.
How to actually show the Message
OK. Well, to show the message its simple. We need to again, connect to the database but this time round we are going to collect information instead of submitting it.
PHP Code:
<?php
include('dbconnect.php');
Add this to the TOP, its connecting to the database for you.
PHP Code:
$select = mysql_query("SELECT message from message order by id DESC LIMIT 1");
$message = mysql_fetch_array($select)
?>
This is telling itself where to find the information. Once the field/table has been found, it will collect the information your after. In this case, the latest DJ Says message written.
HTML Code:
<html>
<body>
<font size="4">DJ Says</font><br>
Lets style the page a bit. Add this after the end of your PHP.
PHP Code:
<?php echo("$message[message]"); ?>
This needs to go next. This is what actually shows the message for you.
HTML Code:
</body>
</html>
Finally we finish of the page.