wOnKo BLARG
PHP Scripts
Blarg WIP
What follows is the experimentation, in the order it was performed, that resulted in the BLARG. Some bits of the experiment are LIVE (ie. lead to actions in the prototype blarg - you can try them out by clicking on the relevant link). Some bits are triggered by previous bits (and so themselves are not linked as it would cause an error to trigger them separately).
Once I got all the bits I needed, I created a new table (different name no der!) to keep my actual blarg in
1 - Connect to the Database
(an include file - used in other scripts)
<?php
/* set the connections to the database */
$host = "localhost";
$username = "enter username here";
$passwd = "enter password here";
$blargdb = "enter the database name here";
$connection = mysql_connect($host, $username, $passwd) or die("problems connecting");
mysql_select_db($blargdb) or die ("problems finding the database");
?>
<?php
include ("connecttothedatabase.php");
/* define the create table */
if (mysql_query("create table blargtable
(blargid integer unsigned not null auto_increment primary key,
blargdate text not null,
topic text not null,
header text,
watching text,
reading text,
listening text,
mood text,
stuff text not null,
piccy text)"))
{ echo "table created successfully"; }
else
{ echo "had a problem - table probably already existed"; }
?>
<?php
include ("connecttothedatabase.php");
/* specify the data */
if (mysql_query("insert into blargtable (blargdate, title, header, watching, reading, listening, mood, stuff, piccy)
values('07-01-2005 12:41 pm',
'The Hills are alive',
'<br>',
'Black Adder',
'Davinci''s Code',
'Portishead - dummy',
'tolerant',
'blah blah',
null)"))
{ echo "table insertion successful"; }
else
{ echo "data not inserted"; }
?>
/*Note: this is any old junk, just to verify data entry occurs */
<?php
include ("connecttothedatabase.php");
/* try to delete from the table */
if (mysql_query("delete from blargtable"))
{ echo "table emptied successfully"; }
else
{ echo "had a problem - table may not exist"; }
?>
<?php
include ("connecttothedatabase.php");
/* try to drop the table */
if (mysql_query("drop table blargtable"))
{ echo "table ditched successfully"; }
else
{ echo "had a problem - table may not exist"; }
?>
<?php
$whenisitnow = date("d-m-Y g:i a");
echo $whenisitnow;
?>
http://au.php.net/manual/en/function.date.php for date string formats - they are not intuitive nor sensible, but are powerful
<h3 align="center">Enter Data</h3>
<form action="stickinformdata.php" method="POST">
<table border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td>topic:</td>
<td><input name="topic" type="text" value="?" size="80"></td>
</tr>
<tr>
<td>header:</td>
<td><textarea name="header" cols="80" rows="3">?</textarea></td>
</tr>
<tr>
<td>watching:</td>
<td><input name="watching" type="text" value="?" size="80"></td>
</tr>
<tr>
<td>reading:</td>
<td><input name="reading" type="text" value="?" size="80"></td>
</tr>
<tr>
<td>listening:</td>
<td><input name="listening" type="text" value="?" size="80"></td>
</tr>
<tr>
<td>mood:</td>
<td><input name="mood" type="text" value="?" size="80"></td>
</tr>
<tr>
<td>stuff:</td>
<td><textarea name="stuff" cols="80" rows="4">?</textarea></td>
</tr>
<tr>
<td>piccy:</td>
<td><input name="piccy" type="text" value="?" size="80"></td>
</tr>
<tr>
<td> </td>
<td><input type="SUBMIT" value="post my information"></td>
</tr>
</table>
</form>
/* note: the creation of form elements (and naming them) when bounced by a POST action to a PHP page creates VARIABLES with the same names in the target page - the topic input here makes a variable called $topic in the connected form - neat hey? */
8 - Take stuff typed in into the above form and inject it into the database
<?php
include ("connecttothedatabase.php");
$whenisitnow = date("d-m-Y g:i a");
/* specify the data */
if (mysql_query("insert into blargtable (blargdate, topic, header, watching, reading, listening, mood, stuff, piccy)
values('".$whenisitnow."',
'".$_POST['topic']."',
'".$_POST['header']."',
'".$_POST['watching']."',
'".$_POST['reading']."',
'".$_POST['listening']."',
'".$_POST['mood']."',
'".$_POST['stuff']."',
'".$_POST['piccy']."')"))
{ echo "form data insertion successful"; }
else
{ echo "data not inserted";
$error_number = mysql_errno();
$error_msg = mysql_error();
echo "error $error_number: $error_msg"; }
?>
/* note: the string catenation here is horrible (is there an easier way?) and this is because we are inserting TEXT values - these need to be enclosed in quotes, but the quote is a reserved character,
so the elaborate
' ". textvariable ." ' construct around each is necessary to ensure the enclosed double quotes- phew!
note: the use of $_POST['formfieldname'] is the way of accessing the previously posted data (from the form that leads to this script) - the form "posts variable information to a section of the servers memory, we have to politely access it.
*/
<?php
include ("connecttothedatabase.php");
$gimme = "select * from blargtable";
/* attempt to retrieve the data */
$data = mysql_query($gimme, $connection);
if (!$data)
{
echo "no data was returned";
}
else
{
echo "<table cellpadding=5 border=1 cellspacing=0>";
while (list($blargid, $blargdate, $topic, $header, $watching,
$reading, $listening, $mood, $stuff, $piccy) = mysql_fetch_row($data))
{
echo "<tr>";
echo "<td>". $blargid . "</td>";
echo "<td>". $blargdate . "</td>";
echo "<td>". $topic . "</td>";
echo "<td>". $header . "</td>";
echo "<td>". $watching . "</td>";
echo "<td>". $reading . "</td>";
echo "<td>". $listening . "</td>";
echo "<td>". $mood . "</td>";
echo "<td>". $stuff . "</td>";
echo "<td>". $piccy . "</td>";
echo "</tr>";
}
echo "</table>";
}
?>
<?php
include ("connecttothedatabase.php");
/* first work out the latest = biggest blargid */
$findbiggest = "select max(blargid) from blargtable";
$result = mysql_query($findbiggest,$connection);
$biggest = mysql_fetch_row($result);
/* now retrieve data filtered on this biggest */
$gimme = "select * from blargtable where blargid = $biggest[0]";
$data = mysql_query($gimme, $connection);
if (!$data)
{
echo "no data was returned";
}
else
{
echo "<table cellpadding=5 border=1 cellspacing=0>";
while (list($blargid, $blargdate, $topic, $header, $watching,
$reading, $listening, $mood, $stuff, $piccy) = mysql_fetch_row($data))
{
echo "<tr>";
echo "<td>". $blargid . "</td>";
echo "<td>". $blargdate . "</td>";
echo "<td>". $topic . "</td>";
echo "<td>". $header . "</td>";
echo "<td>". $watching . "</td>";
echo "<td>". $reading . "</td>";
echo "<td>". $listening . "</td>";
echo "<td>". $mood . "</td>";
echo "<td>". $stuff . "</td>";
echo "<td>". $piccy . "</td>";
echo "</tr>";
}
echo "</table>";
}
?>
/* note: this is NOT how the entry will appear, just a test of getting the data back for a specific record (the latest one). Using this approach, however, I have all the bits in variables that I can place anywhere I want on the final blarg screen which is good - that way I can design a format template and embed the live data in it.
This is a sneaky way to do this and relies on PHP processing the page FROM THE TOP. The first query gets a value that the second query needs (the biggest blargid) */
<form action="loginresults.php" method="post" name="authentication" id="authentication">
<div align="center">
<p>:Blarg Admin Login : </p>
</div>
<table border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td>username:</td>
<td><input name="whoami" type="text" id="whoami" value="who are you?"></td>
</tr>
<tr>
<td>password:</td>
<td><input name="passphrase" type="password" id="passphrase"></td>
</tr>
<tr>
<td colspan="2"><div align="center">
<input type="submit" name="Submit" value="try logging in">
</div></td>
</tr>
</table>
</form>
12 - Process the Username and Password to determine if they are right
<?php
include ("connecttothedatabase.php");
if (($_POST['whoami'] == "tester") && ($_POST['passphrase'] == "letmein"))
{
echo "congrats, welcome ".$_POST['whoami'];
/* rest of page functionality goes here */
}
else
{
echo "sorry, you should go away now, you do not have edit rights on this screen";
}
echo "<br><br><a href='loginthingy.htm'>log out or try again</a>";
?>
/* note: the authentication will be necessary as adding a blarg entry (or editing one if I work out how to do that) require the username and password, otherwise anyone could do it and we cannot have that, can we?
Initial BLUNDER - I used the form element names "username" and "password" and it would NOT AUTHENTICATE CORRECTLY - WHY??? (der!, a username variable is used in the INCLUDE file, so overwrites the incoming value from the login form - wasted 15mins trying to nut that out).
Also, in C, Java and other derivative languages like PHP, the "==" is IMPORTANT (means is the same as) as opposed to "=" (meaning takes on the value of). By mistakenly typing: if (($_POST['whoami'] ="tester") && ($_POST['passphrase'] ="letmein")) you would be clobbering the entered values from the form and replacing them with the required values - meaning you could type any old rubbish on the form and still be let in here - this is not ideal, surely.
One day I will work out how to change passwords also - presumably it would need to be stored in a database table. I am still investigating how SECURE this form of user authentication is - me thinks if it is this simple to write, it MUST not be very secure.*/
<?php
include ("connecttothedatabase.php");
/* now retrieve data filtered on the supplied value*/
$gimme = "select * from blargtable where blargid = $which";
$data = mysql_query($gimme, $connection);
if (!$data)
{
echo "no data was returned";
}
else
{
echo "<table cellpadding=5 border=1 cellspacing=0>";
while (list($blargid, $blargdate, $topic, $header, $watching,
$reading, $listening, $mood, $stuff, $piccy) = mysql_fetch_row($data))
{
echo "<tr>";
echo "<td>". $blargid . "</td>";
...snip
/* Note: this is a variation of the "find the latest blarg entry" with the cunning variation that you can activate it via a supplied value at the URL - for example, to get the 2nd blarg entry, you would enter the url
http://www.wonko.info/blarg/nominateasingleentry.php?which=2 - notice the "which=2" part after the "?" supplies the value to the variable $which in the script.
This works so long as you remember to supply a value (and the name of the variable does not correspond to any other variables - else side effects might emerge*/
<?php
include ("connecttothedatabase.php");
$gimme = "select * from blargtable";
/* attempt to retrieve the data */
$data = mysql_query($gimme, $connection);
if (!$data)
{
echo "no data was returned";
}
else
{
echo "<table cellpadding=5 border=1 cellspacing=0>";
while (list($blargid, $blargdate, $title, $header, $watching,
$reading, $listening, $mood, $stuff, $piccy) = mysql_fetch_row($data))
{
echo "<tr>
<td>
<a href=nominateasingleentry.php?which=".$blargid.">".$title."</a>
</td>
</tr>";
}
echo "</table>";
}
?>
/* Note: I embedded the blargID in the "which" part of the URL and used the title as the link text.
I figure this will be useful for browsing and editing - being able to nominate an entry is pretty useful.*/
...snip
echo "<table cellpadding=5 border=1 cellspacing=0>";
while (list($blargid, $blargdate, $title, $header, $watching,
$reading, $listening, $mood, $stuff, $piccy) = mysql_fetch_row($data))
{
echo "<tr>
<td><a href=nominateasingleentry.php?which=".$blargid.">".$title."</a></td>
<td><a href=modifyentry.php?which=".$blargid."&what=edit>edit</a></td>
<td><a href=modifyentry.php?which=".$blargid."&what=delete>delete</a></td>
</tr>";
}
echo "<tr><td><a href=inputdataform.htm>add a new entry</a></td></tr>";
echo "</table>";>
snip....
/*Note: This modification allows you to SEE the entry by clicking on it's title via the nomimateasingleentry.php script or edit/delete it via the modifyentry.php script (described below)*/
16 - Do a Modify Screen
<?php
include ("connecttothedatabase.php");
/* now retrieve data filtered on the supplied value */
$gimme = "select * from blargtable where blargid = $which";
$data = mysql_query($gimme, $connection);
if (!$data)
{
echo "no data was returned";
}
else
{
while (list($blargid, $blargdate, $topic, $header, $watching,
$reading, $listening, $mood, $stuff, $piccy) = mysql_fetch_row($data))
{
if ($what == "edit")
{
echo "<h3>Editing Record</H3>";
}
else if($what == "delete")
{
echo "<h3>Deleting Record</H3>";
}
echo "<form action=modificationresults.php method=POST>";
echo "<table border=0 align=center cellpadding=3 cellspacing=0>";
echo " <tr>";
echo " <td>topic:</td>";
echo " <td><input name=topic type=text value='".$topic."' size=80></td>";
echo " </tr>";
echo " <tr>";
echo " <td>header:</td>";
echo " <td><textarea name=header cols=80 rows=3>".$header."</textarea></td>";
echo " </tr>";
echo " <tr>";
echo " <td>watching:</td>";
echo " <td><input name=watching type=text value='".$watching."' size=80></td>";
echo " </tr>";
echo " <tr>";
echo " <td>reading:</td>";
echo " <td><input name=reading type=text value='".$reading."' size=80></td>";
echo " </tr>";
echo " <tr>";
echo " <td>listening:</td>";
echo " <td><input name=listening type=text value='".$listening."' size=80></td>";
echo " </tr>";
echo " <tr>";
echo " <td>mood:</td>";
echo " <td><input name=mood type=text value='".$mood."' size=80></td>";
echo " </tr>";
echo " <tr>";
echo " <td>stuff:</td>";
echo " <td><textarea name=stuff cols=80 rows=4>".$stuff."</textarea></td>";
echo " </tr>";
echo " <tr>";
echo " <td>piccy:</td>";
echo " <td><input name=piccy type=text value='".$piccy."' size=80></td>";
echo " </tr>";
echo " <tr>";
echo " <td> </td>";
echo " <td><input type=SUBMIT value='just do it'></td>";
echo " </tr>";
echo "</table>";
echo "<input type=hidden name=which value='".$which."'>";
echo "<input type=hidden name=what value='".$what."'>";
echo "</form>";
echo "<a href=betterlistofentries.php>back</a>";
}
}
?>
/* Note: This php file is bounced to in order to allow the user to CONFIRM their intentions - I thought it better to show them what they were deleting or provide them with an editable copy before modification. You will see I have embedded the data that they should be allowed to edit (excluding date and blargid) amongst the form so it gives them a starting point for their edit rather than requiring they type it all in again.
The WHICH and WHAT correspond to WHICH blarg entry and WHAT to do with it (edit/delete) and those values come in to this file via the URL, and are passed on via the POST action.
Once they submit here, the next file processes their intention. The back link here is provided as a way out of the modification until I can think of a cleverer way. A really good application would probably put up one of those "are you really really really sure?" messages ... might do it later.
The last 2 hidden inputs are there to pass the values of what and which on to the next form (saves the user having to type their intention again), remembering that a POSTed form's fields are sent to the POST area of the server's memory as named variables and can be accessed in the linked file.*/
17 - Perform the nominated modification
<?php
include ("connecttothedatabase.php");
if (($_POST['what'] == "delete") || ($_POST['what'] == "edit"))
{
/* determine the query matching the course of action the user wants to take */
if ($_POST['what'] == "delete")
{ $dothis = "delete from blargtable where blargid = '".$_POST['which']."'"; }
else if ($_POST['what'] == "edit")
{ $dothis= "update blargtable
set topic='".$_POST['topic']."', header='".$_POST['header']."', watching='".$_POST['watching']."', reading='".$_POST['reading']."',
listening='".$_POST['listening']."', mood='".$_POST['mood']."', stuff='".$_POST['stuff']."', piccy='".$_POST['piccy']."'
where blargid='".$_POST['which']."'"; }
echo "<br><br>";
/* attempt to perform the action, reporting success or failure */
if (mysql_query($dothis))
{ echo "record ".$_POST['what']." completed successfully"; }
else
{ echo "had a problem - sorry"; }
}
echo "<br><br><a href=betterlistofentries.php>back</a>";
?>
/*Note: rather pleased with this one - got it right first go (which is saying something given the torturous punctuation). This script retrieves all the form variables (corresponding to an entire blarg entry) and then either removes it or updates the table row with the new values - nifty hey?*/
other things .....
if (mysql_query(....
gets turned into
if (@mysql_query(....
the '@' suppresses any PHP error that it encounters at run time. if you use it, u must remember to also check to see it the operation was successful
|