
|
View Full Version : Working with Text DB
latheesan 12-19-2005, 07:26 PM Hello,
I started to use this database software called PHP Text DB which uses text file but acts exactly like MySQL. The purpose of this software is to use SQL type database without using actual MySQL.
Take a look at what i created out of it for one of my friend's project
<?php
define('SCRIPT_PATH', $_SERVER['DOCUMENT_ROOT']);
include(SCRIPT_PATH.'/php-api/txt-db-api.php');
if(($_POST['movie']) && ($_POST['email'])){
$movie = $_POST['movie'];
$db = new Database("mp3");
$result = $db->executeQuery("SELECT * FROM password LIKE '" . $movie . "%'");
if($result->getRowCount() == 1){
echo "<b>Sorry, there are no password for that album!<b>";
}else{
while($result->next()){
$data = $result->getCurrentValuesAsHash();
$movie = $data['movie'];
$password = $data['password'];
echo "Movie Name: $movie
<br>
Passowrd: $password";
}
}
}else{
$db = new Database("mp3");
$result = $db->executeQuery("SELECT * FROM main");
if($result->getRowCount() == 0){
echo "Sorry, there are no albums in the database!";
}else{
echo "<form action=\"password.php\" method=\"post\">
<table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">
<tr>
<td width=\"12%\" height=\"25\" align=\"left\" valign=\"middle\">Select Album</td>
<td width=\"2%\" height=\"25\" align=\"left\" valign=\"middle\"></td>
<td width=\"86%\" height=\"25\" align=\"left\" valign=\"middle\">
<select size=\"1\">
<option value=\"\">album list...</option>";
while($result->next()){
$data = $result->getCurrentValuesAsHash();
$movie = $data['movie'];
echo "<option name=\"$movie\" value=\"$movie\">$movie</option><br>";
}
echo "</select>
</td>
</tr>
<tr>
<td width=\"12%\" height=\"25\" align=\"left\" valign=\"middle\">Your Email Address</td>
<td width=\"2%\" height=\"25\" align=\"left\" valign=\"middle\"></td>
<td width=\"86%\" height=\"25\" align=\"left\" valign=\"middle\">
<input type=\"text\" name=\"email\" size=\"30\"></td>
</tr>
<tr>
<td width=\"12%\" height=\"25\" align=\"left\" valign=\"middle\"></td>
<td width=\"2%\" height=\"25\" align=\"left\" valign=\"middle\"></td>
<td width=\"86%\" height=\"25\" align=\"left\" valign=\"middle\">
<input type=\"submit\" value=\"Send Password\"></td>
</tr>
</table>
</form>";
}
}
?>
What is suppose to do is, first check if end user has selected an album from the drop down list and also entered his or her email address, if yes, show the password for that album otherwise, show the dropdown list and a field to enter the email address.
Pretty simple stuff, but it isnt working. When i peview this code, all i keep seeing is the drop down list and the field to enter the email address, when i click "send password", it should display the password, but it isnt doing it.
This is exactly same as using MySQL, so i thought someone could help me figure out where i went wrong...
Thanks in advance for any help you can offer me.
Regards,
Lathee
latheesan 12-19-2005, 07:58 PM i Slightly modified the script after sooooooo many experimentation with the script. This is what i ended up at:
<?php
define('SCRIPT_PATH', $_SERVER['DOCUMENT_ROOT']);
include(SCRIPT_PATH.'/php-api/txt-db-api.php');
if(($_POST['movie']) && ($_POST['email'])){
$movie = $_POST['movie'];
$email = $_POST['email'];
$db = new Database("mp3");
$result = $db->executeQuery("SELECT * FROM password LIKE '%" . $movie . "%'");
if($result->getRowCount() == 1){
$data = $result->getCurrentValuesAsHash();
$movie = $data['movie'];
$password = $data['password'];
echo "Your Email: $email
<br>
Movie Name: $movie
<br>
Passowrd: $password";
}
}else{
$db = new Database("mp3");
$result = $db->executeQuery("SELECT * FROM main");
echo "<form name=\"album\" action=\"password.php\" method=\"post\">
<table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">
<tr>
<td width=\"12%\" height=\"25\" align=\"left\" valign=\"middle\">Select Album</td>
<td width=\"2%\" height=\"25\" align=\"left\" valign=\"middle\"></td>
<td width=\"86%\" height=\"25\" align=\"left\" valign=\"middle\">
<select name=\"album\" size=\"1\">
<option value=\"\">album list...</option>";
while($result->next()){
$data = $result->getCurrentValuesAsHash();
$movie = $data['movie'];
echo "<option value=\"$movie\">$movie</option><br>";
}
echo "</select>
</td>
</tr>
<tr>
<td width=\"12%\" height=\"25\" align=\"left\" valign=\"middle\">Your Email Address</td>
<td width=\"2%\" height=\"25\" align=\"left\" valign=\"middle\"></td>
<td width=\"86%\" height=\"25\" align=\"left\" valign=\"middle\">
<input type=\"text\" name=\"email\" size=\"30\"></td>
</tr>
<tr>
<td width=\"12%\" height=\"25\" align=\"left\" valign=\"middle\"></td>
<td width=\"2%\" height=\"25\" align=\"left\" valign=\"middle\"></td>
<td width=\"86%\" height=\"25\" align=\"left\" valign=\"middle\">
<input type=\"submit\" size=\"30\" value=\"Send Password\"></td>
</tr>
</table>
</form>";
}
?>
This prob is driving me nuts for the past 3 hours :(
latheesan 12-19-2005, 08:07 PM I thought there was something wrong with the select query part of my script, so i took it out and tried it separately, it works!!!
This is how the code looks like separately:
<?php
define('SCRIPT_PATH', $_SERVER['DOCUMENT_ROOT']);
include(SCRIPT_PATH.'/php-api/txt-db-api.php');
$movie = "MovieName";
$email = "someone@mail.com";
$db = new Database("mp3");
$result = $db->executeQuery("SELECT * FROM password LIKE '%" . $movie . "%'");
if($result->getRowCount() == 0){
echo "Sorry, that movie doesnt exist!";
}else{
while($result->next()){
$data = $result->getCurrentValuesAsHash();
$movie = $data['movie'];
$password = $data['password'];
echo "Your Email: $email
<br>
Movie Name: $movie
<br>
Passowrd: $password";
}
}
?>
Preview: http://img526.imageshack.us/img526/917/untitled4qq.jpg
latheesan 12-19-2005, 08:08 PM Is there something wrong with the way im posting the movie name from the drop down list and the email address from the field?
mdnava 12-19-2005, 08:17 PM Well, for starters your first SQL query seems to be wrong:
("SELECT * FROM password LIKE '" . $movie . "%'");
It should be something like this:
("SELECT password FROM tablename WHERE userid = '%" . $movie . "%' ");
You should use the WHERE clause in SQL yo define your search and if you are returning only one value of one column you should also specify which column (password?) and a unique value like the "autonumeric" id.
Bare in mind that with text files you will never have the same as when working with a fully featured database server, text files won't give good performance or fast queries, in fact it will get you suspended if you have a high loaded website and your queries will take long if your database gets big.
Another thing, you should never ever store plain-text unencrypted passwords in a database or in text files, if your users need to view its passwords just say NO, is better if you encrypt them one-way and then only allow passwords changes.
latheesan 12-19-2005, 08:20 PM Hello mdnava, thanks for ur reply, im not sure if the text db software can work with "WHERE" clause because in the example script, they used "LIKE"
anyhow, let me try ur suggestion immediately and see what happens... fingers crossed
latheesan 12-19-2005, 08:23 PM i changed the my code to ur suggestion and tried it... it didnt work
i think there is something wrong with the way the data is posted... just cant put my finger on whats wrong :(
mdnava 12-19-2005, 08:30 PM Then it doesn't act exactly like MySQL...
I'm not familiar with "PHP Text DB" but you I could start from scratch with the examples at hand to make sure your script is fully compliant with it.
Don't you have access to MySQL?... You would save a lot of pain if you can start with a database server at first.
latheesan 12-19-2005, 08:44 PM i do have mysql, and i tested it and it works. i have to port this to text db, because im restricted to 3 mysql databases in total and each have limit number of simultaneous connection.
Also... i mentionned tis before, the script by it self without the form posting stuff, it works, it just doesnt work with the drop down list, thats what pissing me off
mdnava 12-19-2005, 09:14 PM If you haven't reached your simultaneous connections, you may reuse the same database for several different scripts... I usually use only one database for an entire website even if there are forums, blogs and classifieds; I just put a prefix (like blog_, forum_, etc.) to the column names to separate them.
Even tho I own my servers, I have one site with many different apps working with only one database... This is a good practice that saves time in further maintenance.
You might wanna consider also to change your provider, I mean, there are really cheap offers with more DBs available...
latheesan 12-20-2005, 03:42 AM I know, but the issue here isnt about having enough mysql databases...
Its just that, the top part of my script that shows the movie name and password is working on its out, but it wont work when i integrate it with a drop down list and end user input of their email
surely if i did the same in mysql, i will encounter the same mistake,,,
if it isnt much, mdnava.. could you re-write what i done, for mysql? I will port it for Text DB (which will definetly work, because i have ported 3 scripts so far to work with Text DB without any probs)...
Googled 12-20-2005, 03:49 AM Hi,
Text DB is good but far away from greatness. Many bugs (auto_increment, per example) and not much development in the near future.
This reply doesn't answer anything although :) Except porting a DB script to Text DB, in my opinion, not the greatest idea.
*my two cents*
G
sea otter 12-20-2005, 04:03 AM Well, here's a minor mod of your page that does work. Kinda quick, sample data, global vars, but it's functioning, and it does what you want. Just save it as a file named password.php and run it. You can then mod it as needed to use your actual functions.
<?
/* won't need these fake movies and passwords in production */
$fake_db = array(
'A Bridge too Far' => 'password_1',
'Dr. No' => 'password_2',
'Convoy' => 'password_3',
'Shaun of the Dead' => 'password_4'
);
/* first, validate our form input if there is any */
$movie = $_POST['album'] ? $_POST['album'] : 'nothing yet...';
$email = $_POST['email'] ? $_POST['email'] : '';
/*
this function builds the movie select list. notice that it also
marks the current movie (if any) as "selected". This is in case
the user didn't enter an email address and got sent back to the form
*/
function build_movie_list()
{
global $fake_db; //globals yucky, I know, but just an example
global $movie;
$list = '';
foreach($fake_db as $title => $password)
{
$list .= "<option value=\"$title\"";
if (strcmp($movie,$title) == 0) $list .= " selected=\"selected\" ";
$list .= ">$title</option>\n";
}
return $list;
}
/* print out any error messages */
function echo_results()
{
global $movie;
global $email;
global $fake_db;
if (!$email && strcmp($movie,'nothing yet...') != 0)
echo '<p><strong>Please enter your email address.</strong></p>';
if (!isset($fake_db[$movie]) && strcmp($movie,'nothing yet...') != 0)
echo '<p><strong>The movie was not found.</strong></p>';
if ($email && strcmp($movie,'nothing yet...') != 0)
{
echo "<pre>";
echo "<p>Thank you.</p>";
echo "<p>Movie: <strong>" . htmlentities($movie) . "</strong></p>";
echo "<p>Password: <strong>" . htmlentities({$fake_db[$movie]}) . "</strong></p>";
echo "</pre>";
}
}
/*
if we got here, it means we need to display our form, so just fall through.
*/
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Movie Order Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<?= echo_results(); ?><br />
<form name="album" action="password.php" method="post">
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td width="12%" height="25" align="left" valign="middle">Select Album</td>
<td width="2%" height="25" align="left" valign="middle"></td>
<td width="86%" height="25" align="left" valign="middle">
<select name="album" size="1">
<option value="">album list...</option>
<?= build_movie_list(); ?>
</select>
</td>
</tr>
<tr>
<td width="12%" height="25" align="left" valign="middle">Your Email Address</td>
<td width="2%" height="25" align="left" valign="middle"></td>
<td width="86%" height="25" align="left" valign="middle">
<input type="text" name="email" size="30" value=""></td>
</tr>
<tr>
<td width="12%" height="25" align="left" valign="middle"></td>
<td width="2%" height="25" align="left" valign="middle"></td>
<td width="86%" height="25" align="left" valign="middle">
<input type="submit" size="30" value="Send Password"></td>
</tr>
</table>
</form>
</body>
</html>
sea otter 12-20-2005, 04:31 AM Always happens, doesn't it?
Look for this:
{$fake_db[$movie]}
And replace it with this:
$fake_db[$movie]
(remove the curly braces)
Sorry :(
latheesan 12-20-2005, 11:05 AM OMFG :beer: Thankss alot sea otter for taking your time and providing this wonderful example
It works exactly like i want it to :D
:lovewht: :lovewht: :lovewht:
latheesan 12-20-2005, 11:07 AM sea otter, where do you usually hang around the most? At WHT or IWDN ?
latheesan 12-20-2005, 12:14 PM Oki sea otter, i modified it and this is how it looks like now:
<?
/* won't need these fake movies and passwords in production */
define('SCRIPT_PATH', $_SERVER['DOCUMENT_ROOT']);
include(SCRIPT_PATH.'/php-api/txt-db-api.php');
$db = new Database("mp3");
$result = $db->executeQuery("SELECT * FROM password");
if($result->getRowCount() == 0){
$movie_db = "No movies in database...";
}else{
while($result->next()){
$data = $result->getCurrentValuesAsHash();
$movie = $data['movie'];
$password = $data['password'];
$movie_db = array('$movie' => '$password');
}
}
/* first, validate our form input if there is any */
$movie = $_POST['album'] ? $_POST['album'] : 'nothing yet...';
$email = $_POST['email'] ? $_POST['email'] : '';
/*
this function builds the movie select list. notice that it also
marks the current movie (if any) as "selected". This is in case
the user didn't enter an email address and got sent back to the form
*/
function build_movie_list()
{
global $movie_db;
global $movie;
$list = '';
foreach($movie_db as $title => $password)
{
$list .= "<option value=\"$title\"";
if (strcmp($movie,$title) == 0) $list .= " selected=\"selected\" ";
$list .= ">$title</option>\n";
}
return $list;
}
/* print out any error messages */
function echo_results()
{
global $movie;
global $email;
global $movie_db;
if (!$email && strcmp($movie,'nothing yet...') != 0)
echo '<p><strong>Please enter your email address.</strong></p>';
if (!isset($movie_db[$movie]) && strcmp($movie,'nothing yet...') != 0)
echo '<p><strong>The movie was not found.</strong></p>';
if ($email && strcmp($movie,'nothing yet...') != 0)
{
echo "<pre>";
echo "<p>Thank you.</p>";
echo "<p>Movie: <strong>" . htmlentities($movie) . "</strong></p>";
echo "<p>Password: <strong>" . htmlentities($movie_db[$movie]) . "</strong></p>";
echo "</pre>";
}
}
/*
if we got here, it means we need to display our form, so just fall through.
*/
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Movie Order Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<?= echo_results(); ?><br />
<form name="album" action="1.php" method="post">
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td width="12%" height="25" align="left" valign="middle">Select Album</td>
<td width="2%" height="25" align="left" valign="middle"></td>
<td width="86%" height="25" align="left" valign="middle">
<select name="album" size="1">
<option value="">album list...</option>
<?= build_movie_list(); ?>
</select>
</td>
</tr>
<tr>
<td width="12%" height="25" align="left" valign="middle">Your Email Address</td>
<td width="2%" height="25" align="left" valign="middle"></td>
<td width="86%" height="25" align="left" valign="middle">
<input type="text" name="email" size="30" value=""></td>
</tr>
<tr>
<td width="12%" height="25" align="left" valign="middle"></td>
<td width="2%" height="25" align="left" valign="middle"></td>
<td width="86%" height="25" align="left" valign="middle">
<input type="submit" size="30" value="Send Password"></td>
</tr>
</table>
</form>
</body>
</html>
What happens if i run this file now is, i get a drop down list, and there is only one option, $movie :S
also, when i select that option and enter my email and press send pass, it outputs this:
Thank you.
Movie: $movie
Password: $password
How do build an array of movie names and passwords from the db?
sea otter 12-20-2005, 02:03 PM sea otter, where do you usually hang around the most? At WHT or IWDN ?
I only drop by the development forums here about once a week or so. I'm in the other major forums here almost daily. But I have email notifications on by default, so I always get alerted to changes in threads I've answered.
As for the problems, here's one quick thing I noticed:
change this:
$movie_db = array('$movie' => '$password');
To this:
$movie_db = array($movie => $password);
(remove the single quotes). PHP doesn't evaluate anything inside of single quotes, so the variable names weren't getting resolved.
That should fix both issues.
latheesan 12-20-2005, 04:02 PM OMFG :kaioken:
if only i knew this, i woulda removed the single quotes and it would have worked this morning and i would have continued with my scriptingg and be done by now!!
arghhhhhhhhhh... 6 hours wasted :( owww welll
anyhow, thanks sea otter, your suggestion worked greattt :angel:
latheesan 12-20-2005, 06:13 PM Hey sea otter...
This is what i finally came up with:
<?
define('SCRIPT_PATH', $_SERVER['DOCUMENT_ROOT']);
include(SCRIPT_PATH.'/php-api/txt-db-api.php');
$db = new Database("mp3");
$result = $db->executeQuery("SELECT * FROM password");
if($result->getRowCount() == 0){
$movie_db = "No movies in database...";
}else{
while($result->next()){
$data = $result->getCurrentValuesAsHash();
$movie = $data['movie'];
$password = $data['password'];
$movie_db = array($movie => $password);
}
}
$movie = $_POST['album'] ? $_POST['album'] : 'nothing yet...';
$email = $_POST['email'] ? $_POST['email'] : '';
function build_movie_list()
{
global $movie_db;
global $movie;
$list = '';
foreach($movie_db as $title => $password)
{
$list .= "<option value=\"$title\"";
if (strcmp($movie,$title) == 0) $list .= " selected=\"selected\" ";
$list .= ">$title</option>\n";
}
return $list;
}
function echo_results()
{
global $movie;
global $email;
global $movie_db;
if (!$email && strcmp($movie,'nothing yet...') != 0)
echo "<font face=\"Verdana\" size=\"2\" color=\"#CC0000\"><b>ERROR - Please
enter your email address</b><br><br>";
if (!isset($movie_db[$movie]) && strcmp($movie,'nothing yet...') != 0)
echo "<font face=\"Verdana\" size=\"2\" color=\"#CC0000\"><b>ERROR - That
movie was not found!</b><br><br>";
if ($email && strcmp($movie,'nothing yet...') != 0)
{
$email = $_POST['email'];
echo "<font face=\"Verdana\" size=\"2\" color=\"#009900\">";
echo "<b>Your Email:</b> $email<br>";
echo "<b>Movie:</b> " . htmlentities($movie) . "<br>";
echo "<b>Password:</b> " . htmlentities($movie_db[$movie]) . "";
echo "<br><br>";
}
}
?>
<?= echo_results(); ?><br />
<form name="album" action="password.php" method="post">
<table border="0" width="100%" cellspacing="0" cellpadding="0" class="b-txt">
<tr>
<td width="12%" height="25" align="left" valign="middle">
<font face="Verdana">
<span style="font-size: 8pt">Select Album</span></font></td>
<td width="2%" height="25" align="left" valign="middle"></td>
<td width="86%" height="25" align="left" valign="middle">
<select name="album" size="1" >
<option value="">album list...</option>
<?= build_movie_list(); ?>
</select>
</td>
</tr>
<tr>
<td width="12%" height="25" align="left" valign="middle">
<font face="Verdana">
<span style="font-size: 8pt">Your Email Address</span></font></td>
<td width="2%" height="25" align="left" valign="middle"></td>
<td width="86%" height="25" align="left" valign="middle">
<input type="text" name="email" size="40" ></td>
</tr>
<tr>
<td width="12%" height="25" align="left" valign="middle"></td>
<td width="2%" height="25" align="left" valign="middle"></td>
<td width="86%" height="25" align="left" valign="middle">
<input type="submit" size="30" value="Send Password" ></td>
</tr>
</table>
</form>
Everything is working fine up-until i moved my script to my linux server. Basically, i work on my home server to create my scripy, similarly i worked on this on my home server running php5 and everything worked fine.
When i moved my script to my linux server with php4 running, what happens is that, the error messages are getting printed even tho the end user didnt submit the form
why is this? :S
sea otter 12-20-2005, 07:34 PM I'm running php4 local and remote, so i'll take a look and post back.
latheesan 12-20-2005, 08:10 PM thanks sea otter =)
sea otter 12-20-2005, 08:25 PM ok, a couple of fixes:
replace this:
$movie_db = "No movies in database...";
with this:
$movie_db = array();
(we're creating an empty array of movies)
And replace this:
$movie_db = array($movie => $password);
with this:
$movie_db[] = array($movie => $password);
(by adding the [] , you're tellilng php to append the latest record to the array and not replace it)
If fixing those two syntax errors doesn't do it, I'll have to dig deeper.
latheesan 12-20-2005, 09:35 PM after i made the changes like u suggested,
my drop down list of movies dissappeared (only see one option, that is "album list..." no other options) and the error message is still being outputted without the end user submitting the form :S
latheesan 12-21-2005, 05:54 AM Im not exactly sure what happened, but after i did the changes like you suggested, the drop down list stopped stop generating options of movies in the db
Secondly, when i put the script back the way it was, before the changes you asked me to make, the script is still the same, now it still wont product the drop down list :S
What happened? :bawling:
sea otter 12-21-2005, 12:15 PM I can think of only one thing, because this has happened to me, too.
When I cut and paste code from WHT into my editor, my programs always stop working. It seems that some odd, non-visible characters get copied over which break php when it runs.
Even backspacing or erasing what I pasted doesn't fix it.
My program editor has an option called "convert to ASCII" which basically erases all non-ascii characters from the file. That always fixes the problem.
Or, do it the hard way -- type everything by HAND into a NEW file.
I've had that happen soooooo many times in the past.
latheesan 12-21-2005, 01:17 PM ermmm
okay, let me write it word for word again into a new file then and try it
latheesan 12-21-2005, 01:19 PM btw. do i still keep these following changes:
replace this:
$movie_db = "No movies in database...";
with this:
$movie_db = array();
(we're creating an empty array of movies)
And replace this:
$movie_db = array($movie => $password);
with this:
$movie_db[] = array($movie => $password);
sea otter 12-21-2005, 01:20 PM yes. They fix some syntax errors. :)
latheesan 12-21-2005, 02:16 PM hey sea otter
i re-wrote the script from scratch by copying ur post, my new scipt looks like this
<?php
define('SCRIPT_PATH', $_SERVER['DOCUMENT_ROOT']);
include(SCRIPT_PATH.'/php-api/txt-db-api.php');
/* Create table and insert data into it */
if(!file_exists(DB_DIR . "mp3/passwords.txt"))
{
$db = new Database("mp3");
$db->executeQuery("CREATE TABLE passwords(movie str, password str)");
$db->executeQuery("INSERT INTO passwords(movie, password) VALUES('Kong', '955ibp')");
$db->executeQuery("INSERT INTO passwords(movie, password) VALUES('Tear Away', '825klx')");
$db->executeQuery("INSERT INTO passwords(movie, password) VALUES('Stolen', '712xhq')");
}
/* Build an array of movie lists and passwords */
$db = new Database("mp3");
$result = $db->executeQuery("SELECT * FROM passwords");
if($result->getRowCount() == 0)
{
$movie_db = array();
}
else
{
while($result->next())
{
$data = $result->getCurrentValuesAsHash();
$movie = $data['movie'];
$password = $data['password'];
$movie_db[] = array($movie -> $password);
}
}
/* Validate form input */
$movie = $_POST['album'] ? $_POST['album'] : '';
$email = $_POST['email'] ? $_POST['email'] : '';
/* Build the drop down list options */
function build_list()
{
global $movie_db;
global $movie;
$list = '';
foreach($movie_db as $titles => $password)
{
$list .= "<option value=\"$title\"";
if(strcmp($movie,$title) == 0)
$list .= " selected=\"selected\" ";
$list .= ">$title</option>\n";
}
return $list;
}
/* Print out any error messages */
function echo_results()
{
global $movies;
global $email;
global $movie_db;
if(!email && strcmp($movie, '') != 0)
echo '<b>Please enter your email address</b>';
if(!isset($movie_db[$movie]) && strcmp($movie, '') != 0)
echo '<b>Sorry, that movie was not found on the DB</b>';
if($email && strcmp($movie, '') != 0)
{
echo "<pre>";
echo "<p>Thank You.</p>";
echo "<p>Movie Name:" . htmlentities($movie) . "</p>";
echo "<p>Movie Password:" . htmlentities($movie_db[$movie]) . "</p>";
echo "</pre>";
}
}
/* Print out the html form */
?>
<html>
<head>
<title>Movie Password Form</title>
</head>
<body>
<?= echo_results(); ?><br />
<form name="album" action="Password.php" method="post">
<table border="0" width="100%">
<tr>
<td width="12%" height="25">Select Album</td>
<td width="2%" height="25"></td>
<td width="86%" height="25">
<select name="album" size="1">
<option value="">album list...</option>
<?= build_list(); ?>
</select></td>
</tr>
<tr>
<td width="12%" height="25">Your Email</td>
<td width="2%" height="25"></td>
<td width="86%" height="25">
<input type="text" name="email" size="30"><td>
</tr>
<tr>
<td width="12%" height="25"></td>
<td width="2%" height="25"></td>
<td width="86%" height="25">
<input type="submit" value="Send Password"><td>
</tr>
</table>
</form>
</body>
</html>
I havent tried this script on the linux server yet, but on my local server (windows), what i see when i run the script is, a blank option on the drop down list (even though there are records of movies and passwords)
How cums?
latheesan 12-21-2005, 02:17 PM Also, the error messages are not getting printed as well :S
latheesan 12-21-2005, 02:27 PM well, i test ran the script on my linux server.
Some good news.. the error messages are not getting output before the user submits the form
Some bads news.. there isnt a drop down list of movies. Also, the error messages are not getting printed when the user posts the form without selecting a movie or email address :S
Any idea whats causing the error now?
sea otter 12-21-2005, 03:24 PM Yes, a typo in your script :)
Change this:
foreach($movie_db as $titles => $password)
to this:
foreach($movie_db as $title => $password)
In the foreach loop, you use the variable name $titles with an 's' at the end.
In the body of the loop, you use the variable name $title without the 's'
latheesan 12-21-2005, 03:56 PM Ohh, how stupid of me..
I fixed it and tried the script. From the script i posted, you would have noticed that there are 3 records of movie and passwords right?
well, now that the drop down list is working, it is working completely differently
what i mean is, i only see one option of the movie name from the drop down list, ALSO, the name of the movie insnt getting printed on the drop downlist, instead a number is getting printer
here, this is what i mean: http://img331.imageshack.us/img331/8820/untitled1tv.jpg
i believe, this error is caused because of the [] next to $movie_db
what do u think?
sea otter 12-21-2005, 05:52 PM Oh, you definitely need the []!
But you're close -- $movie_db isn't being initialized as an array. So...here's what you need:
First, put this at the top of the file:
$movie_db = array();
Then, you can change the getRowCount call to this:
if($result->getRowCount() > 0)
{
while($result->next())
{
$data = $result->getCurrentValuesAsHash();
$movie = $data['movie'];
$password = $data['password'];
$movie_db[] = array($movie -> $password);
}
}
latheesan 12-21-2005, 06:30 PM ahhhhh,
I see, lolz.
I just finished changing the script and ran it on my server and this is the result:
http://img449.imageshack.us/img449/958/untitled3pz.jpg
Id say im getting closer. cause, its creating 3 options in the drop down list, which is equal to the number of records in the table
Ohh yea, the error messages not working btw..
Maybe i should change the form input validation to this:
$movie = $_POST['album'] ? $_POST['album'] : 'NoInput';
$email = $_POST['email'] ? $_POST['email'] : 'NoInput';
and then later in the script, i should change the code like this:
if(!email && strcmp($movie, 'NoInput') != 0)
echo '<b>Please enter your email address</b>';
if(!isset($movie_db[$movie]) && strcmp($movie, 'NoInput') != 0)
echo '<b>Sorry, that movie was not found on the DB</b>';
would this make the error function to work?
latheesan 12-21-2005, 09:36 PM nope, that didnt fix the error function
anyway, sea otter, i thought u wud come online after u came from dinner, but it looks like ur time is diff from mine
lolz, its about 1.30am here in uk, so im gonna sleep and wake up to see ur reply :D
sea otter 12-22-2005, 02:18 AM ah, ok, here's what you need to change:
This:
$movie_db[] = array($movie -> $password);
To this:
$movie_db[$movie] = $password;
As for the error messages not working, I'm not sure. I really only threw something quick in as an example -- it's not the most efficient way for doing real-world error checking.
You should probably come up with some logic as to exactly how detailed you want the error checking to be:
no email address
invalid email address
no movie selected
movie not found
etc. etc.
and then decide if you want to present the user with a list of *all* the errors you encountered, or only display the first (which means that after correcting the problem and resubmitting the form, the user will then be shown the NEXT error).
I always show the user all the errors at once (or as many as I can detect at a time) in a bulleted list at the top of the page, with very visible CSS styling applied so that they see as many ways as possible what went wrong.
In production, it's not unusual for error checking and user handholding code to double the size of the codebase because you have to check for so many ways that things can go wrong (and that the user can try to break your system). So...
Quick sample code really isn't meant to be turned into production code :(
latheesan 12-22-2005, 07:12 AM hey sea otter,
Your way of error checking was fine for the script i was making, because its basic and simple.
Also, your suggestion now worked. I see the real list of movies. But the movie and password is not showing when i press "Send Password" button
Current Code:
<?php
define('SCRIPT_PATH', $_SERVER['DOCUMENT_ROOT']);
include(SCRIPT_PATH.'/php-api/txt-db-api.php');
/* Create a blank array to begin */
$movie_db = array();
/* Then, build an array of movies and passwords */
$db = new Database("mp3");
$result = $db->executeQuery("SELECT * FROM passwords");
if($result->getRowCount() > 0)
{
while($result->next())
{
$data = $result->getCurrentValuesAsHash();
$movie = $data['movie'];
$password = $data['password'];
$movie_db[$movie] = $password;
}
}
/* Validate form input */
$movie = $_POST['album'] ? $_POST['album'] : 'NoData';
$email = $_POST['email'] ? $_POST['email'] : '';
/* Build the drop down list options */
function build_list()
{
global $movie_db;
global $movie;
$list = '';
foreach($movie_db as $title => $password)
{
$list .= "<option value=\"$title\"";
if(strcmp($movie,$title) == 0) $list .= " selected=\"selected\" ";
$list .= ">$title</option>\n";
}
return $list;
}
/* Print out any error messages first (if any). Then the acual result */
function echo_results()
{
global $movies;
global $email;
global $movie_db;
if(!email && strcmp($movie, 'NoData') != 0)
echo '<b>Please enter your email address</b>';
if(!isset($movie_db[$movie]) && strcmp($movie, 'NoData') != 0)
echo '<b>Sorry, that movie was not found on the DB</b>';
if($email && strcmp($movie, 'NoData') != 0)
{
echo "<pre>";
echo "<p>Thank You.</p>";
echo "<p>Your Email: $email</p>";
echo "<p>Movie Name:" . htmlentities($movie) . "</p>";
echo "<p>Movie Password:" . htmlentities($movie_db[$movie]) . "</p>";
echo "</pre>";
}
}
/* Print out the html form */
?>
<html>
<head>
<title>Movie Password Form</title>
</head>
<body>
<?= echo_results(); ?><br />
<form name="album" action="Password.php" method="post">
<table border="0" width="100%">
<tr>
<td width="12%" height="25">Select Album</td>
<td width="2%" height="25"></td>
<td width="86%" height="25">
<select name="album" size="1">
<option value="">album list...</option>
<?= build_list(); ?>
</select></td>
</tr>
<tr>
<td width="12%" height="25">Your Email</td>
<td width="2%" height="25"></td>
<td width="86%" height="25">
<input type="text" name="email" size="30"><td>
</tr>
<tr>
<td width="12%" height="25"></td>
<td width="2%" height="25"></td>
<td width="86%" height="25">
<input type="submit" value="Send Password"><td>
</tr>
</table>
</form>
</body>
</html>
Screenshot: http://img383.imageshack.us/img383/1157/untitled7zl1.jpg
I coppied your working example exactly, yet, it isnt printing the results correctly...
latheesan 12-22-2005, 04:47 PM My baddd...
i made a mistake in the function echo_results()
i had to change
global $movies;
to
global $movie;
Its working now, on windows that is. on linux server, its not building a drop down list of movies, and also, the default error message "Sorry, that movie was not found on DB" is getting printed without me even submitting the form :S
sea otter 12-22-2005, 04:50 PM Ah. good catch.
RE Unix/Windows -- windows uses a different kind of line ending than Linux:
crlf (carriage return and then a linefeed character) whereas Linux uses only one ("\n");
You'll need to either (a) resave the files in your program editor to have "unix" style line endings, or run a script (perl, php, .bat, .cmd whatever) to convert the line endings to unix style.
latheesan 12-22-2005, 04:57 PM erm sea otter, i dont understand what u mean....
also, look. its working fine on windows: http://img277.imageshack.us/img277/4010/test2kx.jpg
just keep messing in linux server (Free BSD OS)
latheesan 12-22-2005, 05:06 PM lolz, excuse the poor movie title
also, since ur online, i hope u come on WHTirc, im online now
sea otter 12-22-2005, 05:33 PM I'm on now as well.
latheesan 12-23-2005, 06:09 AM all fixed and working great :D
thanks for ur help so far thuru whtirc :D
sea otter 12-23-2005, 06:16 AM excellent. glad to help. :)
|